Skip to content

Commit 2b890d7

Browse files
committed
feat: Add a script for uploading browser bundles to CDN
1 parent c4f11bb commit 2b890d7

File tree

5 files changed

+418
-14
lines changed

5 files changed

+418
-14
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"packages/utils"
2020
],
2121
"devDependencies": {
22+
"@google-cloud/storage": "^1.7.0",
2223
"@types/chai": "^4.1.3",
2324
"@types/jest": "^22.2.3",
2425
"@types/mocha": "^5.2.0",

packages/raven-js/Gruntfile.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ module.exports = function(grunt) {
158158
});
159159

160160
var awsConfigPath = path.join(os.homedir(), '.aws', 'raven-js.json');
161+
var gcsConfigPath =
162+
process.env.GRUNT_GOOGLE_APPLICATION_CREDENTIALS ||
163+
path.join(os.homedir(), '.gcs', 'js-sdk.json');
164+
var gcsConfig = grunt.file.exists(gcsConfigPath)
165+
? grunt.file.readJSON(gcsConfigPath)
166+
: {};
161167
var gruntConfig = {
162168
pkg: grunt.file.readJSON('package.json'),
163169
aws: grunt.file.exists(awsConfigPath) ? grunt.file.readJSON(awsConfigPath) : {},
@@ -230,6 +236,25 @@ module.exports = function(grunt) {
230236
]
231237
}
232238
},
239+
gcs: {
240+
options: {
241+
credentials: gcsConfig,
242+
project: process.env.GRUNT_GOOGLE_PROJECT_ID || gcsConfig.project_id,
243+
// Normally there's no info about bucket name in a GCS service account
244+
// file, but we can add it for convenience
245+
bucket: process.env.GRUNT_GOOGLE_BUCKET_NAME || gcsConfig.bucket_name,
246+
gzip: true,
247+
headers: {
248+
cacheControl: 'public, max-age=300' // FIXME: increase after testing
249+
},
250+
metadata: {}
251+
},
252+
dist: {
253+
cwd: 'build',
254+
src: '**/*',
255+
dest: '<%= pkg.release || "unreleased" %>/'
256+
}
257+
},
233258

234259
copy: {
235260
dist: {
@@ -309,6 +334,7 @@ module.exports = function(grunt) {
309334
grunt.loadNpmTasks('grunt-browserify');
310335
grunt.loadNpmTasks('grunt-release');
311336
grunt.loadNpmTasks('grunt-s3');
337+
grunt.loadNpmTasks('grunt-google-cloud');
312338
grunt.loadNpmTasks('grunt-gitinfo');
313339
grunt.loadNpmTasks('grunt-sri');
314340

@@ -329,6 +355,8 @@ module.exports = function(grunt) {
329355
);
330356
grunt.registerTask('dist', ['build', 'copy:dist', 'sri:dist', 'copy:distRoot']);
331357
grunt.registerTask('publish', ['build', 's3']);
358+
grunt.registerTask('publish-gcs', ['build', 'gcs']);
332359
grunt.registerTask('cdn', ['version', 's3']);
360+
grunt.registerTask('cdn-gcs', ['version', 'gcs']);
333361
grunt.registerTask('test:ci', ['config:ci', 'build.test']);
334362
};

packages/raven-js/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"grunt-contrib-copy": "^0.8.2",
4747
"grunt-contrib-uglify": "^0.11.0",
4848
"grunt-gitinfo": "^0.1.7",
49+
"grunt-google-cloud": "^1.0.7",
4950
"grunt-release": "^0.13.0",
5051
"grunt-s3": "0.2.0-alpha.3",
5152
"grunt-sri": "mattrobenolt/grunt-sri#pretty",

scripts/browser-upload-cdn.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
'use strict';
2+
3+
const Storage = require('@google-cloud/storage');
4+
const path = require('path');
5+
const os = require('os');
6+
const fs = require('fs');
7+
8+
const bundleFilesRegex = /^bundle.*\.js.*$/;
9+
const rootDir = path.dirname(__dirname);
10+
const browserDir = path.join(rootDir, 'packages', 'browser');
11+
const browserBuildDir = path.join(browserDir, 'build');
12+
13+
/** Return full paths of files to upload */
14+
function findFiles() {
15+
const bundleFiles = fs
16+
.readdirSync(browserBuildDir)
17+
.filter(filename => filename.match(bundleFilesRegex))
18+
.map(filename => path.join(browserBuildDir, filename));
19+
return bundleFiles;
20+
}
21+
22+
/** Upload sentry-browser bundles to a GCS bucket */
23+
async function uploadFiles() {
24+
const gcsConfigPath =
25+
process.env.BROWSER_GOOGLE_APPLICATION_CREDENTIALS ||
26+
path.join(os.homedir(), '.gcs', 'sentry-browser-sdk.json');
27+
console.log(`Reading GCS configuration from "${gcsConfigPath}"...`);
28+
29+
const gcsConfig = fs.existsSync(gcsConfigPath)
30+
? JSON.parse(fs.readFileSync(gcsConfigPath))
31+
: undefined;
32+
33+
if (!gcsConfig) {
34+
console.error(
35+
'Google Storage configuration (service account key) not found.\n' +
36+
`Place it at ${gcsConfigPath} or use the environment variable ` +
37+
'(BROWSER_GOOGLE_APPLICATION_CREDENTIALS) to specify the path.',
38+
);
39+
process.exit(1);
40+
}
41+
42+
const projectId =
43+
process.env.BROWSER_GOOGLE_PROJECT_ID || gcsConfig.project_id;
44+
if (!projectId) {
45+
console.error('Google project ID not found.');
46+
process.exit(1);
47+
}
48+
49+
const bucketName =
50+
process.env.BROWSER_GOOGLE_BUCKET_NAME || gcsConfig.bucket_name;
51+
if (!bucketName) {
52+
console.error('Bucket name not found in the configuration.');
53+
process.exit(1);
54+
}
55+
56+
const bundleFiles = findFiles();
57+
if (!bundleFiles.length) {
58+
console.error('Error: no files to upload!');
59+
process.exit(1);
60+
}
61+
62+
const browserPackageJson = path.join(browserDir, 'package.json');
63+
const version =
64+
JSON.parse(fs.readFileSync(browserPackageJson)).version || 'unreleased';
65+
66+
const storage = new Storage({
67+
projectId,
68+
});
69+
70+
const bucket = storage.bucket(bucketName);
71+
// TODO increase after some testing is done
72+
const cacheAge = 300;
73+
74+
await Promise.all(
75+
bundleFiles.map(async filepath => {
76+
const destination = path.join(version, path.basename(filepath));
77+
const options = {
78+
gzip: true,
79+
destination: destination,
80+
metadata: {
81+
cacheControl: `public, max-age=${cacheAge}`,
82+
},
83+
};
84+
await bucket.upload(filepath, options);
85+
console.log(`Uploaded "${destination}"`);
86+
}),
87+
);
88+
console.log('Upload complete.');
89+
}
90+
91+
uploadFiles().catch(error => {
92+
console.error('Error occurred:', error);
93+
process.exit(1);
94+
});

0 commit comments

Comments
 (0)