-
Notifications
You must be signed in to change notification settings - Fork 246
feat(web, ci): add publish script; automatically publish for dev on every merge to main COMPASS-10156 #7637
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c39f33b
chore(web): move upload script to release dir; move some reusable cod…
gribnoysup 985ef21
feat(web, ci): add publish script; add publish-web step to publish CI…
gribnoysup d9004f0
fix(web): max-age is seconds, not millis
gribnoysup 91c0133
chore(web): update object key resolution to invalidate latest cache
gribnoysup 51a46ac
chore(web): add more logging to publish script
gribnoysup afb0408
chore(ci): only run publish on merges to main
gribnoysup e346f02
Merge branch 'main' into COMPASS-10156
gribnoysup File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import path from 'path'; | ||
| import { brotliCompressSync } from 'zlib'; | ||
| import { inspect } from 'util'; | ||
| import { | ||
| ALLOWED_PUBLISH_ENVIRONMENTS, | ||
| DOWNLOADS_BUCKET, | ||
| DOWNLOADS_BUCKET_PUBLIC_HOST, | ||
| ENTRYPOINT_FILENAME, | ||
| MANIFEST_FILENAME, | ||
| PUBLISH_ENVIRONMENT, | ||
| RELEASE_COMMIT, | ||
| asyncPutObject, | ||
| getObjectKey, | ||
| } from './utils.mts'; | ||
|
|
||
| console.log( | ||
| 'Publishing compass-web@%s to %s environment', | ||
| RELEASE_COMMIT, | ||
| PUBLISH_ENVIRONMENT | ||
| ); | ||
|
|
||
| if (!ALLOWED_PUBLISH_ENVIRONMENTS.includes(PUBLISH_ENVIRONMENT ?? '')) { | ||
| throw new Error( | ||
| `Unknown publish environment: expected ${inspect( | ||
| ALLOWED_PUBLISH_ENVIRONMENTS | ||
| )}, got ${inspect(PUBLISH_ENVIRONMENT)}` | ||
| ); | ||
| } | ||
|
|
||
| const publicManifestUrl = new URL( | ||
| getObjectKey(MANIFEST_FILENAME), | ||
| DOWNLOADS_BUCKET_PUBLIC_HOST | ||
| ); | ||
|
|
||
| const publicEntrypointUrl = new URL( | ||
| getObjectKey(ENTRYPOINT_FILENAME), | ||
| DOWNLOADS_BUCKET_PUBLIC_HOST | ||
| ); | ||
|
|
||
| let assets: URL[]; | ||
|
|
||
| function assertResponseIsOk(res: Response) { | ||
| if (res.status !== 200) { | ||
| throw new Error( | ||
| `Request returned a non-OK response: ${res.status} ${res.statusText}` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| console.log('Fetching manifest from %s', publicManifestUrl); | ||
|
|
||
| try { | ||
| const res = await fetch(publicManifestUrl); | ||
| assertResponseIsOk(res); | ||
| const manifest = await res.json(); | ||
|
|
||
| if ( | ||
| !( | ||
| Array.isArray(manifest) && | ||
| manifest.every((asset) => { | ||
| return typeof asset === 'string'; | ||
| }) | ||
| ) | ||
| ) { | ||
| throw new Error( | ||
| `Manifest schema is not matching: expected string[], got ${inspect( | ||
| manifest | ||
| )}` | ||
| ); | ||
| } | ||
|
|
||
| console.log('Checking that assets in manifest exist'); | ||
|
|
||
| assets = manifest.map((asset) => { | ||
| return new URL(getObjectKey(asset), DOWNLOADS_BUCKET_PUBLIC_HOST); | ||
| }); | ||
|
|
||
| await Promise.all( | ||
| assets.map(async (assetUrl) => { | ||
| const res = await fetch(assetUrl, { method: 'HEAD' }); | ||
| assertResponseIsOk(res); | ||
| }) | ||
| ); | ||
| } catch (err) { | ||
| throw new AggregateError( | ||
| [err], | ||
| `Aborting publish, failed to resolve manifest ${publicManifestUrl}` | ||
| ); | ||
| } | ||
|
|
||
| function buildProxyEntrypointFile() { | ||
| return ( | ||
| assets | ||
| .map((asset) => { | ||
| return `import ${JSON.stringify(asset)};`; | ||
| }) | ||
| .concat( | ||
| `export * from ${JSON.stringify(publicEntrypointUrl)};`, | ||
| `/** Compass version: https://github.com/mongodb-js/compass/tree/${RELEASE_COMMIT} */` | ||
| ) | ||
| .join('\n') + '\n' | ||
| ); | ||
| } | ||
|
|
||
| const fileKey = getObjectKey('index.mjs', PUBLISH_ENVIRONMENT); | ||
| const fileContent = buildProxyEntrypointFile(); | ||
| const compressedFileContent = brotliCompressSync(fileContent); | ||
|
|
||
| console.log( | ||
| 'Uploading entrypoint to s3://%s/%s ...', | ||
| DOWNLOADS_BUCKET, | ||
| fileKey | ||
| ); | ||
|
|
||
| const ENTRYPOINT_CACHE_MAX_AGE_SECONDS = 1 * 60 * 3; // 3mins | ||
|
|
||
| const res = await asyncPutObject({ | ||
| ACL: 'private', | ||
| Bucket: DOWNLOADS_BUCKET, | ||
| Key: fileKey, | ||
| Body: compressedFileContent, | ||
| ContentType: 'text/javascript', | ||
| ContentEncoding: 'br', | ||
| ContentLength: compressedFileContent.byteLength, | ||
| // "Latest" entrypoint files can change quite often, so max-age is quite | ||
| // short and browser should always revalidate on stale | ||
| CacheControl: `public, max-age=${ENTRYPOINT_CACHE_MAX_AGE_SECONDS}, must-revalidate`, | ||
| }); | ||
|
|
||
| console.log( | ||
| 'Successfully uploaded %s (ETag: %s)', | ||
| path.basename(fileKey), | ||
| res.ETag | ||
| ); |
|
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. Logic here is the same as before, but I moved some things around and git thinks it's a completely new file now 😓 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import fs from 'fs'; | ||
| import path from 'path'; | ||
| import { brotliCompressSync } from 'zlib'; | ||
| import { | ||
| DIST_DIR, | ||
| DOWNLOADS_BUCKET, | ||
| asyncPutObject, | ||
| getObjectKey, | ||
| } from './utils.mts'; | ||
|
|
||
| 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 / assets 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 IMMUTABLE_CACHE_MAX_AGE_SECONDS = 1 * 60 * 60 * 24 * 7; // a week | ||
|
|
||
| for (const file of artifacts) { | ||
| const filePath = path.join(DIST_DIR, file); | ||
| const objectKey = getObjectKey(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 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_SECONDS}, immutable`, | ||
| }); | ||
|
|
||
| console.log('Successfully uploaded %s (ETag: %s)', file, res.ETag); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import S3 from 'aws-sdk/clients/s3.js'; | ||
| import child_process from 'child_process'; | ||
| import path from 'path'; | ||
| import { promisify } from 'util'; | ||
|
|
||
| // TODO(SRE-4971): replace with a compass-web-only bucket when provisioned | ||
| export const DOWNLOADS_BUCKET = 'cdn-origin-compass'; | ||
|
|
||
| export const DOWNLOADS_BUCKET_PUBLIC_HOST = 'https://downloads.mongodb.com'; | ||
|
|
||
| export const ENTRYPOINT_FILENAME = 'compass-web.mjs'; | ||
|
|
||
| export const MANIFEST_FILENAME = 'assets-manifest.json'; | ||
|
|
||
| export const DIST_DIR = path.resolve(import.meta.dirname, '..', '..', 'dist'); | ||
|
|
||
| export const ALLOWED_PUBLISH_ENVIRONMENTS = ['dev', 'qa', 'staging', 'prod']; | ||
|
|
||
| export const PUBLISH_ENVIRONMENT = process.env.COMPASS_WEB_PUBLISH_ENVIRONMENT; | ||
|
|
||
| export const RELEASE_COMMIT = | ||
| process.env.COMPASS_WEB_RELEASE_COMMIT || | ||
| child_process | ||
| .spawnSync('git', ['rev-parse', 'HEAD'], { encoding: 'utf8' }) | ||
| .stdout.trim(); | ||
|
|
||
| function getAWSCredentials() { | ||
| 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 s3Client = new S3({ | ||
| credentials: getAWSCredentials(), | ||
| }); | ||
|
|
||
| export const asyncPutObject: ( | ||
| params: S3.Types.PutObjectRequest | ||
| ) => Promise<S3.Types.PutObjectOutput> = promisify( | ||
| s3Client.putObject.bind(s3Client) | ||
| ); | ||
|
|
||
| export function getObjectKey(filename: string, release = RELEASE_COMMIT) { | ||
| // TODO(SRE-4971): while we're uploading to the downloads bucket, the object | ||
| // key always needs to start with `compass/` | ||
| return `compass/compass-web/${release}/${filename}`; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Using parameters for these values so that later down the road we will be able to use those when spawning patches for releases with evergreen cli