Skip to content

feat: Add a script for uploading browser bundles to CDN #1424

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

Merged
merged 1 commit into from
Jul 4, 2018
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -19,6 +19,7 @@
"packages/utils"
],
"devDependencies": {
"@google-cloud/storage": "^1.7.0",
"@types/chai": "^4.1.3",
"@types/jest": "^22.2.3",
"@types/mocha": "^5.2.0",
Expand Down
94 changes: 94 additions & 0 deletions scripts/browser-upload-cdn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
'use strict';

const Storage = require('@google-cloud/storage');
const path = require('path');
const os = require('os');
const fs = require('fs');

const bundleFilesRegex = /^bundle.*\.js.*$/;
const rootDir = path.dirname(__dirname);
const browserDir = path.join(rootDir, 'packages', 'browser');
const browserBuildDir = path.join(browserDir, 'build');

/** Return full paths of files to upload */
function findFiles() {
const bundleFiles = fs
.readdirSync(browserBuildDir)
.filter(filename => filename.match(bundleFilesRegex))
.map(filename => path.join(browserBuildDir, filename));
return bundleFiles;
}

/** Upload sentry-browser bundles to a GCS bucket */
async function uploadFiles() {
const gcsConfigPath =
process.env.BROWSER_GOOGLE_APPLICATION_CREDENTIALS ||
path.join(os.homedir(), '.gcs', 'sentry-browser-sdk.json');
console.log(`Reading GCS configuration from "${gcsConfigPath}"...`);

const gcsConfig = fs.existsSync(gcsConfigPath)
? JSON.parse(fs.readFileSync(gcsConfigPath))
: undefined;

if (!gcsConfig) {
console.error(
'Google Storage configuration (service account key) not found.\n' +
`Place it at ${gcsConfigPath} or use the environment variable ` +
'(BROWSER_GOOGLE_APPLICATION_CREDENTIALS) to specify the path.',
);
process.exit(1);
}

const projectId =
process.env.BROWSER_GOOGLE_PROJECT_ID || gcsConfig.project_id;
if (!projectId) {
console.error('Google project ID not found.');
process.exit(1);
}

const bucketName =
process.env.BROWSER_GOOGLE_BUCKET_NAME || gcsConfig.bucket_name;
if (!bucketName) {
console.error('Bucket name not found in the configuration.');
process.exit(1);
}

const bundleFiles = findFiles();
if (!bundleFiles.length) {
console.error('Error: no files to upload!');
process.exit(1);
}

const browserPackageJson = path.join(browserDir, 'package.json');
const version =
JSON.parse(fs.readFileSync(browserPackageJson)).version || 'unreleased';

const storage = new Storage({
projectId,
});

const bucket = storage.bucket(bucketName);
// TODO increase after some testing is done
const cacheAge = 300;

await Promise.all(
bundleFiles.map(async filepath => {
const destination = path.join(version, path.basename(filepath));
const options = {
gzip: true,
destination: destination,
metadata: {
cacheControl: `public, max-age=${cacheAge}`,
},
};
await bucket.upload(filepath, options);
console.log(`Uploaded "${destination}"`);
}),
);
console.log('Upload complete.');
}

uploadFiles().catch(error => {
console.error('Error occurred:', error);
process.exit(1);
});
Loading