Skip to content

Commit

Permalink
Improve NPM publish GH Action to only publish when repo version is la…
Browse files Browse the repository at this point in the history
…test

Signed-off-by: Frank Hinek <[email protected]>
  • Loading branch information
frankhinek committed May 23, 2023
1 parent 382ec71 commit 92eaf1f
Showing 1 changed file with 56 additions and 9 deletions.
65 changes: 56 additions & 9 deletions .github/workflows/release-npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,21 @@ permissions:

jobs:
publish-npm:
name: Release NPM Package
name: NPM Publish
runs-on: ubuntu-latest

strategy:
matrix:
package:
[
"crypto",
"dids",
"web5",
"web5-agent",
"web5-proxy-agent",
"web5-user-agent",
]

steps:
- name: Checkout source
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
Expand All @@ -28,19 +40,54 @@ jobs:
- name: Install latest npm
run: npm install -g npm@latest

- name: Install dependencies
run: npm ci

- name: Build all workspace packages
run: npm run build
- name: Install semver utility
run: npm install -g [email protected]

# 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: Display the npm username of the currently logged-in user
- 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 @tbd54566975/${{ matrix.package }} version)
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 "@tbd54566975/${{ matrix.package }}@$REPO_VERSION is latest"
echo "IS_LATEST=true" >> $GITHUB_ENV
else
echo "@tbd54566975/${{ 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 Release to NPM Public Registry
run: npm publish --ws --access public --provenance
- name: Publish @tbd54566975/${{ 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

0 comments on commit 92eaf1f

Please sign in to comment.