8000 [release] fix: correctly set tag during publish by devjiwonchoi · Pull Request #79548 · vercel/next.js · GitHub
[go: up one dir, main page]

Skip to content

[release] fix: correctly set tag during publish #79548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/build_and_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,6 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.RELEASE_BOT_GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN_ELEVATED }}
RELEASE_TYPE: ${{ github.event.inputs.releaseType }}

# Add label to verify the PR is created from this workflow.
- name: Add label to PR
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/trigger_release_new.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,4 @@ jobs:
version: pnpm ci:version
env:
GITHUB_TOKEN: ${{ secrets.RELEASE_BOT_GITHUB_TOKEN }}
RELEASE_TYPE: ${{ github.event.inputs.releaseType }}
100 changes: 90 additions & 10 deletions scripts/release/publish-npm.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,97 @@
import execa from 'execa'
import semver from 'semver'
import { existsSync } from 'fs'
import { join } from 'path'
import { readdir, readFile } from 'fs/promises'

async function fetchTagsFromRegistry(packageName: string) {
const res = await fetch(
`https://registry.npmjs.org/-/package/${packageName}/dist-tags`
)
const tags = await res.json()
return tags
}

async function getTag({
name,
version,
}: {
name: string
version: string
}): Promise<string> {
const preConfigPath = join(process.cwd(), '.changeset', 'pre.json')

if (existsSync(preConfigPath)) {
const { tag, mode } = JSON.parse(await readFile(preConfigPath, 'utf-8'))
if (mode === 'pre') {
if (!version.includes('-')) {
throw new Error(
`The changeset is in pre mode, but the version of "${name}@${version}" is not prerelease. It is likely a bug from versioning the packages.`
)
}

return tag
}
}

if (version.includes('-')) {
throw new Error(
`The changeset is not in pre mode, but the version of "${name}@${version}" is prerelease. It is likely a bug from versioning the packages.`
)
}

return 'latest'
}

async function publishNpm() {
const releaseType = process.env.RELEASE_TYPE
const tag =
releaseType === 'canary'
? 'canary'
: releaseType === 'release-candidate'
? 'rc'
: 'latest'

await execa('pnpm', ['changeset', 'publish', '--tag', tag], {
stdio: 'inherit',
if (!process.env.NPM_TOKEN) {
throw new Error('NPM_TOKEN is not set')
}

const packagesDir = join(process.cwd(), 'packages')
const packageDirs = await readdir(packagesDir, {
withFileTypes: true,
})

for (const packageDir of packageDirs) {
if (!packageDir.isDirectory()) {
continue
}

const pkgJson = JSON.parse(
await readFile(
join(process.cwd(), 'packages', packageDir.name, 'package.json'),
'utf-8'
)
)

if (pkgJson.private) {
continue
}

const tags = await fetchTagsFromRegistry(pkgJson.name)
// If the current version is already published in the
// registry, skip the process.
if (semver.eq(pkgJson.version, tags.latest)) {
console.log(
`Skipping ${pkgJson.name}@${pkgJson.version} because it is already published.`
)
continue
}

const tag = await getTag({
name: pkgJson.name,
version: pkgJson.version,
})

const packagePath = join(packagesDir, packageDir.name)
const args = ['publish', packagePath, '--tag', tag]

console.log(
`Running command: "pnpm ${args.join(' ')}" for ${pkgJson.name}@${pkgJson.version}`
)
await execa('pnpm', args, { stdio: 'inherit' })
}
}

publishNpm()
Loading
0