Skip to content

Commit e435453

Browse files
authored
fix(dev-tools): make ocular-publish safe to re-run after a partial publish (#71)
1 parent 11de43b commit e435453

4 files changed

Lines changed: 71 additions & 8 deletions

File tree

‎modules/dev-tools/README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,10 @@ GitHub release when possible and selects the npm dist-tag from the package versi
401401
changes package versions, Git commits/tags, remote branches, and npm state; run it only from a
402402
reviewed release checkout.
403403

404+
Version modes push only the tags on the version commit. `from-git` can be re-run for the same tag:
405+
it keeps an existing GitHub release, publishes only versions missing from npm, and retries failed
406+
lerna publishes (up to 3 attempts, 120 seconds apart).
407+
404408
## ESM repositories
405409

406410
For native ESM:

‎modules/dev-tools/docs/cli/ocular-publish.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,21 @@ npx ocular-publish [mode] [npm-tag]
2525
## npm-tag
2626

2727
Custom tag for the release. If not specified, the release is tagged with `beta` if `mode: beta` and `latest` if `mode: prod`.
28+
29+
## Pushing the version
30+
31+
The version modes push the release branch and then only the tags on the version commit. They don't use `git push --tags`, which would also push any local tags missing from the remote, such as old prerelease tags or tags fetched from other repositories. GitHub doesn't create push events when more than three tags are pushed at once, so a release workflow triggered by tags would silently not run.
32+
33+
## Re-running a partial publish
34+
35+
`from-git` is safe to run again for the same tag:
36+
37+
- If the GitHub release for the tag already exists, it's left as is.
38+
- In a monorepo, `lerna publish from-package` only publishes versions that aren't on npm yet. A failed attempt is retried automatically, up to 3 attempts, 120 seconds apart.
39+
- A single package is skipped if its version is already on npm.
40+
41+
In the release workflow, use **Re-run failed jobs** to finish a publish that failed partway.
42+
43+
Known cause: `npm publish --provenance` can fail with `TLOG_CREATE_ENTRY_ERROR ... (409) an equivalent entry already exists in the transparency log`, which leaves that package unpublished ([npm/cli#10078](https://github.com/npm/cli/issues/10078)). A new attempt succeeds.
44+
45+
Newly published versions can take a few minutes to appear on the registry. If a retry fails only with `You cannot publish over the previously published versions`, those packages are published. Check with `npm view <package>@<version>`.

‎modules/dev-tools/scripts/publish.sh‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,13 @@ bumpVersion() {
5252
exit 1
5353
fi
5454

55-
# push to branch
56-
(set -x; git push --no-verify && git push --tags --no-verify)
55+
# Push the branch, then only the tags on the version commit, to the same remote that `git push`
56+
# picks for the branch. `git push --tags` would also push any stray local tags, and GitHub skips
57+
# tag workflows when more than 3 tags are pushed at once, so the release workflow would never run.
58+
local branch=`git branch --show-current`
59+
local remote=`git config "branch.$branch.pushRemote" || git config remote.pushDefault || git config "branch.$branch.remote" || echo origin`
60+
local tags=`git tag --points-at HEAD | sed 's#^#refs/tags/#'`
61+
(set -x; git push --no-verify && git push --no-verify $remote $tags)
5762
}
5863

5964
publishToNPM() {
@@ -62,9 +67,32 @@ publishToNPM() {
6267

6368
local tag=`node $DEV_TOOLS_DIR/dist/helpers/get-npm-dist-tag.js`
6469
if [ -d "modules" ]; then
65-
(set -x; npx lerna publish from-package --force-publish --yes --dist-tag $tag --no-commit-hooks)
70+
# `from-package` skips versions already on npm, so retrying only publishes the packages
71+
# that failed, e.g. on npm's intermittent provenance TLOG_CREATE_ENTRY_ERROR (409).
72+
# Wait between attempts: new versions can take a few minutes to show up on the registry, and
73+
# retrying sooner can fail with "cannot publish over the previously published versions".
74+
local attempts=3
75+
local delay=120
76+
local attempt=1
77+
until (set -x; npx lerna publish from-package --force-publish --yes --dist-tag $tag --no-commit-hooks); do
78+
if [ $attempt -ge $attempts ]; then
79+
print_red "\nerror: some packages failed to publish after $attempt attempts. Run \`ocular-publish from-git\` again to publish the rest.\n"
80+
exit 1
81+
fi
82+
attempt=$((attempt + 1))
83+
print_red "\nSome packages failed to publish. Retrying in ${delay}s (attempt $attempt of $attempts)\n"
84+
sleep $delay
85+
done
6686
else
67-
(set -x; npm publish --tag $tag)
87+
local name=`node -p "require('./package.json').name"`
88+
local version=`node -p "require('./package.json').version"`
89+
# Check the registry that `npm publish` uses
90+
local registry=`node -p "require('./package.json').publishConfig?.registry || ''"`
91+
if [ -n "`npm view "$name@$version" version ${registry:+--registry "$registry"} 2>/dev/null`" ]; then
92+
echo "$name@$version is already published"
93+
else
94+
(set -x; npm publish --tag $tag)
95+
fi
6896
fi
6997
}
7098

‎modules/dev-tools/src/github-release.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,28 @@ if (releaseNotes === null) {
3737
createRelease();
3838

3939
async function createRelease() {
40+
const headers = {
41+
Authorization: `token ${token}`,
42+
Accept: 'application/vnd.github.v3+json'
43+
};
44+
45+
// A re-run of a partially failed publish finds the release from the first run
46+
// https://docs.github.com/en/rest/releases/releases#get-a-release-by-tag-name
47+
const existing = await fetch(
48+
`https://api.github.com/repos/${repository}/releases/tags/${encodeURIComponent(tag!)}`,
49+
{headers}
50+
);
51+
if (existing.ok) {
52+
console.log(`Release already exists: https://github.com/${repository}/releases/tag/${tag}`);
53+
return;
54+
}
55+
4056
// Publish release notes to GitHub
4157
// https://docs.github.com/en/rest/reference/repos#create-a-release
4258
const url = `https://api.github.com/repos/${repository}/releases`;
4359
const resp = await fetch(url, {
4460
method: 'POST',
45-
headers: {
46-
Authorization: `token ${token}`,
47-
Accept: 'application/vnd.github.v3+json'
48-
},
61+
headers,
4962
body: JSON.stringify({
5063
tag_name: tag,
5164
name: tag,

0 commit comments

Comments
 (0)