Skip to content

Commit 2c97265

Browse files
committed
Make npm publish workflow idempotent with retries
Skip packages already published to npm, and retry failed publishes up to 3 times with a 30-second delay. After a failed attempt, also check if the version was published by a concurrent run (TOCTOU). [ci skip] Assisted-by: OpenCode:deepseek-v4-pro Assisted-by: Codex:gpt-5.5
1 parent e9ad1fa commit 2c97265

1 file changed

Lines changed: 34 additions & 7 deletions

File tree

.github/workflows/build.yaml

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,39 @@ jobs:
5555
echo "Skipping private package: $pkg"
5656
continue
5757
fi
58-
npm publish \
59-
--logs-dir=. \
60-
--provenance \
61-
--access public \
62-
--tag "$TAG" \
63-
"$pkg" \
64-
|| grep "Cannot publish over previously published version" *.log
58+
PKG_NAME=$(tar -xOzf "$pkg" package/package.json | jq -r .name)
59+
PKG_VERSION=$(tar -xOzf "$pkg" package/package.json | jq -r .version)
60+
# Skip if this version is already published
61+
if npm view "$PKG_NAME@$PKG_VERSION" version > /dev/null 2>&1; then
62+
echo "Skipping already published: $PKG_NAME@$PKG_VERSION"
63+
continue
64+
fi
65+
# Publish with retry (up to 3 attempts)
66+
success=false
67+
for attempt in 1 2 3; do
68+
echo "Publishing $PKG_NAME@$PKG_VERSION (attempt $attempt/3)"
69+
if npm publish \
70+
--logs-dir=. \
71+
--provenance \
72+
--access public \
73+
--tag "$TAG" \
74+
"$pkg"; then
75+
echo "Successfully published $PKG_NAME@$PKG_VERSION"
76+
success=true
77+
break
78+
fi
79+
# Check if version was published by another concurrent run
80+
if npm view "$PKG_NAME@$PKG_VERSION" version > /dev/null 2>&1; then
81+
echo "Version $PKG_VERSION already exists on npm (published by another run)"
82+
success=true
83+
break
84+
fi
85+
echo "Failed to publish $PKG_NAME@$PKG_VERSION, waiting 30 seconds..."
86+
sleep 30
87+
done
88+
if [ "$success" = false ]; then
89+
echo "ERROR: Failed to publish $PKG_NAME@$PKG_VERSION after 3 attempts"
90+
exit 1
91+
fi
6592
rm -f *.log
6693
done

0 commit comments

Comments
 (0)