Skip to content

Commit

Permalink
Merge pull request #645 from adobecom/MWPW-145723
Browse files Browse the repository at this point in the history
MWPW-145723 Automate EdgeWorker bundle version increment
  • Loading branch information
Blainegunn authored May 7, 2024
2 parents a7cdba3 + abb9750 commit 545145b
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 6 deletions.
16 changes: 12 additions & 4 deletions .github/workflows/deploy-edgeworker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ on:
required: true
default: 'Acrobat_DC_web_stg'
options:
- 'Acrobat_DC_web_stg'
- 'Acrobat_DC_web_prod'
- 'Acrobat_DC_web_stg'
- 'Acrobat_DC_web_prod'
network:
description: 'Network'
type: choice
Expand All @@ -24,6 +24,11 @@ on:
options:
- 'staging'
- 'production'
description:
description: 'Bundle Description'
type: string
required: true
default: 'Bundle Description'

jobs:
deploy-edgeworker:
Expand All @@ -38,8 +43,11 @@ jobs:
- name: Build Edge Worker Scripts
run: |
npm run ewbuild
npm run ewprod2stg
npm run ewprod2stg
npm run ewsetbundle ${{ inputs.edgeworker }} "${{ inputs.description }}"
env:
EDGERC: ${{ secrets.AKAMAI_EDGERC }}

- name: Deploy Edge Worker
uses: jdmevo123/[email protected]
env:
Expand Down
89 changes: 89 additions & 0 deletions edgeworkers/scripts/setbundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Environment variables:
* - EDGERC: The content of the .edgerc file
* Command Arguments:
* - edgeworker name: The name of the EdgeWorker
* example: 'Acrobat_DC_web_prod' or 'Acrobat_DC_web_stg'
* - description: The description of the bundle
*/
const fs = require('fs');
const os = require('os');
const EdgeGrid = require('akamai-edgegrid');
const { compareVersions } = require('compare-versions');

const egSend = (eg, path, method, body) => {
eg.auth({ path, method, body });

return new Promise((resolve, reject) => {
eg.send(function (error, response, body) {
if (error) {
reject(error);
} else {
resolve(JSON.parse(body));
}
});
});
};


async function main() {
const edgeworkerName = process.argv.length > 2 ? process.argv[2] : null;
const description = process.argv.length > 3 ? process.argv[3] : null;

if (!edgeworkerName) {
console.error('Please provide the name of the EdgeWorker');
process.exit(1);
}

if (process.env.EDGERC) {
fs.writeFileSync('.edgerc', process.env.EDGERC, 'utf8');
}

const edgercPath = fs.existsSync('.edgerc') ? '.edgerc' : `${os.homedir()}/.edgerc`;

const eg = new EdgeGrid({ path: edgercPath, section: 'edgeworkers' });

let body = await egSend(eg, '/edgeworkers/v1/ids', 'GET');
const edgeWorkers = body.edgeWorkerIds;
const edgeWorker = edgeWorkers.find((ew) => ew.name === edgeworkerName);

if (!edgeWorker) {
console.error(`EdgeWorker ${edgeworkerName} not found`);
process.exit(1);
}

body = await egSend(
eg,
`/edgeworkers/v1/ids/${edgeWorker.edgeWorkerId}/versions`,
'GET'
);
const versions = body.versions.map((x) => x.version).sort(compareVersions);
const latestVersion = versions[versions.length - 1];

console.log(`The latest version of ${edgeworkerName} is ${latestVersion}`);

const bundleJsonPath = `edgeworkers/${edgeworkerName}/bundle.json`;

if (!fs.existsSync(bundleJsonPath)) {
console.error(`Bundle file ${bundleJsonPath} not found`);
process.exit(1);
}

let bundleJson = JSON.parse(fs.readFileSync(bundleJsonPath, 'utf8'));

let versionSegs = latestVersion.split('.').map(x => parseInt(x));
versionSegs[versionSegs.length - 1] += 1;

bundleJson['edgeworker-version'] = versionSegs.join('.');

if (description) {
bundleJson.description = description;
}

console.log(`New bundle info: ${JSON.stringify(bundleJson, null, 2)}`);

fs.writeFileSync(bundleJsonPath, JSON.stringify(bundleJson, null, 2), 'utf8');
}

main();
127 changes: 127 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"update-csp": "node ./tools/cspUpdate/cspUpdate.js",
"ewtest": "jest --testPathPattern=edgeworkers",
"ewprod2stg": "node ./edgeworkers/scripts/prod2stg.js",
"ewbuild": "node ./edgeworkers/scripts/build.js"
"ewbuild": "node ./edgeworkers/scripts/build.js",
"ewsetbundle": "node ./edgeworkers/scripts/setbundle.js"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -54,7 +55,9 @@
"sinon": "13.0.1",
"stylelint": "14.6.0",
"stylelint-config-prettier": "9.0.3",
"stylelint-config-standard": "25.0.0"
"stylelint-config-standard": "25.0.0",
"akamai-edgegrid": "^3.4.5",
"compare-versions": "^6.1.0"
},
"dependencies": {
"@web/test-runner": "0.18.1",
Expand Down

0 comments on commit 545145b

Please sign in to comment.