Skip to content

Publish to NPM

Publish to NPM #7

Workflow file for this run

name: Publish to NPM
on:
push:
branches: [master]
paths:
- "package.json"
- "src/**"
- "methods/**"
workflow_dispatch:
jobs:
publish:
runs-on: ubuntu-latest
if: github.repository == 'JeffML/eco.json'
permissions:
contents: write
id-token: write
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "18"
cache: "npm"
registry-url: "https://registry.npmjs.org"
- name: Install dependencies
run: npm ci
- name: Type check
run: npm run type-check
- name: Build
run: npm run build
- name: Check if version changed
id: version-check
run: |
PACKAGE_VERSION=$(node -p "require('./package.json').version")
NPM_VERSION=$(npm view @chess-openings/eco.json version 2>/dev/null || echo "0.0.0")
echo "package_version=$PACKAGE_VERSION" >> $GITHUB_OUTPUT
echo "npm_version=$NPM_VERSION" >> $GITHUB_OUTPUT
if [ "$PACKAGE_VERSION" != "$NPM_VERSION" ]; then
echo "changed=true" >> $GITHUB_OUTPUT
echo "Version changed from $NPM_VERSION to $PACKAGE_VERSION"
else
echo "changed=false" >> $GITHUB_OUTPUT
echo "Version unchanged at $PACKAGE_VERSION"
fi
- name: Publish to NPM
if: steps.version-check.outputs.changed == 'true'
run: npm publish --provenance --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Create Git tag
if: steps.version-check.outputs.changed == 'true'
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git tag -a v${{ steps.version-check.outputs.package_version }} -m "Release v${{ steps.version-check.outputs.package_version }}"
git push origin v${{ steps.version-check.outputs.package_version }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}