|
| 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 } |
0 commit comments