-
-
Notifications
You must be signed in to change notification settings - Fork 11
actions: promote release action #142
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
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
name: Promote Release | ||
|
||
on: | ||
# Triggered by the dist server | ||
workflow_dispatch: | ||
inputs: | ||
path: | ||
description: 'path to promote' | ||
required: true | ||
recursive: | ||
description: 'is the path a directory' | ||
type: boolean | ||
required: true | ||
|
||
jobs: | ||
promote-release: | ||
name: Promote Release | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Git Checkout | ||
uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 | ||
|
||
- name: Setup Node | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: lts/* | ||
cache: 'npm' | ||
|
||
- name: Promote Files | ||
env: | ||
CF_ACCESS_KEY_ID: ${{ secrets.CF_ACCESS_KEY_ID }} | ||
CF_SECRET_ACCESS_KEY: ${{ secrets.CF_SECRET_ACCESS_KEY }} | ||
run: | | ||
node scripts/promote-release.mjs ${{ inputs.path }} ${{ inputs.recursive == true && '--recursive' || '' }} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
export const ENDPOINT = | ||
process.env.ENDPOINT ?? | ||
'https://07be8d2fbc940503ca1be344714cb0d1.r2.cloudflarestorage.com'; | ||
|
||
export const PROD_BUCKET = process.env.PROD_BUCKET ?? 'dist-prod'; | ||
|
||
export const STAGING_BUCKET = process.env.STAGING_BUCKET ?? 'dist-staging'; | ||
|
||
export const R2_RETRY_COUNT = 3; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Usage: `promote-release <prefix in dist-staging to promote> [--recursive]` | ||
* ex/ `promote-release nodejs/release/v20.0.0/ --recursive` | ||
*/ | ||
|
||
import { | ||
S3Client, | ||
ListObjectsV2Command, | ||
CopyObjectCommand, | ||
} from '@aws-sdk/client-s3'; | ||
import { | ||
ENDPOINT, | ||
PROD_BUCKET, | ||
STAGING_BUCKET, | ||
R2_RETRY_COUNT, | ||
} from './constants.mjs'; | ||
|
||
if (process.argv.length !== 3 && process.argv.length !== 4) { | ||
console.error( | ||
`usage: promote-release <prefix in dist-staging to promote> [--recursive]` | ||
); | ||
process.exit(1); | ||
} | ||
|
||
if (!process.env.CF_ACCESS_KEY_ID) { | ||
console.error('CF_ACCESS_KEY_ID missing'); | ||
process.exit(1); | ||
} | ||
|
||
if (!process.env.CF_SECRET_ACCESS_KEY) { | ||
console.error('CF_SECRET_ACCESS_KEY missing'); | ||
process.exit(1); | ||
} | ||
|
||
const client = new S3Client({ | ||
endpoint: ENDPOINT, | ||
region: 'auto', | ||
credentials: { | ||
accessKeyId: process.env.CF_ACCESS_KEY_ID, | ||
secretAccessKey: process.env.CF_SECRET_ACCESS_KEY, | ||
}, | ||
}); | ||
|
||
const path = process.argv[2]; | ||
const recursive = | ||
process.argv.length === 4 && process.argv[3] === '--recursive'; | ||
|
||
if (recursive) { | ||
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. Instead of if else, when not recursive make an array of one file :) |
||
const files = await getFilesToPromote(path); | ||
|
||
for (const file of files) { | ||
await promoteFile(file); | ||
} | ||
} else { | ||
await promoteFile(file); | ||
} | ||
|
||
/** | ||
* @param {string} path | ||
* @returns {string[]} | ||
*/ | ||
async function getFilesToPromote(path) { | ||
let paths = []; | ||
|
||
let truncated = true; | ||
let continuationToken; | ||
while (truncated) { | ||
const data = await retryWrapper(async () => { | ||
return await client.send( | ||
new ListObjectsV2Command({ | ||
Bucket: STAGING_BUCKET, | ||
Delimiter: '/', | ||
Prefix: path, | ||
ContinuationToken: continuationToken, | ||
}) | ||
); | ||
}); | ||
|
||
if (data.CommonPrefixes) { | ||
for (const directory of data.CommonPrefixes) { | ||
paths.push(...(await getFilesToPromote(directory.Prefix))); | ||
} | ||
} | ||
|
||
if (data.Contents) { | ||
for (const object of data.Contents) { | ||
paths.push(object.Key); | ||
} | ||
} | ||
|
||
truncated = data.IsTruncated ?? false; | ||
continuationToken = data.NextContinuationToken; | ||
} | ||
|
||
return paths; | ||
} | ||
|
||
/** | ||
* @param {string} file | ||
*/ | ||
async function promoteFile(file) { | ||
console.log(`Promoting ${file}`); | ||
|
||
await retryWrapper(async () => { | ||
return await client.send( | ||
new CopyObjectCommand({ | ||
Bucket: PROD_BUCKET, | ||
CopySource: `${STAGING_BUCKET}/${file}`, | ||
Key: file, | ||
}) | ||
); | ||
}, R2_RETRY_COUNT); | ||
} | ||
|
||
/** | ||
* @param {() => Promise<T>} request | ||
* @returns {Promise<T>} | ||
*/ | ||
async function retryWrapper(request, retryLimit) { | ||
let r2Error; | ||
|
||
for (let i = 0; i < R2_RETRY_COUNT; i++) { | ||
try { | ||
const result = await request(); | ||
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. In general await with a return is not needed? As an |
||
return result; | ||
} catch (err) { | ||
r2Error = err; | ||
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. Shouldn't the throw happen here already? |
||
process.emitWarning(`error when contacting r2: ${err}`); | ||
} | ||
} | ||
|
||
throw r2Error; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.