Skip to content

v0.4.1

v0.4.1 #122

Workflow file for this run

name: Publish to NPM
on:
push:
branches:
- main
tags:
- 'v*'
workflow_dispatch:
permissions:
contents: read
id-token: write # Required for OIDC trusted publishing
jobs:
publish:
name: Publish to NPM
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Required for git describe to find tags
- uses: ./.github/actions/setup-cmux
with:
install-imagemagick: 'true'
# Sets up .npmrc with the auth token
- uses: actions/setup-node@v4
with:
registry-url: 'https://registry.npmjs.org'
- run: sudo npm i -g npm@latest
- name: Generate unique version from git
id: version
run: |
# Get base version from package.json
BASE_VERSION=$(node -p "require('./package.json').version")
# Generate git describe version
GIT_DESCRIBE=$(git describe --tags --always --dirty 2>/dev/null || echo "unknown")
if [[ $GITHUB_REF == refs/tags/* ]]; then
# For tags, use the base version as-is (stable release)
NPM_VERSION="${BASE_VERSION}"
NPM_TAG="latest"
echo "Publishing stable release: ${NPM_VERSION}"
else
# For main branch, create a pre-release version using git describe
# Format: 0.3.0-next.5.g1a2b3c4 (base-next.commits.hash)
GIT_COMMIT=$(git rev-parse --short HEAD)
COMMITS_SINCE_TAG=$(git rev-list --count HEAD ^$(git describe --tags --abbrev=0 2>/dev/null || echo HEAD) 2>/dev/null || echo "0")
NPM_VERSION="${BASE_VERSION}-next.${COMMITS_SINCE_TAG}.g${GIT_COMMIT}"
NPM_TAG="next"
echo "Publishing pre-release: ${NPM_VERSION}"
fi
echo "version=${NPM_VERSION}" >> $GITHUB_OUTPUT
echo "tag=${NPM_TAG}" >> $GITHUB_OUTPUT
# Update package.json with the new version
node -e "const fs = require('fs'); const pkg = JSON.parse(fs.readFileSync('package.json')); pkg.version = '${NPM_VERSION}'; fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n');"
echo "Updated package.json to version ${NPM_VERSION}"
- name: Generate version file
run: ./scripts/generate-version.sh
- name: Build application
run: make build
- name: Check if version exists
id: check-exists
run: |
PACKAGE_NAME=$(node -p "require('./package.json').name")
VERSION="${{ steps.version.outputs.version }}"
if npm view "${PACKAGE_NAME}@${VERSION}" version &>/dev/null; then
echo "exists=true" >> $GITHUB_OUTPUT
echo "Version ${VERSION} already exists on npm"
else
echo "exists=false" >> $GITHUB_OUTPUT
echo "Version ${VERSION} does not exist, will publish"
fi
- name: Publish to NPM
if: steps.check-exists.outputs.exists == 'false'
run: npm publish --tag ${{ steps.version.outputs.tag }}
- name: Update dist-tag (version already exists)
if: steps.check-exists.outputs.exists == 'true' && github.ref_type == 'tag'
run: |
PACKAGE_NAME=$(node -p "require('./package.json').name")
VERSION="${{ steps.version.outputs.version }}"
TAG="${{ steps.version.outputs.tag }}"
echo "Version ${VERSION} already published, updating dist-tag to ${TAG}"
npm dist-tag add "${PACKAGE_NAME}@${VERSION}" "${TAG}"
- name: Skip (pre-release already exists)
if: steps.check-exists.outputs.exists == 'true' && github.ref_type != 'tag'
run: |
echo "⏭️ Pre-release version already exists, skipping"