Publish Packages to NPM #72
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: Publish Packages to NPM | |
on: | |
workflow_run: | |
workflows: ["Create GH Release"] | |
types: | |
- completed | |
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 | |
# only runs if workflow_run is completed successfully or manually workflow dispatch | |
if: github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' | |
strategy: | |
max-parallel: 1 | |
matrix: | |
package: ["protocol", "http-client", "http-server"] | |
steps: | |
- name: Checkout source | |
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2 | |
with: | |
submodules: 'true' | |
- name: install pnpm | |
uses: pnpm/action-setup@v2 | |
with: | |
version: 8 | |
- name: Set up Node.js | |
uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0 | |
with: | |
node-version: 20 | |
registry-url: https://registry.npmjs.org/ | |
# 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: pnpm whoami | |
- name: Install dependencies | |
run: pnpm install | |
- name: Build packages | |
run: pnpm build | |
- 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=$(pnpm view @tbdex/${{ 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=$(pnpm semver --range ">$PUBLISHED_VERSION" $REPO_VERSION || true) | |
if [ -n "$IS_GREATER" ] ; then | |
echo "@tbdex/${{ matrix.package }}@$REPO_VERSION is latest" | |
echo "IS_LATEST=true" >> $GITHUB_ENV | |
else | |
echo "@tbdex/${{ matrix.package }}@$REPO_VERSION is already published or repo version is lower" | |
echo "IS_LATEST=false" >> $GITHUB_ENV | |
fi | |
shell: bash | |
- name: Publish @tbdex/${{ matrix.package }}@${{ env.REPO_VERSION }} | |
if: env.IS_LATEST == 'true' | |
env: | |
NODE_AUTH_TOKEN: ${{secrets.npm_token}} | |
NPM_CONFIG_PROVENANCE: true | |
run: | | |
cd packages/${{ matrix.package }} | |
pnpm publish --access public | |
shell: bash |