Skip to content

Build&Publish

Build&Publish #78

Workflow file for this run

name: Build&Publish
on:
push:
branches: [ main ]
pull_request:
release:
types: [published]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org'
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm run test:ci
# Only build and publish on push to main and release events
- name: Build and publish (main branch only)
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
run: |
NPM_PACKAGE_VERSION=$(node -e "const fs = require('fs'); console.log(JSON.parse(fs.readFileSync('package.json')).version);")
SHORT_SHA=$(echo $GITHUB_SHA | cut -c1-7)
npm run build
npm --no-git-tag-version version $NPM_PACKAGE_VERSION-rc.$SHORT_SHA
npm publish --access public --tag dev
env:
NODE_AUTH_TOKEN: ${{ secrets.NPMJS_PUBLISH }}
CI: true
GITHUB_SHA: ${{ github.sha }}
- name: Fetch all branches and tags
run: git fetch origin +refs/heads/main:refs/remotes/origin/main --tags --prune
- name: Publish on release (tag must be ancestor of main)
if: github.event_name == 'release'
run: |
TAG_COMMIT=$(git rev-parse ${{ github.event.release.tag_name }})
MAIN_COMMIT=$(git rev-parse origin/main)
if git merge-base --is-ancestor "$TAG_COMMIT" "$MAIN_COMMIT"; then
echo "Tag is on main's history, proceeding with publish."
npm run build
npm publish --access public
else
echo "Tag commit is not in main's history. Skipping publish."
exit 0
fi
env:
NODE_AUTH_TOKEN: ${{ secrets.NPMJS_PUBLISH }}
CI: true