Skip to content

Commit

Permalink
setup node
Browse files Browse the repository at this point in the history
Signed-off-by: flakey5 <[email protected]>
  • Loading branch information
flakey5 committed Oct 5, 2024
1 parent 098350a commit 5d94b1c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 38 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/promote.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ jobs:
runs-on: ubuntu-latest

steps:
- 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 }}
Expand Down
80 changes: 42 additions & 38 deletions scripts/promote-release.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/usr/bin/env node

import { S3Client, ListObjectsV2Command, CopyObjectCommand } from '@aws-sdk/client-s3';
import {
S3Client,
ListObjectsV2Command,
CopyObjectCommand,
} from '@aws-sdk/client-s3';

if (process.argv.length !== 3) {
console.error(`usage: promote-release <prefix in dist-staging to promote>`);
Expand All @@ -19,86 +23,86 @@ if (!process.env.CF_SECRET_ACCESS_KEY) {

const ENDPOINT =
'https://07be8d2fbc940503ca1be344714cb0d1.r2.cloudflarestorage.com';
const PROD_BUCKET = 'dist-prod'
const STAGING_BUCKET = 'dist-staging'
const PROD_BUCKET = 'dist-prod';
const STAGING_BUCKET = 'dist-staging';
const RETRY_LIMIT = 3;

const client = new S3Client({
endpoint: ENDPOINT,
region: 'auto',
credentials: {
accessKeyId: process.env.CF_ACCESS_KEY_ID,
secretAccessKey: process.env.CF_SECRET_ACCESS_KEY
}
})
secretAccessKey: process.env.CF_SECRET_ACCESS_KEY,
},
});

const path = process.argv[2]
const path = process.argv[2];
const files = await getFilesToPromote(path);

for (const file of files) {
promoteFile(file)
promoteFile(file);
}

/**
* @param {string} path
* @returns {string[]}
* @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
})
)
}
)
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))
paths.push(...(await getFilesToPromote(directory.Prefix)));
}
}

if (data.Contents) {
for (const object of data.Contents) {
paths.push(object.Key)
paths.push(object.Key);
}
}

truncated = data.IsTruncated ?? false
continuationToken = data.NextContinuationToken
truncated = data.IsTruncated ?? false;
continuationToken = data.NextContinuationToken;
}

return paths
return paths;
}

/**
* @param {string} file
* @param {string} file
*/
async function promoteFile(file) {
console.log(`Promoting ${file}`)

await client.send(new CopyObjectCommand({
Bucket: PROD_BUCKET,
CopySource: `${STAGING_BUCKET}/${file}`,
Key: file
}))
console.log(`Promoting ${file}`);

await client.send(
new CopyObjectCommand({
Bucket: PROD_BUCKET,
CopySource: `${STAGING_BUCKET}/${file}`,
Key: file,
})
);
}

/**
* @param {() => Promise<T>} request
* @param {() => Promise<T>} request
* @returns {Promise<T>}
*/
async function retryWrapper (request, retryLimit) {
async function retryWrapper(request, retryLimit) {
let r2Error;

for (let i = 0; i < RETRY_LIMIT; i++) {
Expand All @@ -107,9 +111,9 @@ async function retryWrapper (request, retryLimit) {
return result;
} catch (err) {
r2Error = err;
process.emitWarning(`error when contacting r2: ${err}`)
process.emitWarning(`error when contacting r2: ${err}`);
}
}

throw r2Error
throw r2Error;
}

0 comments on commit 5d94b1c

Please sign in to comment.