Skip to content

Publish Package

Publish Package #20

Workflow file for this run

name: Publish Package
on:
workflow_dispatch:
inputs:
ref:
description: Optional git ref to publish. Leave blank to use the selected branch or tag.
required: false
type: string
permissions:
contents: write
id-token: write
jobs:
publish:
runs-on: ubuntu-latest
environment: npm-publish
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ inputs.ref || github.ref }}
fetch-depth: 0
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 22
registry-url: https://registry.npmjs.org/
cache: npm
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
- name: Build package bundle
run: npm run build
- name: Read package version
id: package
run: |
printf 'version=%s\n' "$(node -p "require('./package.json').version")" >> "$GITHUB_OUTPUT"
- name: Ensure release tag does not already exist
run: |
tag="v${{ steps.package.outputs.version }}"
if git rev-parse "$tag" >/dev/null 2>&1; then
echo "Tag $tag already exists"
exit 1
fi
- name: Publish to npm
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Create and push release tag
run: |
tag="v${{ steps.package.outputs.version }}"
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git tag -a "$tag" -m "$tag"
git push origin "$tag"
- name: Extract release notes from CHANGELOG
run: |
version="${{ steps.package.outputs.version }}"
awk -v version="$version" '
$0 == "## " version { in_section = 1; print; next }
/^## / && in_section { exit }
in_section { print }
' CHANGELOG.md > release-notes.md
if [ ! -s release-notes.md ]; then
echo "Failed to extract release notes for version $version from CHANGELOG.md"
exit 1
fi
- name: Create GitHub release
env:
GH_TOKEN: ${{ github.token }}
run: |
tag="v${{ steps.package.outputs.version }}"
gh release create "$tag" \
--generate-notes \
--notes "$(cat release-notes.md)" \
--verify-tag