Release to NPM Registry #22
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Release to NPM Registry | |
on: | |
release: | |
types: [created] | |
workflow_dispatch: | |
# Allow only one concurrent deployment,but do NOT cancel in-progress runs as | |
# we want to allow these release deployments to complete. | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.ref }} | |
cancel-in-progress: false | |
permissions: | |
contents: read | |
id-token: write # necessary for NPM provenance | |
jobs: | |
publish-npm: | |
name: NPM Publish | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
package: | |
[ | |
"agent", | |
"api", | |
"common", | |
"credentials", | |
"crypto", | |
"dids", | |
"identity-agent", | |
"proxy-agent", | |
"user-agent", | |
] | |
steps: | |
- name: Checkout source | |
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 #v4.1.1 | |
- name: Set up Node.js | |
uses: actions/setup-node@5ef044f9d09786428e6e895be6be17937becee3a #v4.0.0 | |
with: | |
node-version: 18 | |
registry-url: https://registry.npmjs.org/ | |
cache: 'npm' | |
- name: Install semver utility | |
run: npm install -g semver@7.5.1 | |
# Note - this is not required but it gives a clean failure prior to attempting a release if the GH workflow runner is not authenticated with NPMjs.com | |
- name: Verify NPM token is authenticated with NPMjs.com | |
env: | |
NODE_AUTH_TOKEN: ${{secrets.npm_token}} | |
run: npm whoami | |
- name: Check if GitHub repo package version is latest | |
env: | |
NODE_AUTH_TOKEN: ${{secrets.npm_token}} | |
run: | | |
cd packages/${{ matrix.package }} | |
# Fetch the published version on NPMjs.com. | |
PUBLISHED_VERSION=$(npm view @web5/${{ matrix.package }} version 2>/dev/null || echo "0.0.0") | |
echo "Published Version: $PUBLISHED_VERSION" | |
# Fetch the version in the GitHub repo's package.json file. | |
REPO_VERSION=$(node -p "require('./package.json').version") | |
echo "REPO_VERSION=$REPO_VERSION" >> $GITHUB_ENV | |
echo "Repo Version: $REPO_VERSION" | |
# Compare the repo and NPMjs.com package versions. | |
IS_GREATER=$(semver --range ">$PUBLISHED_VERSION" $REPO_VERSION || true) | |
if [ -n "$IS_GREATER" ] ; then | |
echo "@web5/${{ matrix.package }}@$REPO_VERSION is latest" | |
echo "IS_LATEST=true" >> $GITHUB_ENV | |
else | |
echo "@web5/${{ matrix.package }}@$REPO_VERSION is already published or repo version is lower" | |
echo "IS_LATEST=false" >> $GITHUB_ENV | |
fi | |
shell: bash | |
- name: Install dependencies | |
if: env.IS_LATEST == 'true' | |
run: npm ci | |
- name: Build all workspace packages | |
if: env.IS_LATEST == 'true' | |
run: npm run build | |
- name: Publish @web5/${{ matrix.package }}@${{ env.REPO_VERSION }} | |
if: env.IS_LATEST == 'true' | |
env: | |
NODE_AUTH_TOKEN: ${{secrets.npm_token}} | |
run: | | |
cd packages/${{ matrix.package }} | |
npm publish --access public --provenance | |
shell: bash |