Skip to content

Commit 43efabd

Browse files
committed
chore: release only when the CLI version is newer
Replace CI with a release workflow that compares the CLI version against the latest GitHub release before building and publishing. Remove separate mod compatibility jobs and update the release documentation.
1 parent 53439e2 commit 43efabd

4 files changed

Lines changed: 125 additions & 166 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 0 additions & 163 deletions
This file was deleted.

‎.github/workflows/release.yml‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
branches: [main]
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: read
10+
11+
concurrency:
12+
group: github-release
13+
cancel-in-progress: false
14+
15+
jobs:
16+
version:
17+
if: github.ref == 'refs/heads/main'
18+
runs-on: ubuntu-latest
19+
outputs:
20+
needed: ${{ steps.version.outputs.needed }}
21+
version: ${{ steps.version.outputs.version }}
22+
steps:
23+
- uses: actions/checkout@v7
24+
with:
25+
persist-credentials: false
26+
- uses: actions/setup-node@v7
27+
with:
28+
node-version: '24'
29+
package-manager-cache: false
30+
- name: Compare with the latest GitHub release
31+
id: version
32+
env:
33+
GH_TOKEN: ${{ github.token }}
34+
run: |
35+
node --input-type=module <<'NODE'
36+
import { appendFileSync, readFileSync } from 'node:fs';
37+
const pkg = JSON.parse(readFileSync('cli/package.json', 'utf8'));
38+
if (pkg.name !== '@mizius/minecraft') throw new Error('Unexpected package name.');
39+
const stable = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/;
40+
let needed = false;
41+
if (stable.test(pkg.version)) {
42+
const response = await fetch(process.env.GITHUB_API_URL + '/repos/' + process.env.GITHUB_REPOSITORY + '/releases/latest', {
43+
headers: { Authorization: 'Bearer ' + process.env.GH_TOKEN, Accept: 'application/vnd.github+json' }
44+
});
45+
if (response.status === 404) {
46+
needed = true;
47+
} else {
48+
if (!response.ok) throw new Error('Cannot read latest release. HTTP ' + response.status);
49+
const release = await response.json();
50+
const latest = release.tag_name.replace(/^v/, '');
51+
if (!stable.test(latest)) throw new Error('Latest release must use a stable version tag.');
52+
const currentParts = pkg.version.split('.').map(BigInt);
53+
const latestParts = latest.split('.').map(BigInt);
54+
const difference = currentParts.findIndex((part, index) => part !== latestParts[index]);
55+
needed = difference >= 0 && currentParts[difference] > latestParts[difference];
56+
}
57+
}
58+
console.log(needed ? 'A new release is needed.' : 'No new stable release is needed.');
59+
appendFileSync(process.env.GITHUB_OUTPUT, 'needed=' + needed + '\nversion=' + pkg.version + '\n');
60+
NODE
61+
62+
release:
63+
name: Build and publish release
64+
needs: version
65+
if: needs.version.outputs.needed == 'true'
66+
permissions:
67+
contents: write
68+
runs-on: windows-latest
69+
defaults:
70+
run:
71+
working-directory: cli
72+
steps:
73+
- uses: actions/checkout@v7
74+
with:
75+
fetch-depth: 0
76+
persist-credentials: false
77+
- uses: actions/setup-node@v7
78+
with:
79+
node-version: '24'
80+
package-manager-cache: false
81+
- uses: actions/setup-java@v6
82+
with:
83+
distribution: temurin
84+
java-version: '25'
85+
- run: npm ci
86+
- run: npm test
87+
- name: Build the distributable npm package
88+
run: |
89+
New-Item -ItemType Directory -Path artifacts/npm -Force | Out-Null
90+
npm pack --pack-destination artifacts/npm
91+
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
92+
- name: Verify the packed CLI can run
93+
run: |
94+
$package = @(Get-ChildItem artifacts/npm/*.tgz)
95+
if ($package.Count -ne 1) { throw 'Expected exactly one npm package.' }
96+
$installation = Join-Path $env:RUNNER_TEMP 'minecraft-package-smoke'
97+
npm install --prefix $installation --ignore-scripts --no-audit --no-fund $package[0].FullName
98+
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
99+
& (Join-Path $installation 'node_modules/.bin/minecraft.cmd') --help
100+
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
101+
& (Join-Path $installation 'node_modules/.bin/minecraft.cmd') versions --json
102+
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
103+
- name: Publish the tested package as a GitHub release
104+
env:
105+
GH_TOKEN: ${{ github.token }}
106+
GH_REPO: ${{ github.repository }}
107+
RELEASE_VERSION: ${{ needs.version.outputs.version }}
108+
RELEASE_COMMIT: ${{ github.sha }}
109+
run: |
110+
$package = @(Get-ChildItem artifacts/npm/*.tgz)
111+
if ($package.Count -ne 1) { throw 'Expected exactly one npm package.' }
112+
$manifest = tar -xOf $package[0].FullName package/package.json
113+
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
114+
$metadata = $manifest | ConvertFrom-Json
115+
if ($metadata.name -ne '@mizius/minecraft' -or $metadata.version -ne $env:RELEASE_VERSION) {
116+
throw 'Packed package does not match the release version.'
117+
}
118+
git show-ref --verify --quiet "refs/tags/v$env:RELEASE_VERSION"
119+
if ($LASTEXITCODE -eq 0) { throw 'Release tag already exists. Bump the package version again.' }
120+
if ($LASTEXITCODE -ne 1) { throw 'Could not check the release tag.' }
121+
gh release create "v$env:RELEASE_VERSION" $package[0].FullName --target $env:RELEASE_COMMIT --title "Minecraft CLI v$env:RELEASE_VERSION" --generate-notes --latest
122+
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ npm pack
4545

4646
## Releases
4747

48-
Pushes to every branch run the Windows CLI tests, mod compatibility builds, and package installation checks through `ci.yml`. When a push to `main` increases the stable version in `cli/package.json`, the same workflow creates a GitHub release tagged `v<version>` and attaches the tested `.tgz` package after all checks pass.
48+
The `release.yml` workflow runs on pushes to `main` and can also run manually on `main`. It compares the stable version in `cli/package.json` with the latest GitHub release. If the version is newer, or no release exists, it builds and publishes a release tagged `v<version>` with the packaged `.tgz` attached. Otherwise it skips the build.
4949

5050
See [the CLI release instructions](cli/README.md#publishing) for version updates and release permissions. Publishing and installing public releases do not require an npm account.

‎cli/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ Run the CLI tests with `npm test`.
9696

9797
## Publishing
9898

99-
The `ci.yml` GitHub Actions workflow creates a GitHub release when a push to `main` increases the stable version in `cli/package.json`. Changes to other package fields and prerelease versions do not publish. Version decreases fail the release check.
99+
The `release.yml` workflow runs on pushes to `main` and supports manual runs on `main` through GitHub Actions. It compares `cli/package.json` with the latest published GitHub release. A newer stable version triggers a build. If there is no release yet, it publishes the first stable version. Equal versions, older versions, and prerelease versions skip the build. Release lookup errors fail the workflow.
100100

101101
From the `cli` directory, update both version files without creating a local Git tag.
102102

@@ -106,6 +106,6 @@ npm version patch --no-git-tag-version
106106

107107
Use `minor` or `major` instead of `patch` when appropriate. Commit `package.json` and `package-lock.json`, then merge into `main`.
108108

109-
The workflow runs the Windows CLI tests, both mod compatibility builds, and package installation checks. After they pass, it creates the tag `v<version>` at the tested commit and attaches the tested `mizius-minecraft-<version>.tgz` archive without rebuilding it. GitHub generates the release notes. Existing release tags must not be reused.
109+
When a release is needed, the workflow runs the Windows CLI tests, packages the CLI, and checks installation. There are no separate mod compatibility jobs. Packaging still builds the mod JAR that the CLI bundles. After they pass, it creates the tag `v<version>` at the tested commit and attaches the tested `mizius-minecraft-<version>.tgz` archive without rebuilding it. GitHub generates the release notes. Existing release tags must not be reused.
110110

111111
Publishing uses the workflow's built-in `GITHUB_TOKEN` with `contents: write`. The repository must allow that permission and tag creation. No `NPM_TOKEN`, npm trusted publisher, or additional secret is needed. The old npm publishing workflow has been removed. Use a public repository so users can download release assets without authentication.

0 commit comments

Comments
 (0)