added build script #9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Publish to npm (Only if version changes) | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
check-and-publish: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 2 # Fetch the last commit for comparison | |
- name: Set up Node.js | |
uses: actions/setup-node@v3 | |
with: | |
node-version: 18 | |
registry-url: "https://registry.npmjs.org/" | |
- name: Get previous package.json version | |
id: prev_version | |
run: | | |
PREV_VERSION=$(git show HEAD~1:package.json | jq -r '.version') | |
echo "Previous version: $PREV_VERSION" | |
echo "PREV_VERSION=$PREV_VERSION" >> $GITHUB_ENV | |
- name: Get current package.json version | |
id: current_version | |
run: | | |
CUR_VERSION=$(jq -r '.version' package.json) | |
echo "Current version: $CUR_VERSION" | |
echo "CUR_VERSION=$CUR_VERSION" >> $GITHUB_ENV | |
- name: Check if version changed | |
id: version_check | |
run: | | |
if [ "$PREV_VERSION" != "$CUR_VERSION" ]; then | |
echo "Version changed, proceeding with publish." | |
echo "PUBLISH=true" >> $GITHUB_ENV | |
else | |
echo "Version did not change, skipping publish." | |
echo "PUBLISH=false" >> $GITHUB_ENV | |
fi | |
- name: Install dependencies | |
if: env.PUBLISH == 'true' | |
run: npm install | |
- name: Build package (if necessary) | |
if: env.PUBLISH == 'true' | |
run: npm run build || echo "No build step" | |
- name: Publish to npm | |
if: env.PUBLISH == 'true' | |
run: npm publish --access public | |
env: | |
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
- name: Skip publishing | |
if: env.PUBLISH == 'false' | |
run: echo "No version change detected. Skipping npm publish." |