Skip to content

Publish NPM package

Publish NPM package #8

Workflow file for this run

name: Publish NPM package
on:
push:
branches:
- dev
- "release/*"
permissions:
contents: write
issues: write
pull-requests: write
jobs:
publish:
runs-on: ubuntu-latest
steps:
# Checkout the repository
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
# Setup Node.js
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
registry-url: "https://registry.npmjs.org"
# Ensure jq is installed
- name: Install jq
run: |
if ! command -v jq &> /dev/null; then
sudo apt-get update && sudo apt-get install -y jq
fi
# Install dependencies
- name: Install dependencies
working-directory: ui
run: npm install
# Audit dependencies
- name: Audit dependencies
working-directory: ui
run: npm audit signatures
# Modify package.json for dev branch
- name: Modify package.json for dev
if: github.ref == 'refs/heads/dev'
run: |
jq '.name = "@lidonation/govtool-outcomes-pillar-ui"' ui/package.json > ui/package.tmp.json && mv ui/package.tmp.json ui/package.json
# Build the package
- name: Build
working-directory: ui
run: npm run build
# Set the npm token based on the branch
- name: Set NPM Token
run: |
if [[ "${GITHUB_REF}" == "refs/heads/dev" ]]; then
echo "NODE_AUTH_TOKEN=${{ secrets.NPM_TOKEN_DEV }}" >> $GITHUB_ENV
elif [[ "${GITHUB_REF}" == refs/heads/release/* ]]; then
echo "NODE_AUTH_TOKEN=${{ secrets.NPM_TOKEN_RELEASE }}" >> $GITHUB_ENV
fi
# Publish package to NPM
- name: Publish to NPM
working-directory: ui
run: |
set -e
# Get the version from package.json
PACKAGE_VERSION=$(jq -r .version package.json)
# Get the branch version
if [[ "${GITHUB_REF}" == refs/heads/release/* ]]; then
BRANCH_VERSION=$(echo "${GITHUB_REF}" | sed -n 's|refs/heads/release/\(.*\)|\1|p')
# Extract the base version
BASE_BRANCH_VERSION=$(echo "$BRANCH_VERSION" | sed -E 's/(-rc\.[0-9]+|-beta\.[0-9]+|-alpha\.[0-9]+)//')
echo "Branch version: $BRANCH_VERSION"
echo "Base branch version: $BASE_BRANCH_VERSION"
echo "package.json version: $PACKAGE_VERSION"
if [[ "$BASE_BRANCH_VERSION" != "$PACKAGE_VERSION" ]]; then
echo "Error: Version in package.json ($PACKAGE_VERSION) does not match base branch version ($BASE_BRANCH_VERSION)"
exit 1
fi
TAG="$BRANCH_VERSION"
elif [[ "${GITHUB_REF}" == "refs/heads/dev" ]]; then
echo "Publishing beta version from dev branch..."
LATEST_BETA_VERSION=$(npm dist-tag ls @lidonation/govtool-outcomes-pillar-ui | grep -E "${PACKAGE_VERSION}-beta" | awk -F': ' '{print $2}' || echo "${PACKAGE_VERSION}-beta.0")
BETA_VERSION_NUMBER=$(echo "$LATEST_BETA_VERSION" | grep -oP 'beta\.\K[0-9]+' || echo 0)
BETA_VERSION_NUMBER=$((BETA_VERSION_NUMBER + 1))
TAG="${PACKAGE_VERSION}-beta.${BETA_VERSION_NUMBER}"
else
echo "Error: Unsupported branch for publishing."
exit 1
fi
jq --arg version "$TAG" '.version = $version' package.json > package.tmp.json && mv package.tmp.json package.json
echo "Updated version in package.json to $TAG"
npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ env.NODE_AUTH_TOKEN }}
# Tag the new release in Git
- name: Tag release
run: |
set -e
git config --local user.name "github-actions[bot]"
git config --local user.email "github-actions[bot]@users.noreply.github.com"
VERSION=$(jq -r .version ui/package.json)
echo "Tagging version: $VERSION"
git tag -a "$VERSION" -m "Version $VERSION"
git push origin "$VERSION"