I have a package that I want to publish on NPM under two conditions:
I have the following Github action:
name: CI/CD Pipeline
on:
push:
branches:
- master
pull_request:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
cache: "npm"
- name: Install dependencies
run: npm install
- name: Run typescript
run: npm run tsc
- name: Run linter
run: npm run lint
- name: Run tests
run: npm run test
- name: Build the project
run: npm run build
publish:
if: github.ref == 'refs/heads/master' && github.event_name == 'push'
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
cache: "npm"
- name: Install dependencies
run: npm install
- name: Check if package version has been bumped
id: version_check
run: |
current_version=$(jq -r '.version' package.json)
echo "Current version: $current_version"
git fetch --tags
previous_version=$(git tag --sort=-v:refname | head -n 1 || echo "none")
echo "Previous version: $previous_version"
if [ "$previous_version" != "none" ] && [ "$previous_version" == "v$current_version" ]; then
echo "Version has not been bumped. Exiting."
exit 1
fi
- name: Publish to npm
if: success()
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npm publish --access=public
- name: Create GitHub Release
if: success()
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git tag "v${{ steps.version_check.outputs.current_version }}"
git push origin "v${{ steps.version_check.outputs.current_version }}"
release_notes=$(git log -1 --pretty=%B)
gh release create "v${{ steps.version_check.outputs.current_version }}" --title "v${{ steps.version_check.outputs.current_version }}" --notes "${release_notes}"
The build
job runs fine. The publish
job is triggered as well. But the step Publish to npm
is never called, because needs.build.outputs.current_version
is always empty.
How to fix this?
You need to change the logic:
if [ "$previous_version" == "v$current_version" ]; then
echo "Version has not been bumped. Exiting."
exit 1
else
echo current_version=$current_version >> $GITHUB_OUTPUT
fi