Skip to content

Commit

Permalink
chore: update release pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
TheOrangePuff committed Nov 15, 2024
1 parent 59d52fe commit 778a970
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 34 deletions.
66 changes: 32 additions & 34 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 "[email protected]"
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 }}
- 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 "[email protected]"
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 }}
4 changes: 4 additions & 0 deletions packages/basic-auth/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
Expand Down
4 changes: 4 additions & 0 deletions packages/cloudfront-security-headers/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
Expand Down
4 changes: 4 additions & 0 deletions packages/esbuild/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
Expand Down
4 changes: 4 additions & 0 deletions packages/feature-env-handlers/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
Expand Down
4 changes: 4 additions & 0 deletions packages/geoip-redirect/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
Expand Down
4 changes: 4 additions & 0 deletions packages/graphql-mesh-server/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
Expand Down
4 changes: 4 additions & 0 deletions packages/prerender-fargate/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
Expand Down
4 changes: 4 additions & 0 deletions packages/prerender-proxy/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
Expand Down
4 changes: 4 additions & 0 deletions packages/rabbitmq/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
Expand Down
4 changes: 4 additions & 0 deletions packages/shared-vpc/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
Expand Down
4 changes: 4 additions & 0 deletions packages/static-hosting/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
Expand Down
4 changes: 4 additions & 0 deletions packages/waf/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
Expand Down
60 changes: 60 additions & 0 deletions tools/scripts/publish.mjs
Original file line number Diff line number Diff line change
@@ -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}`);

0 comments on commit 778a970

Please sign in to comment.