-
Notifications
You must be signed in to change notification settings - Fork 246
feat(web, ci): upload compass-web assets to an s3 bucket during publish COMPASS-10132 #7631
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
Changes from all commits
c62496e
d8288cb
c0ec910
3e30d7d
0b0bfbd
79788db
263691e
70bddfd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,6 +48,9 @@ variables: | |
| GITHUB_TOKEN: ${devtoolsbot_github_token} | ||
| DOWNLOAD_CENTER_AWS_ACCESS_KEY_ID: ${aws_key_evergreen_integrations} | ||
| DOWNLOAD_CENTER_AWS_SECRET_ACCESS_KEY: ${aws_secret_evergreen_integrations} | ||
| DOWNLOAD_CENTER_NEW_AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID} | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a bit of an overkill moving it here taking into account we will be using a different bucket, but this anyway seems more consistent with how we do it for other variables
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems OK. I have no real opinion. |
||
| DOWNLOAD_CENTER_NEW_AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY} | ||
| DOWNLOAD_CENTER_NEW_AWS_SESSION_TOKEN: ${AWS_SESSION_TOKEN} | ||
| EVERGREEN_BUCKET_NAME: mciuploads | ||
| EVERGREEN_BUCKET_KEY_PREFIX: ${project}/${revision}_${revision_order_id} | ||
| MONGODB_RUNNER_LOG_DIR: ${workdir}/src/.testserver/ | ||
|
|
@@ -504,23 +507,39 @@ functions: | |
| publish: | ||
| - command: ec2.assume_role | ||
| params: | ||
| role_arn: 'arn:aws:iam::119629040606:role/s3-access.cdn-origin-compass' | ||
| role_arn: ${downloads_bucket_role_arn} | ||
| - command: shell.exec | ||
| params: | ||
| working_dir: src | ||
| shell: bash | ||
| env: | ||
| <<: *compass-env | ||
| DOWNLOAD_CENTER_NEW_AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID} | ||
| DOWNLOAD_CENTER_NEW_AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY} | ||
| DOWNLOAD_CENTER_NEW_AWS_SESSION_TOKEN: ${AWS_SESSION_TOKEN} | ||
| script: | | ||
| set -e | ||
| # Load environment variables | ||
| eval $(.evergreen/print-compass-env.sh) | ||
| echo "Uploading release assets to S3 and GitHub if needed..." | ||
| npm run --workspace mongodb-compass upload | ||
|
|
||
| publish-web: | ||
| - command: ec2.assume_role | ||
| params: | ||
| role_arn: ${downloads_bucket_role_arn} | ||
| - command: shell.exec | ||
| params: | ||
| working_dir: src | ||
| shell: bash | ||
| env: | ||
| <<: *compass-env | ||
| script: | | ||
| set -e | ||
| # Load environment variables | ||
| eval $(.evergreen/print-compass-env.sh) | ||
| echo "Compiling compass-web" | ||
| npm run --workspace @mongodb-js/compass-web compile | ||
| echo "Uploading release assets to S3" | ||
| npm run --workspace @mongodb-js/compass-web upload | ||
|
|
||
| publish-dev-release-info: | ||
| - command: shell.exec | ||
| params: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -187,16 +187,18 @@ export const lessLoader = (args: ConfigArgs) => ({ | |
| }); | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| export const assetsLoader = (_args: ConfigArgs) => ({ | ||
| test: /\.(jpe?g|png|svg|gif|woff|woff2|ttf|eot|otf)(\?.+?)?$/, | ||
| // asset (or asset auto) will either compile as data-uri or to a file path | ||
| // based on the size, this is a good strategy for loading assets in the GUI | ||
| type: 'asset', | ||
| parser: { | ||
| dataUrlCondition: { | ||
| maxSize: 2 * 1024, // 2kb | ||
| }, | ||
| }, | ||
| export const fontLoader = (_args: ConfigArgs) => ({ | ||
| test: /\.(woff|woff2|ttf|eot|otf)(\?.+?)?$/, | ||
| // fonts are always big and should be emitted as a separate file | ||
| type: 'asset/resource', | ||
| }); | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| export const imageLoader = (_args: ConfigArgs) => ({ | ||
| test: /\.(jpe?g|png|svg|gif)(\?.+?)?$/, | ||
| // it's convenient to inline images as data-urls to make sure that publised | ||
| // library artifacts only produce importable javascript assets | ||
| type: 'asset/inline', | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just easier not to deal with them for compass-web which is published as a library, they are all relatively small (the biggest is like 5kb) so data-urls work well enough |
||
| }); | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
|
|
@@ -223,7 +225,7 @@ export const sourceLoader = (args: ConfigArgs) => ({ | |
| nodeLoader(args).test, | ||
| cssLoader(args).test, | ||
| lessLoader(args).test, | ||
| assetsLoader(args).test, | ||
| resourceLoader(args).test, | ||
| sharedObjectLoader(args).test, | ||
| // Produced by html-webpack-plugin and should not be handled | ||
| /\.(ejs|html)$/, | ||
|
|
||
gribnoysup marked this conversation as resolved.
Show resolved
Hide resolved
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is maybe an argument for this code to be a part of hadron-build upload script, but I didn't want to add to the code we already know we need to heavily refactor in the future. Keeping it closer to compass-web seems reasonable to me for now, we can move this all to one place later down the road |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import fs from 'fs'; | ||
| import path from 'path'; | ||
| import child_process from 'child_process'; | ||
| import { brotliCompressSync } from 'zlib'; | ||
| import { promisify } from 'util'; | ||
| import S3 from 'aws-sdk/clients/s3.js'; | ||
|
|
||
| // TODO(SRE-4971): replace with a compass-web-only bucket when provisioned | ||
| const DOWNLOADS_BUCKET = 'cdn-origin-compass'; | ||
|
|
||
| const DIST_DIR = path.resolve(import.meta.dirname, '..', 'dist'); | ||
|
|
||
| const HEAD = child_process | ||
| .spawnSync('git', ['rev-parse', 'HEAD'], { encoding: 'utf8' }) | ||
| .stdout.trim(); | ||
|
|
||
| function getCredentials() { | ||
| if ( | ||
| !process.env.DOWNLOAD_CENTER_NEW_AWS_ACCESS_KEY_ID || | ||
| !process.env.DOWNLOAD_CENTER_NEW_AWS_SECRET_ACCESS_KEY || | ||
| !process.env.DOWNLOAD_CENTER_NEW_AWS_SESSION_TOKEN | ||
| ) { | ||
| throw new Error('Missing required env variables'); | ||
| } | ||
| return { | ||
| accessKeyId: process.env.DOWNLOAD_CENTER_NEW_AWS_ACCESS_KEY_ID, | ||
| secretAccessKey: process.env.DOWNLOAD_CENTER_NEW_AWS_SECRET_ACCESS_KEY, | ||
| sessionToken: process.env.DOWNLOAD_CENTER_NEW_AWS_SESSION_TOKEN, | ||
| }; | ||
| } | ||
|
|
||
| const artifacts = await fs.promises.readdir(DIST_DIR); | ||
|
|
||
| if (!artifacts.length) { | ||
| throw new Error('No artifact files found'); | ||
| } | ||
|
|
||
| const contentTypeForExt: Record<string, string> = { | ||
| '.mjs': 'text/javascript', | ||
| '.txt': 'text/plain', // extracted third party license info | ||
| '.ts': 'text/typescript', // type definitions | ||
| '.json': 'application/json', // tsdoc meta | ||
| }; | ||
|
|
||
| const ALLOWED_EXTS = Object.keys(contentTypeForExt); | ||
|
|
||
| for (const file of artifacts) { | ||
| if (!ALLOWED_EXTS.includes(path.extname(file))) { | ||
| throw new Error(`Unexpected artifact file extension for ${file}`); | ||
| } | ||
| } | ||
|
|
||
| const s3Client = new S3({ | ||
| credentials: getCredentials(), | ||
| }); | ||
|
|
||
| const IMMUTABLE_CACHE_MAX_AGE = 1000 * 60 * 60 * 24 * 7; // a week | ||
|
|
||
| for (const file of artifacts) { | ||
| const filePath = path.join(DIST_DIR, file); | ||
| // TODO(SRE-4971): while we're uploading to the downloads bucket, the object | ||
| // key always needs to start with `compass/` | ||
| const objectKey = `compass/web/${HEAD}/${file}`; | ||
|
|
||
| console.log( | ||
| 'Uploading compass-web/dist/%s to s3://%s/%s ...', | ||
| file, | ||
| DOWNLOADS_BUCKET, | ||
| objectKey | ||
| ); | ||
|
|
||
| const fileContent = fs.readFileSync(filePath, 'utf8'); | ||
| const compressedFileContent = brotliCompressSync(fileContent); | ||
|
|
||
| const asyncPutObject: ( | ||
| params: S3.Types.PutObjectRequest | ||
| ) => Promise<S3.Types.PutObjectOutput> = promisify( | ||
| s3Client.putObject.bind(s3Client) | ||
| ); | ||
|
|
||
| const res = await asyncPutObject({ | ||
| ACL: 'private', | ||
| Bucket: DOWNLOADS_BUCKET, | ||
| Key: objectKey, | ||
| Body: compressedFileContent, | ||
| ContentType: contentTypeForExt[path.extname(file)], | ||
| ContentEncoding: 'br', | ||
| ContentLength: compressedFileContent.byteLength, | ||
| // Assets stored under the commit hash never change after upload, so the | ||
| // cache-control setting for them can be quite generous | ||
| CacheControl: `public, max-age=${IMMUTABLE_CACHE_MAX_AGE}, immutable`, | ||
| }); | ||
|
|
||
| console.log('Successfully uploaded %s (ETag: %s)', file, res.ETag); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drive-by fix: at some point we updated the template (the .in file), but not the actual config