From 778a9709f7518405841c1fdf4d5b9da1050044ff Mon Sep 17 00:00:00 2001 From: Daniel Van Der Ploeg Date: Fri, 15 Nov 2024 11:02:39 +1030 Subject: [PATCH] chore: update release pipeline --- .github/workflows/release.yml | 66 +++++++++---------- packages/basic-auth/project.json | 4 ++ .../cloudfront-security-headers/project.json | 4 ++ packages/esbuild/project.json | 4 ++ packages/feature-env-handlers/project.json | 4 ++ packages/geoip-redirect/project.json | 4 ++ packages/graphql-mesh-server/project.json | 4 ++ packages/prerender-fargate/project.json | 4 ++ packages/prerender-proxy/project.json | 4 ++ packages/rabbitmq/project.json | 4 ++ packages/shared-vpc/project.json | 4 ++ packages/static-hosting/project.json | 4 ++ packages/waf/project.json | 4 ++ tools/scripts/publish.mjs | 60 +++++++++++++++++ 14 files changed, 140 insertions(+), 34 deletions(-) create mode 100644 tools/scripts/publish.mjs diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 90380102..df8d2418 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -2,43 +2,41 @@ name: Publish to NPM on: release: - tags: - - '**-[0-9]+.[0-9]+.[0-9]+' - - '**-[0-9]+.[0-9]+.[0-9]+-*' + tags: + - "**-[0-9]+.[0-9]+.[0-9]+" + - "**-[0-9]+.[0-9]+.[0-9]+-*" types: [published] jobs: build-and-publish: + name: 🚀 Publish to NPM runs-on: ubuntu-latest steps: - # Source checkout - - uses: actions/checkout@v2 - with: - ref: ${{ github.event.release.target_commitish }} - # Setting Node environment - - name: Setting up Node environment - uses: actions/setup-node@v1 - with: - node-version: 16 - registry-url: https://registry.npmjs.org/ - # Environment configuration - - name: Preparing environment for release - run: | - VERSION=$(echo $GITHUB_REF_NAME | sed 's/^.*[A-Za-z]-//g') - echo "VERSION=$VERSION" >> $GITHUB_ENV - echo "PACKAGE=$(echo $GITHUB_REF_NAME | sed "s/-${VERSION}//g")" >> $GITHUB_ENV - # Release process - - name: Release - run: | - npm ci - git config --global user.name "Automated NPM Release" - git config --global user.email "sysadmin+npm-deploy@aligent.com.au" - npm version $VERSION --allow-same-version --workspace "@aligent/cdk-${{env.PACKAGE}}" - npm run build --workspace "@aligent/cdk-${{env.PACKAGE}}" - npm test --workspace "@aligent/cdk-${{env.PACKAGE}}" --if-present - npm publish --access public --workspace "@aligent/cdk-${{env.PACKAGE}}" - env: - NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} - - run: git push - env: - github-token: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file + - uses: actions/checkout@v4 + with: + ref: ${{ github.event.release.target_commitish }} + + - uses: actions/setup-node@v4 + with: + node-version-file: ".nvmrc" + cache: "yarn" + registry-url: https://registry.npmjs.org/ + + - name: Install + run: yarn install + + - name: Preparing environment for release + run: | + VERSION=$(echo $GITHUB_REF_NAME | sed 's/^.*[A-Za-z]-//g') + echo "VERSION=$VERSION" >> $GITHUB_ENV + echo "PACKAGE=$(echo $GITHUB_REF_NAME | sed "s/-${VERSION}//g")" >> $GITHUB_ENV + + - name: Release + run: | + git config --global user.name "Automated NPM Release" + git config --global user.email "devops+npm-deploy@aligent.com.au" + echo "Publishing $PACKAGE @ $VERSION" + yarn config set registry https://registry.npmjs.org/ + yarn nx publish $PACKAGE --ver=$VERSION --tag=latest --verbose + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/packages/basic-auth/project.json b/packages/basic-auth/project.json index c20a3cc2..4bc57253 100644 --- a/packages/basic-auth/project.json +++ b/packages/basic-auth/project.json @@ -23,6 +23,10 @@ "jestConfig": "packages/basic-auth/jest.config.ts", "passWithNoTests": true } + }, + "publish": { + "command": "node tools/scripts/publish.mjs basic-auth {args.ver} {args.tag}", + "dependsOn": ["build"] } }, "tags": [] diff --git a/packages/cloudfront-security-headers/project.json b/packages/cloudfront-security-headers/project.json index 4b60985c..3dc6d82c 100644 --- a/packages/cloudfront-security-headers/project.json +++ b/packages/cloudfront-security-headers/project.json @@ -23,6 +23,10 @@ "jestConfig": "packages/cloudfront-security-headers/jest.config.ts", "passWithNoTests": true } + }, + "publish": { + "command": "node tools/scripts/publish.mjs cloudfront-security-headers {args.ver} {args.tag}", + "dependsOn": ["build"] } }, "tags": [] diff --git a/packages/esbuild/project.json b/packages/esbuild/project.json index e36d592d..c01f9adc 100644 --- a/packages/esbuild/project.json +++ b/packages/esbuild/project.json @@ -23,6 +23,10 @@ "jestConfig": "packages/esbuild/jest.config.ts", "passWithNoTests": true } + }, + "publish": { + "command": "node tools/scripts/publish.mjs esbuild {args.ver} {args.tag}", + "dependsOn": ["build"] } }, "tags": [] diff --git a/packages/feature-env-handlers/project.json b/packages/feature-env-handlers/project.json index 6341d3fa..f6cccc67 100644 --- a/packages/feature-env-handlers/project.json +++ b/packages/feature-env-handlers/project.json @@ -23,6 +23,10 @@ "jestConfig": "packages/feature-env-handlers/jest.config.ts", "passWithNoTests": true } + }, + "publish": { + "command": "node tools/scripts/publish.mjs feature-env-handlers {args.ver} {args.tag}", + "dependsOn": ["build"] } }, "tags": [] diff --git a/packages/geoip-redirect/project.json b/packages/geoip-redirect/project.json index 28b5cee1..ffe1d882 100644 --- a/packages/geoip-redirect/project.json +++ b/packages/geoip-redirect/project.json @@ -23,6 +23,10 @@ "jestConfig": "packages/geoip-redirect/jest.config.ts", "passWithNoTests": true } + }, + "publish": { + "command": "node tools/scripts/publish.mjs geoip-redirect {args.ver} {args.tag}", + "dependsOn": ["build"] } }, "tags": [] diff --git a/packages/graphql-mesh-server/project.json b/packages/graphql-mesh-server/project.json index e8d2ec67..6a66a28a 100644 --- a/packages/graphql-mesh-server/project.json +++ b/packages/graphql-mesh-server/project.json @@ -23,6 +23,10 @@ "jestConfig": "packages/graphql-mesh-server/jest.config.ts", "passWithNoTests": true } + }, + "publish": { + "command": "node tools/scripts/publish.mjs graphql-mesh-server {args.ver} {args.tag}", + "dependsOn": ["build"] } }, "tags": [] diff --git a/packages/prerender-fargate/project.json b/packages/prerender-fargate/project.json index db6436e1..62be1c77 100644 --- a/packages/prerender-fargate/project.json +++ b/packages/prerender-fargate/project.json @@ -23,6 +23,10 @@ "jestConfig": "packages/prerender-fargate/jest.config.ts", "passWithNoTests": true } + }, + "publish": { + "command": "node tools/scripts/publish.mjs prerender-fargate {args.ver} {args.tag}", + "dependsOn": ["build"] } }, "tags": [] diff --git a/packages/prerender-proxy/project.json b/packages/prerender-proxy/project.json index 81e11b97..ee2250b3 100644 --- a/packages/prerender-proxy/project.json +++ b/packages/prerender-proxy/project.json @@ -23,6 +23,10 @@ "jestConfig": "packages/prerender-proxy/jest.config.ts", "passWithNoTests": true } + }, + "publish": { + "command": "node tools/scripts/publish.mjs prerender-proxy {args.ver} {args.tag}", + "dependsOn": ["build"] } }, "tags": [] diff --git a/packages/rabbitmq/project.json b/packages/rabbitmq/project.json index fb1053ee..24079ab9 100644 --- a/packages/rabbitmq/project.json +++ b/packages/rabbitmq/project.json @@ -23,6 +23,10 @@ "jestConfig": "packages/rabbitmq/jest.config.ts", "passWithNoTests": true } + }, + "publish": { + "command": "node tools/scripts/publish.mjs rabbitmq {args.ver} {args.tag}", + "dependsOn": ["build"] } }, "tags": [] diff --git a/packages/shared-vpc/project.json b/packages/shared-vpc/project.json index a66d4c2e..68677b75 100644 --- a/packages/shared-vpc/project.json +++ b/packages/shared-vpc/project.json @@ -23,6 +23,10 @@ "jestConfig": "packages/shared-vpc/jest.config.ts", "passWithNoTests": true } + }, + "publish": { + "command": "node tools/scripts/publish.mjs shared-vpc {args.ver} {args.tag}", + "dependsOn": ["build"] } }, "tags": [] diff --git a/packages/static-hosting/project.json b/packages/static-hosting/project.json index c5db187a..e2295d52 100644 --- a/packages/static-hosting/project.json +++ b/packages/static-hosting/project.json @@ -23,6 +23,10 @@ "jestConfig": "packages/static-hosting/jest.config.ts", "passWithNoTests": true } + }, + "publish": { + "command": "node tools/scripts/publish.mjs static-hosting {args.ver} {args.tag}", + "dependsOn": ["build"] } }, "tags": [] diff --git a/packages/waf/project.json b/packages/waf/project.json index d92d2255..4ba6c7d3 100644 --- a/packages/waf/project.json +++ b/packages/waf/project.json @@ -23,6 +23,10 @@ "jestConfig": "packages/waf/jest.config.ts", "passWithNoTests": true } + }, + "publish": { + "command": "node tools/scripts/publish.mjs waf {args.ver} {args.tag}", + "dependsOn": ["build"] } }, "tags": [] diff --git a/tools/scripts/publish.mjs b/tools/scripts/publish.mjs new file mode 100644 index 00000000..893ea35a --- /dev/null +++ b/tools/scripts/publish.mjs @@ -0,0 +1,60 @@ +/** + * This is a minimal script to publish your package to "npm". + * This is meant to be used as-is or customize as you see fit. + * + * This script is executed on "dist/path/to/library" as "cwd" by default. + * + * You might need to authenticate with NPM before running this script. + */ + +import { execSync } from 'child_process'; +import { readFileSync, writeFileSync } from 'fs'; + +import devkit from '@nx/devkit'; +const { readCachedProjectGraph } = devkit; + +function invariant(condition, message) { + if (!condition) { + console.error(message); + process.exit(1); + } +} + +// Executing publish script: node path/to/publish.mjs {name} --version {version} --tag {tag} +// Default "tag" to "next" so we won't publish the "latest" tag by accident. +const [, , name, version, tag = 'next'] = process.argv; + +// A simple SemVer validation to validate the version +const validVersion = /^\d+\.\d+\.\d+(-\w+\.\d+)?/; +invariant( + version && validVersion.test(version), + `No version provided or version did not match Semantic Versioning, expected: #.#.#-tag.# or #.#.#, got ${version}.` +); + +const graph = readCachedProjectGraph(); +const project = graph.nodes[name]; + +invariant( + project, + `Could not find project "${name}" in the workspace. Is the project.json configured correctly?` +); + +const outputPath = project.data?.targets?.build?.options?.outputPath; +invariant( + outputPath, + `Could not find "build.options.outputPath" of project "${name}". Is project.json configured correctly?` +); + +process.chdir(outputPath); + +// Updating the version in "package.json" before publishing +try { + const json = JSON.parse(readFileSync(`package.json`).toString()); + json.version = version; + writeFileSync(`package.json`, JSON.stringify(json, null, 2)); +} catch (e) { + console.error(`Error reading package.json file from library build output.`); +} + +// Execute "npm publish" to publish +execSync(`npm publish --verbose --access=public --tag ${tag}`);