-
Notifications
You must be signed in to change notification settings - Fork 0
Add versioned release workflow #50
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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,143 @@ | ||
| import { readFile, writeFile } from "node:fs/promises"; | ||
|
|
||
| const bump = process.argv[2]; | ||
| const supportedBumps = new Set(["patch", "minor", "major"]); | ||
|
|
||
| if (!supportedBumps.has(bump)) { | ||
| throw new Error(`Expected patch, minor, or major; received: ${bump ?? "<none>"}`); | ||
| } | ||
|
|
||
| const packagePaths = [ | ||
| "package.json", | ||
| "bandersnatch/package.json", | ||
| "bandersnatch/npm/darwin-arm64/package.json", | ||
| "bandersnatch/npm/linux-x64-gnu/package.json", | ||
| "native/package.json", | ||
| ]; | ||
| const cargoManifestPaths = [ | ||
| "bandersnatch/core/Cargo.toml", | ||
| "bandersnatch/native-binding/Cargo.toml", | ||
| "bandersnatch/wasm-binding/Cargo.toml", | ||
| ]; | ||
| const nativePackages = [ | ||
| "@typeberry/bandersnatch-native-darwin-arm64", | ||
| "@typeberry/bandersnatch-native-linux-x64-gnu", | ||
| ]; | ||
|
|
||
| const rootPackage = JSON.parse(await readFile("package.json", "utf8")); | ||
| const currentVersion = rootPackage.version; | ||
| const versionParts = currentVersion.match(/^(\d+)\.(\d+)\.(\d+)$/); | ||
|
|
||
| if (!versionParts) { | ||
| throw new Error(`Root package version is not a stable semantic version: ${currentVersion}`); | ||
| } | ||
|
|
||
| let [, major, minor, patch] = versionParts.map(Number); | ||
|
|
||
| if (bump === "major") { | ||
| major += 1; | ||
| minor = 0; | ||
| patch = 0; | ||
| } else if (bump === "minor") { | ||
| minor += 1; | ||
| patch = 0; | ||
| } else { | ||
| patch += 1; | ||
| } | ||
|
|
||
| const nextVersion = `${major}.${minor}.${patch}`; | ||
|
|
||
| function assertCurrentVersion(actual, location) { | ||
| if (actual !== currentVersion) { | ||
| throw new Error( | ||
| `Expected ${location} to use ${currentVersion}, but found ${actual}`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| function updateNativeDependencies(pkg, location) { | ||
| for (const dependency of nativePackages) { | ||
| if (pkg.optionalDependencies?.[dependency] !== undefined) { | ||
| assertCurrentVersion( | ||
| pkg.optionalDependencies[dependency], | ||
| `${location} optional dependency ${dependency}`, | ||
| ); | ||
| pkg.optionalDependencies[dependency] = nextVersion; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (const packagePath of packagePaths) { | ||
| const pkg = JSON.parse(await readFile(packagePath, "utf8")); | ||
| assertCurrentVersion(pkg.version, packagePath); | ||
| pkg.version = nextVersion; | ||
| updateNativeDependencies(pkg, packagePath); | ||
| await writeFile(packagePath, `${JSON.stringify(pkg, null, 2)}\n`); | ||
| } | ||
|
|
||
| const packageLockPath = "package-lock.json"; | ||
| const packageLock = JSON.parse(await readFile(packageLockPath, "utf8")); | ||
| const lockPackages = [ | ||
| "", | ||
| "bandersnatch", | ||
| "bandersnatch/npm/darwin-arm64", | ||
| "bandersnatch/npm/linux-x64-gnu", | ||
| "native", | ||
| ]; | ||
|
|
||
| assertCurrentVersion(packageLock.version, `${packageLockPath} root`); | ||
| packageLock.version = nextVersion; | ||
|
|
||
| for (const packagePath of lockPackages) { | ||
| const pkg = packageLock.packages?.[packagePath]; | ||
| if (!pkg) { | ||
| throw new Error(`Missing ${packageLockPath} entry: ${packagePath || "<root>"}`); | ||
| } | ||
|
|
||
| assertCurrentVersion( | ||
| pkg.version, | ||
| `${packageLockPath} entry ${packagePath || "<root>"}`, | ||
| ); | ||
| pkg.version = nextVersion; | ||
| updateNativeDependencies( | ||
| pkg, | ||
| `${packageLockPath} entry ${packagePath || "<root>"}`, | ||
| ); | ||
| } | ||
|
|
||
| await writeFile(packageLockPath, `${JSON.stringify(packageLock, null, 2)}\n`); | ||
|
|
||
| for (const manifestPath of cargoManifestPaths) { | ||
| let manifest = await readFile(manifestPath, "utf8"); | ||
| const versionLine = `version = "${currentVersion}"`; | ||
|
|
||
| if (!manifest.includes(versionLine)) { | ||
| throw new Error(`Expected ${manifestPath} to contain ${versionLine}`); | ||
| } | ||
|
|
||
| manifest = manifest.replace(versionLine, `version = "${nextVersion}"`); | ||
| await writeFile(manifestPath, manifest); | ||
| } | ||
|
|
||
| const cargoLockPath = "bandersnatch/Cargo.lock"; | ||
| let cargoLock = await readFile(cargoLockPath, "utf8"); | ||
|
|
||
| for (const packageName of [ | ||
| "bandersnatch-core", | ||
| "bandersnatch-native", | ||
| "bandersnatch-wasm", | ||
| ]) { | ||
| const packageVersion = `name = "${packageName}"\nversion = "${currentVersion}"`; | ||
|
|
||
| if (!cargoLock.includes(packageVersion)) { | ||
| throw new Error(`Expected ${cargoLockPath} to contain ${packageVersion}`); | ||
| } | ||
|
|
||
| cargoLock = cargoLock.replace( | ||
| packageVersion, | ||
| `name = "${packageName}"\nversion = "${nextVersion}"`, | ||
| ); | ||
| } | ||
|
|
||
| await writeFile(cargoLockPath, cargoLock); | ||
| process.stdout.write(nextVersion); |
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
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,92 @@ | ||
| name: Prepare release | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
| inputs: | ||
| bump: | ||
| description: Version bump type | ||
| required: true | ||
| default: patch | ||
| type: choice | ||
| options: | ||
| - patch | ||
| - minor | ||
| - major | ||
|
|
||
| concurrency: | ||
| group: prepare-release | ||
| cancel-in-progress: false | ||
|
|
||
| jobs: | ||
| prepare: | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: write | ||
| pull-requests: write | ||
|
|
||
| steps: | ||
| - name: Checkout main | ||
| uses: actions/checkout@v6 | ||
| with: | ||
| ref: main | ||
|
|
||
| - name: Use Node.js | ||
| uses: actions/setup-node@v6 | ||
| with: | ||
| node-version: 24 | ||
|
|
||
| - name: Bump versions | ||
| id: version | ||
| run: | | ||
| version=$(node .github/scripts/bump-version.mjs "${{ inputs.bump }}") | ||
| echo "version=$version" >> "$GITHUB_OUTPUT" | ||
|
|
||
| - name: Validate version files | ||
| run: | | ||
| cargo metadata \ | ||
| --manifest-path bandersnatch/Cargo.toml \ | ||
| --format-version 1 \ | ||
| --locked \ | ||
| --no-deps >/dev/null | ||
| node -e "JSON.parse(require('fs').readFileSync('package-lock.json'))" | ||
|
|
||
| - name: Create GitHub App token | ||
| id: app-token | ||
| uses: actions/create-github-app-token@v2 | ||
| with: | ||
| app-id: ${{ vars.PR_APP_ID }} | ||
| private-key: ${{ secrets.PR_APP_PRIVATE_KEY }} | ||
|
|
||
| - name: Create version bump pull request | ||
| uses: peter-evans/create-pull-request@v8 | ||
| with: | ||
| token: ${{ steps.app-token.outputs.token }} | ||
| base: main | ||
| branch: release/v${{ steps.version.outputs.version }} | ||
| delete-branch: true | ||
| commit-message: "Bump version to ${{ steps.version.outputs.version }}" | ||
| title: "Bump version to ${{ steps.version.outputs.version }}" | ||
| body: | | ||
| Bumps the release version to `${{ steps.version.outputs.version }}`. | ||
|
|
||
| After merging this PR, publish the draft `v${{ steps.version.outputs.version }}` release. | ||
|
|
||
| - name: Create draft GitHub release | ||
| uses: softprops/action-gh-release@v2 | ||
| with: | ||
| tag_name: v${{ steps.version.outputs.version }} | ||
| name: v${{ steps.version.outputs.version }} | ||
| target_commitish: main | ||
| generate_release_notes: true | ||
| draft: true | ||
| prerelease: false | ||
|
|
||
| - name: Write summary | ||
| run: | | ||
| { | ||
| echo "## Release v${{ steps.version.outputs.version }} prepared" | ||
| echo | ||
| echo "1. Merge \`release/v${{ steps.version.outputs.version }}\`." | ||
| echo "2. Publish the draft \`v${{ steps.version.outputs.version }}\` release." | ||
| echo "3. The publish workflow will publish npm version \`${{ steps.version.outputs.version }}-<short-sha>\`." | ||
| } >> "$GITHUB_STEP_SUMMARY" | ||
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.