Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(publisher-gcs): add Google Cloud Storage publisher #2100

Merged
merged 9 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@electron/get": "^2.0.0",
"@electron/osx-sign": "^1.0.5",
"@electron/rebuild": "^3.2.10",
"@google-cloud/storage": "^7.1.0",
"@malept/cross-spawn-promise": "^2.0.0",
"@octokit/core": "^3.2.4",
"@octokit/plugin-retry": "^3.0.9",
Expand Down
31 changes: 31 additions & 0 deletions packages/publisher/gcs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@electron-forge/publisher-gcs",
"version": "6.4.2",
"description": "Google Cloud Storage publisher for Electron Forge",
"repository": "https://github.com/electron/forge",
"author": "Evgeny Vlasenko",
"license": "MIT",
"main": "dist/PublisherGCS.js",
"typings": "dist/PublisherGCS.d.ts",
"devDependencies": {
"chai": "^4.3.3",
"mocha": "^9.0.1"
},
"engines": {
"node": ">= 14.17.5"
},
"dependencies": {
"@electron-forge/publisher-base": "6.4.2",
"@electron-forge/shared-types": "6.4.2",
"@google-cloud/storage": "^7.1.0",
"debug": "^4.3.1",
"google-auth-library": "^9.0.0"
mahnunchik marked this conversation as resolved.
Show resolved Hide resolved
},
"publishConfig": {
"access": "public"
},
"files": [
"dist",
"src"
]
}
59 changes: 59 additions & 0 deletions packages/publisher/gcs/src/Config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { PredefinedAcl } from '@google-cloud/storage';

export interface PublisherGCSConfig {
/**
* The path to the file that is either:
* - the JSON file that contains your Google service account credentials, or
* - the PEM/PKCS #12-formatted file that contains the private key.
*
* Defaults to the value in the `GOOGLE_APPLICATION_CREDENTIALS` environment variable.
*/
keyFilename?: string;
/**
* The Google Cloud project ID.
*
* Defaults to the value in the `GOOGLE_CLOUD_PROJECT` environment variable.
* */
projectId?: string;
/**
* The email for your Google service account, *required* when using a PEM/PKCS #12-formatted
* file in the [[keyFilename]] option.
*
* Defaults to the value in the `GOOGLE_CLOUD_CLIENT_EMAIL` environment variable.
*/
clientEmail?: string;
/**
* The private key for your Google service account.
*
* Defaults to the value in the `GOOGLE_CLOUD_PRIVATE_KEY` environment variable.
*/
privateKey?: string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment: For these auth-related options, can we put them in a separate auth object?

Copy link
Member

@erickzhao erickzhao Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the previous comment in this chain is still a valid concern. It might be cleaner to put all auth-related private key config options under an auth property.

/**
* The name of the Google Cloud Storage bucket where artifacts are uploaded.
*/
bucket?: string;
/**
* The key prefix where artifacts are uploaded, e.g., `my/prefix`.
*
* Defaults to the application `version` specified in the app's `package.json`.
*/
folder?: string;
/**
* Apply a predefined set of access controls to this object.
*/
predefinedAcl?: PredefinedAcl;
/**
* Whether to make uploaded artifacts public to the internet.
* Alias for config.predefinedAcl = 'publicRead'.
*/
public?: boolean;
/**
* Whether to make uploaded artifacts private.
* Alias for config.predefinedAcl = 'private'.
*/
private?: boolean;
/**
* Custom function to provide the key to upload a given file to
*/
keyResolver?: (fileName: string, platform: string, arch: string) => string;
erickzhao marked this conversation as resolved.
Show resolved Hide resolved
}
98 changes: 98 additions & 0 deletions packages/publisher/gcs/src/PublisherGCS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import path from 'path';

import { PublisherBase, PublisherOptions } from '@electron-forge/publisher-base';
import { Storage } from '@google-cloud/storage';
import debug from 'debug';
import { CredentialBody } from 'google-auth-library';

import { PublisherGCSConfig } from './Config';

const d = debug('electron-forge:publish:gcs');

type GCSArtifact = {
path: string;
keyPrefix: string;
platform: string;
arch: string;
};
erickzhao marked this conversation as resolved.
Show resolved Hide resolved

export default class PublisherGCS extends PublisherBase<PublisherGCSConfig> {
name = 'gcs';

private GCSKeySafe = (key: string) => {
return key.replace(/@/g, '_').replace(/\//g, '_');
};

async publish({ makeResults, setStatusLine }: PublisherOptions): Promise<void> {
const artifacts: GCSArtifact[] = [];

if (!this.config.bucket) {
throw new Error('In order to publish to Google Cloud Storage you must set the "bucket" property in your Forge config.');
}

for (const makeResult of makeResults) {
artifacts.push(
...makeResult.artifacts.map((artifact) => ({
path: artifact,
keyPrefix: this.config.folder || this.GCSKeySafe(makeResult.packageJSON.name),
platform: makeResult.platform,
arch: makeResult.arch,
}))
);
}

const storage = new Storage({
erickzhao marked this conversation as resolved.
Show resolved Hide resolved
keyFilename: this.config.keyFilename,
credentials: this.generateCredentials(),
projectId: this.config.projectId,
});

const bucket = storage.bucket(this.config.bucket);

d('creating Google Cloud Storage client with options:', this.config);

let uploaded = 0;
const updateStatusLine = () => setStatusLine(`Uploading distributable (${uploaded}/${artifacts.length})`);

updateStatusLine();
await Promise.all(
artifacts.map(async (artifact) => {
d('uploading:', artifact.path);

await bucket.upload(artifact.path, {
gzip: true,
erickzhao marked this conversation as resolved.
Show resolved Hide resolved
destination: this.keyForArtifact(artifact),
predefinedAcl: this.config.predefinedAcl,
public: this.config.public,
private: this.config.private,
});

uploaded += 1;
updateStatusLine();
})
);
}

keyForArtifact(artifact: GCSArtifact): string {
if (this.config.keyResolver) {
return this.config.keyResolver(path.basename(artifact.path), artifact.platform, artifact.arch);
}

return `${artifact.keyPrefix}/${artifact.platform}/${artifact.arch}/${path.basename(artifact.path)}`;
}

generateCredentials(): CredentialBody | undefined {
const clientEmail = this.config.clientEmail;
const privateKey = this.config.privateKey;

if (clientEmail && privateKey) {
return {
client_email: clientEmail,
private_key: privateKey,
};
}
return undefined;
}
}

export { PublisherGCS, PublisherGCSConfig };
Loading