Repository navigation
Move Changesets versioning and tagging to Azure Pipelines #3715
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
Open
Jake Bailey (jakebailey)
wants to merge
1
commit into
microsoft:v2
Choose a base branch
from
jakebailey:changesets-pipelines
base: v2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| // Opens or updates the "Version Packages" PR while changesets are pending. | ||
| // Once that PR is merged, tags the new versions and creates GitHub releases. | ||
|
|
||
| import { execFileSync } from "node:child_process"; | ||
| import fs from "node:fs"; | ||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
|
|
||
| const pipelinePath = "build/azure-pipelines/changesets.yml"; | ||
| const maxBodyLength = 60000; | ||
|
|
||
| function requiredEnv(name) { | ||
| const value = process.env[name]; | ||
| if (!value) throw new Error(`${name} is required.`); | ||
| return value; | ||
| } | ||
|
|
||
| const repository = requiredEnv("GITHUB_REPOSITORY"); | ||
| const token = requiredEnv("GH_TOKEN"); | ||
| const botName = "typescript-automation[bot]"; | ||
| const botEmail = "290192711+typescript-automation[bot]@users.noreply.github.com"; | ||
| const env = { | ||
| ...process.env, | ||
| GITHUB_TOKEN: token, | ||
| GIT_AUTHOR_NAME: botName, | ||
| GIT_AUTHOR_EMAIL: botEmail, | ||
| GIT_COMMITTER_NAME: botName, | ||
| GIT_COMMITTER_EMAIL: botEmail, | ||
| }; | ||
|
|
||
| function run(command, args, input, extraEnv) { | ||
| console.log(`> ${command} ${args.join(" ")}`); | ||
| return execFileSync(command, args, { env: { ...env, ...extraEnv }, input, encoding: "utf8", stdio: ["pipe", "pipe", "inherit"], maxBuffer: 64 * 1024 * 1024 }); | ||
| } | ||
|
|
||
| function changesetOutput(args) { | ||
| const outputPath = path.join(fs.mkdtempSync(path.join(os.tmpdir(), "changesets-")), "output"); | ||
| process.stdout.write(run("pnpm", ["changeset", ...args], undefined, { CHANGESETS_OUTPUT: outputPath })); | ||
| return fs.existsSync(outputPath) ? fs.readFileSync(outputPath, "utf8") : ""; | ||
| } | ||
|
|
||
| function githubApi(apiPath, method = "GET", body) { | ||
| const args = ["api", "--method", method, `repos/${repository}/${apiPath}`]; | ||
| if (body) args.push("--input", "-"); | ||
| return JSON.parse(run("gh", args, body && JSON.stringify(body))); | ||
| } | ||
|
|
||
| const basicAuth = Buffer.from(`x-access-token:${token}`).toString("base64"); | ||
| console.log(`##vso[task.setsecret]${basicAuth}`); | ||
| function push(args) { | ||
| run("git", ["push", `https://github.com/${repository}.git`, ...args], undefined, { | ||
| GIT_CONFIG_COUNT: "1", | ||
| GIT_CONFIG_KEY_0: "http.https://github.com/.extraheader", | ||
| GIT_CONFIG_VALUE_0: `AUTHORIZATION: basic ${basicAuth}`, | ||
| }); | ||
| } | ||
|
|
||
| // Returns the CHANGELOG.md section for a version, the same section | ||
| // changesets/action uses for version PR bodies and release notes. | ||
| function getChangelogEntry(packageDir, version) { | ||
| const changelogPath = path.join(packageDir, "CHANGELOG.md"); | ||
| if (!fs.existsSync(changelogPath)) return ""; | ||
|
|
||
| let fence; | ||
| let depth; | ||
| const content = []; | ||
| for (const line of fs.readFileSync(changelogPath, "utf8").split(/\r?\n/)) { | ||
| const fenceMatch = /^ {0,3}(`{3,}|~{3,})/.exec(line); | ||
| if (fence) { | ||
| if (fenceMatch && fenceMatch[1][0] === fence[0] && fenceMatch[1].length >= fence.length) fence = undefined; | ||
| } | ||
| else if (fenceMatch) { | ||
| fence = fenceMatch[1]; | ||
| } | ||
| else { | ||
| const heading = /^ {0,3}(#{1,6})(?:[ \t]+(.*?))?(?:[ \t]+#+)?[ \t]*$/.exec(line); | ||
| if (heading) { | ||
| if (depth === undefined) { | ||
| if ((heading[2] ?? "").trim() === version) depth = heading[1].length; | ||
| continue; | ||
| } | ||
| if (heading[1].length <= depth) break; | ||
| } | ||
| } | ||
| if (depth !== undefined) content.push(line); | ||
| } | ||
| return content.join("\n").trim(); | ||
| } | ||
|
|
||
| function getPackageDirs() { | ||
| const packages = JSON.parse(run("pnpm", ["list", "--recursive", "--depth", "-1", "--json"])); | ||
| return new Map(packages.map(pkg => [pkg.name, pkg.path])); | ||
| } | ||
|
|
||
| function tagReleases() { | ||
| const tags = changesetOutput(["git-tag"]).split("\n").filter(Boolean).map(line => JSON.parse(line)); | ||
| if (tags.length === 0) return; | ||
|
|
||
| push(tags.map(({ tag }) => `refs/tags/${tag}`)); | ||
| const packageDirs = getPackageDirs(); | ||
| for (const { tag, packageName } of tags) { | ||
| const version = tag.slice(packageName.length + 1); | ||
| const release = githubApi("releases", "POST", { | ||
| tag_name: tag, | ||
| name: tag, | ||
| body: getChangelogEntry(packageDirs.get(packageName), version), | ||
| prerelease: version.includes("-"), | ||
| }); | ||
| console.log(`Created ${release.html_url}`); | ||
| } | ||
| } | ||
|
|
||
| function createBody(baseBranch, releases, preState, includeEntries) { | ||
| const lines = [ | ||
| `This PR was opened by the Changesets pipeline (\`${pipelinePath}\`). When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to ${baseBranch}, this PR will be updated.`, | ||
| "", | ||
| ]; | ||
| if (preState?.mode === "pre") { | ||
| lines.push( | ||
| `> [!IMPORTANT]`, | ||
| `> \`${baseBranch}\` is in **pre mode**, so this branch contains prereleases. To exit pre mode, run \`changeset pre exit\` on \`${baseBranch}\`.`, | ||
| "", | ||
| ); | ||
| } | ||
| lines.push("# Releases", ""); | ||
| if (!includeEntries) { | ||
| lines.push("> The changelog information of each package has been omitted from this message, as the content exceeds the size limit.", ""); | ||
| } | ||
| for (const { name, newVersion, entry } of releases) { | ||
| lines.push(`## ${name}@${newVersion}`, ""); | ||
| if (includeEntries && entry) lines.push(entry, ""); | ||
| } | ||
| return lines.join("\n"); | ||
| } | ||
|
|
||
| function versionPackages(baseBranch, plan) { | ||
| const versionBranch = `changeset-release/${baseBranch}`; | ||
| const title = plan.preState?.mode === "pre" ? `Version Packages (${plan.preState.tag})` : "Version Packages"; | ||
|
|
||
| run("git", ["switch", "--force-create", versionBranch]); | ||
| process.stdout.write(run("pnpm", ["changeset", "version"])); | ||
| run("git", ["add", "--all"]); | ||
| run("git", ["-c", "core.hooksPath=/dev/null", "commit", "--message", title]); | ||
| push(["--force", `HEAD:refs/heads/${versionBranch}`]); | ||
|
|
||
| const bumpOrder = ["major", "minor", "patch"]; | ||
| const packageDirs = getPackageDirs(); | ||
| const releases = plan.releases | ||
| .filter(release => release.type !== "none") | ||
| .sort((a, b) => bumpOrder.indexOf(a.type) - bumpOrder.indexOf(b.type)) | ||
| .map(release => ({ ...release, entry: getChangelogEntry(packageDirs.get(release.name), release.newVersion) })); | ||
|
|
||
| let body = createBody(baseBranch, releases, plan.preState, true); | ||
| if (body.length > maxBodyLength) body = createBody(baseBranch, releases, plan.preState, false); | ||
|
|
||
| const [owner] = repository.split("/"); | ||
| const query = new URLSearchParams({ state: "open", base: baseBranch, head: `${owner}:${versionBranch}` }); | ||
| const [existing] = githubApi(`pulls?${query}`); | ||
| const pr = existing | ||
| ? githubApi(`pulls/${existing.number}`, "PATCH", { title, body }) | ||
| : githubApi("pulls", "POST", { base: baseBranch, head: versionBranch, title, body }); | ||
| console.log(`${existing ? "Updated" : "Created"} ${pr.html_url}`); | ||
| } | ||
|
|
||
| const { baseBranch } = JSON.parse(fs.readFileSync(".changeset/config.json", "utf8")); | ||
| // `changeset status` compares the working tree against a local base branch. | ||
| run("git", ["switch", "--force-create", baseBranch]); | ||
| const plan = JSON.parse(changesetOutput(["status"])); | ||
|
|
||
| if (plan.changesets.length === 0) { | ||
|
jakebailey marked this conversation as resolved.
Resolved
|
||
| tagReleases(); | ||
| } | ||
| else if (plan.changesets.every(changeset => changeset.releases.length === 0)) { | ||
| console.log("All pending changesets are empty; not creating a version PR."); | ||
| } | ||
| else { | ||
| versionPackages(baseBranch, plan); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| trigger: | ||
| batch: true | ||
| branches: | ||
| include: | ||
| - v2 | ||
| pr: none | ||
|
|
||
| variables: | ||
| - name: TYPESCRIPT_AUTOMATION_GITHUB_APP_CLIENT_ID | ||
| value: Iv23li4GolzJSEp1mzHI | ||
| - name: TYPESCRIPT_AUTOMATION_GITHUB_APP_KEY_ID | ||
| value: https://jststeam-passwords.vault.azure.net/keys/typescript-automation | ||
|
|
||
| jobs: | ||
| - ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/v2') }}: | ||
| - job: Changesets | ||
| displayName: Version or tag packages | ||
| pool: | ||
| vmImage: ubuntu-latest | ||
| steps: | ||
| # The changelog generator reads the commit that added each changeset. | ||
| - checkout: self | ||
| fetchDepth: 0 | ||
| fetchFilter: blob:none | ||
| fetchTags: false | ||
|
|
||
| - task: UseNode@1 | ||
| displayName: Use Node 24.x | ||
| inputs: | ||
| version: 24.x | ||
|
|
||
| - bash: | | ||
| set -euo pipefail | ||
| npm install --global "$(jq -r '.packageManager' package.json)" | ||
| pnpm install --frozen-lockfile | ||
| displayName: Install dependencies | ||
|
|
||
| - template: /build/azure-pipelines/steps/create-github-app-token.yml@self | ||
| parameters: | ||
| azureSubscription: typescript-automation-signing | ||
| repositories: TypeScript-Website | ||
| permissions: contents:write,pull_requests:write | ||
| insertSteps: | ||
| - bash: node build/azure-pipelines/changesets.mjs | ||
| displayName: Version or tag packages | ||
| env: | ||
| GH_TOKEN: $(GH_TOKEN) | ||
| GITHUB_REPOSITORY: microsoft/TypeScript-Website |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.