Skip to content

Commit c36baed

Browse files
committed
Prevents npm publish if version already exists
Adds a workflow step to check if the package version is unpublished on npm before attempting to publish. Skips publishing and outputs a notice when the version already exists, avoiding redundant deployments and potential errors.
1 parent d848734 commit c36baed

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

.github/workflows/publish.yml

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,38 @@ permissions:
1111
contents: read
1212

1313
jobs:
14-
publish:
14+
check-version:
1515
runs-on: ubuntu-latest
16+
outputs:
17+
should_publish: ${{ steps.check.outputs.should_publish }}
18+
version: ${{ steps.read_version.outputs.version }}
1619
steps:
17-
- name: Checkout
18-
uses: actions/checkout@v4
20+
- uses: actions/checkout@v4
21+
22+
- name: Read version from package.json
23+
id: read_version
24+
run: echo "version=$(jq -r '.version' package.json)" >> $GITHUB_OUTPUT
1925

20-
- name: Check if package version is unpublished on npm
21-
id: version-check
26+
- name: Check if version exists on npm
27+
id: check
2228
run: |
23-
VERSION=$(jq -r '.version' package.json)
24-
NAME=$(jq -r '.name' package.json)
29+
echo "Checking npm for version ${{ steps.read_version.outputs.version }}"
2530
26-
echo "Package: $NAME"
27-
echo "Version: $VERSION"
31+
EXISTS=$(npm view $(jq -r '.name' package.json) versions --json | jq -e '.[] | select(.=="'${{ steps.read_version.outputs.version }}'")' || echo "no")
2832
29-
if npm view "$NAME@$VERSION" version > /dev/null 2>&1; then
30-
echo "### 🛑 Deployment aborted" >> $GITHUB_STEP_SUMMARY
31-
echo "Version $VERSION of $NAME is already published on npm." >> $GITHUB_STEP_SUMMARY
32-
echo "Skipping remaining steps." >> $GITHUB_STEP_SUMMARY
33-
exit 0
33+
if [ "$EXISTS" = "no" ]; then
34+
echo "should_publish=true" >> $GITHUB_OUTPUT
3435
else
35-
echo "Version $VERSION of $NAME is NOT published. Continuing workflow."
36+
echo "should_publish=false" >> $GITHUB_OUTPUT
3637
fi
3738
39+
publish:
40+
needs: check-version
41+
if: needs.check-version.outputs.should_publish == 'true'
42+
runs-on: ubuntu-latest
43+
steps:
44+
- uses: actions/checkout@v4
45+
3846
- uses: actions/setup-node@v4
3947
with:
4048
node-version: '25'
@@ -43,6 +51,17 @@ jobs:
4351

4452
- name: Update npm
4553
run: npm install -g npm@latest
54+
4655
- run: npm ci
4756
- run: npm run build
48-
- run: npm publish --access public
57+
58+
- name: Publish package
59+
run: npm publish --access public
60+
61+
skip-notice:
62+
needs: check-version
63+
if: needs.check-version.outputs.should_publish == 'false'
64+
runs-on: ubuntu-latest
65+
steps:
66+
- name: Version already published
67+
run: echo "Version ${{ needs.check-version.outputs.version }} already exists on npm. Skipping publish."

0 commit comments

Comments
 (0)