Skip to content

Commit 2219ba0

Browse files
[release] fix: correctly set tag during publish (#79548)
### Why? The `process.env.RELEASE_TYPE` handling was set incorrectly. It should've been set to the "trigger" workflow, but instead, it was set to the "publish" workflow. Therefore, the "publish" workflow needs to know which dist tag to set for publish. --------- Co-authored-by: Sebastian Sebbie Silbermann <sebastian.silbermann@vercel.com>
1 parent e4d18b0 commit 2219ba0

File tree

3 files changed

+91
-11
lines changed

3 files changed

+91
-11
lines changed

.github/workflows/build_and_deploy.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,6 @@ jobs:
611611
env:
612612
GITHUB_TOKEN: ${{ secrets.RELEASE_BOT_GITHUB_TOKEN }}
613613
NPM_TOKEN: ${{ secrets.NPM_TOKEN_ELEVATED }}
614-
RELEASE_TYPE: ${{ github.event.inputs.releaseType }}
615614

616615
# Add label to verify the PR is created from this workflow.
617616
- name: Add label to PR

.github/workflows/trigger_release_new.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,4 @@ jobs:
113113
version: pnpm ci:version
114114
env:
115115
GITHUB_TOKEN: ${{ secrets.RELEASE_BOT_GITHUB_TOKEN }}
116+
RELEASE_TYPE: ${{ github.event.inputs.releaseType }}

scripts/release/publish-npm.ts

Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,97 @@
11
import execa from 'execa'
2+
import semver from 'semver'
3+
import { existsSync } from 'fs'
4+
import { join } from 'path'
5+
import { readdir, readFile } from 'fs/promises'
6+
7+
async function fetchTagsFromRegistry(packageName: string) {
8+
const res = await fetch(
9+
`https://registry.npmjs.org/-/package/${packageName}/dist-tags`
10+
)
11+
const tags = await res.json()
12+
return tags
13+
}
14+
15+
async function getTag({
16+
name,
17+
version,
18+
}: {
19+
name: string
20+
version: string
21+
}): Promise<string> {
22+
const preConfigPath = join(process.cwd(), '.changeset', 'pre.json')
23+
24+
if (existsSync(preConfigPath)) {
25+
const { tag, mode } = JSON.parse(await readFile(preConfigPath, 'utf-8'))
26+
if (mode === 'pre') {
27+
if (!version.includes('-')) {
28+
throw new Error(
29+
`The changeset is in pre mode, but the version of "${name}@${version}" is not prerelease. It is likely a bug from versioning the packages.`
30+
)
31+
}
32+
33+
return tag
34+
}
35+
}
36+
37+
if (version.includes('-')) {
38+
throw new Error(
39+
`The changeset is not in pre mode, but the version of "${name}@${version}" is prerelease. It is likely a bug from versioning the packages.`
40+
)
41+
}
42+
43+
return 'latest'
44+
}
245

346
async function publishNpm() {
4-
const releaseType = process.env.RELEASE_TYPE
5-
const tag =
6-
releaseType === 'canary'
7-
? 'canary'
8-
: releaseType === 'release-candidate'
9-
? 'rc'
10-
: 'latest'
11-
12-
await execa('pnpm', ['changeset', 'publish', '--tag', tag], {
13-
stdio: 'inherit',
47+
if (!process.env.NPM_TOKEN) {
48+
throw new Error('NPM_TOKEN is not set')
49+
}
50+
51+
const packagesDir = join(process.cwd(), 'packages')
52+
const packageDirs = await readdir(packagesDir, {
53+
withFileTypes: true,
1454
})
55+
56+
for (const packageDir of packageDirs) {
57+
if (!packageDir.isDirectory()) {
58+
continue
59+
}
60+
61+
const pkgJson = JSON.parse(
62+
await readFile(
63+
join(process.cwd(), 'packages', packageDir.name, 'package.json'),
64+
'utf-8'
65+
)
66+
)
67+
68+
if (pkgJson.private) {
69+
continue
70+
}
71+
72+
const tags = await fetchTagsFromRegistry(pkgJson.name)
73+
// If the current version is already published in the
74+
// registry, skip the process.
75+
if (semver.eq(pkgJson.version, tags.latest)) {
76+
console.log(
77+
`Skipping ${pkgJson.name}@${pkgJson.version} because it is already published.`
78+
)
79+
continue
80+
}
81+
82+
const tag = await getTag({
83+
name: pkgJson.name,
84+
version: pkgJson.version,
85+
})
86+
87+
const packagePath = join(packagesDir, packageDir.name)
88+
const args = ['publish', packagePath, '--tag', tag]
89+
90+
console.log(
91+
`Running command: "pnpm ${args.join(' ')}" for ${pkgJson.name}@${pkgJson.version}`
92+
)
93+
await execa('pnpm', args, { stdio: 'inherit' })
94+
}
1595
}
1696

1797
publishNpm()

0 commit comments

Comments
 (0)