Skip to content

Commit 7748b1b

Browse files
committed
ci: harden publish workflow reruns
1 parent 25fb9df commit 7748b1b

1 file changed

Lines changed: 49 additions & 17 deletions

File tree

.github/workflows/npm-deploy.yml

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ on:
66
- 1.x
77
workflow_dispatch:
88

9+
concurrency:
10+
group: publish-package-${{ github.ref }}
11+
cancel-in-progress: false
12+
913
jobs:
1014
verify_version:
1115
runs-on: ubuntu-latest
@@ -23,40 +27,50 @@ jobs:
2327
run: |
2428
echo "Current branch: ${{ github.ref }}"
2529
26-
# Get current version
30+
# Get current package metadata
31+
PACKAGE_NAME=$(jq -r .name package.json)
2732
CURRENT_VERSION=$(jq -r .version package.json)
33+
echo "Package: $PACKAGE_NAME"
2834
echo "Current version: $CURRENT_VERSION"
2935
30-
# Get previous commit hash
31-
git rev-parse HEAD~1 || git rev-parse HEAD
32-
PREV_COMMIT=$(git rev-parse HEAD~1 2>/dev/null || git rev-parse HEAD)
36+
# Get previous commit hash and determine whether package.json changed.
37+
if git rev-parse HEAD~1 >/dev/null 2>&1; then
38+
PREV_COMMIT=$(git rev-parse HEAD~1)
39+
PACKAGE_JSON_CHANGED=$(
40+
git diff --name-only HEAD~1 HEAD | grep -q "^package.json$" && echo "true" || echo "false"
41+
)
42+
else
43+
PREV_COMMIT=$(git rev-parse HEAD)
44+
PACKAGE_JSON_CHANGED=$(
45+
git show --pretty='' --name-only HEAD | grep -q "^package.json$" && echo "true" || echo "false"
46+
)
47+
fi
3348
34-
# Check if package.json changed
35-
if git diff --name-only HEAD~1 HEAD | grep "package.json"; then
49+
if [[ "$PACKAGE_JSON_CHANGED" == "true" ]]; then
3650
echo "Package.json was changed in this commit"
37-
51+
3852
# Get previous version if possible
39-
if git show "$PREV_COMMIT:package.json" 2>/dev/null; then
53+
if git show "$PREV_COMMIT:package.json" >/dev/null 2>&1; then
4054
PREV_VERSION=$(git show "$PREV_COMMIT:package.json" | jq -r .version)
4155
echo "Previous version: $PREV_VERSION"
42-
56+
4357
if [ "$CURRENT_VERSION" != "$PREV_VERSION" ]; then
4458
echo "Version changed from $PREV_VERSION to $CURRENT_VERSION"
45-
echo "should_publish=true" >> $GITHUB_OUTPUT
59+
echo "should_publish=true" >> "$GITHUB_OUTPUT"
4660
else
4761
echo "Version unchanged"
48-
echo "should_publish=false" >> $GITHUB_OUTPUT
62+
echo "should_publish=false" >> "$GITHUB_OUTPUT"
4963
fi
5064
else
5165
echo "First commit with package.json, will publish"
52-
echo "should_publish=true" >> $GITHUB_OUTPUT
66+
echo "should_publish=true" >> "$GITHUB_OUTPUT"
5367
fi
5468
else
5569
echo "Package.json not changed in this commit"
56-
echo "should_publish=false" >> $GITHUB_OUTPUT
70+
echo "should_publish=false" >> "$GITHUB_OUTPUT"
5771
fi
5872
59-
echo "version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
73+
echo "version=$CURRENT_VERSION" >> "$GITHUB_OUTPUT"
6074
6175
publish:
6276
needs: verify_version
@@ -72,10 +86,19 @@ jobs:
7286

7387
- name: Create Git tag
7488
run: |
89+
TAG_NAME="v${{ needs.verify_version.outputs.version }}"
90+
7591
git config user.name "github-actions[bot]"
7692
git config user.email "github-actions[bot]@users.noreply.github.com"
77-
git tag -a "v${{ needs.verify_version.outputs.version }}" -m "Release v${{ needs.verify_version.outputs.version }}"
78-
git push origin "v${{ needs.verify_version.outputs.version }}"
93+
94+
if git ls-remote --exit-code --tags origin "refs/tags/${TAG_NAME}" >/dev/null 2>&1; then
95+
echo "Tag ${TAG_NAME} already exists on origin; skipping tag creation"
96+
elif git rev-parse "${TAG_NAME}" >/dev/null 2>&1; then
97+
echo "Tag ${TAG_NAME} already exists locally; skipping tag push"
98+
else
99+
git tag -a "${TAG_NAME}" -m "Release ${TAG_NAME}"
100+
git push origin "${TAG_NAME}"
101+
fi
79102
env:
80103
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
81104

@@ -89,7 +112,16 @@ jobs:
89112
run: bun run build
90113

91114
- name: Publish to npm
92-
run: bun publish
115+
run: |
116+
VERSION="${{ needs.verify_version.outputs.version }}"
117+
DIST_TAG="latest"
118+
119+
if [[ "$VERSION" =~ -([A-Za-z0-9]+) ]]; then
120+
DIST_TAG="${BASH_REMATCH[1]}"
121+
fi
122+
123+
echo "Publishing ${VERSION} with dist-tag ${DIST_TAG}"
124+
bun publish --access public --tag "${DIST_TAG}" --tolerate-republish
93125
env:
94126
NPM_CONFIG_TOKEN: ${{ secrets.NPM_TOKEN }}
95127

0 commit comments

Comments
 (0)