ci: derive package version from release tag automatically #8
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
| name: Release | ||
|
Check failure on line 1 in .github/workflows/release.yml
|
||
| on: | ||
| # Create a release in the GitHub UI (New release → choose/publish a tag) and | ||
| # this workflow builds the binaries and attaches them to that release. | ||
| # Draft the release first, fill in the notes, then hit “Publish release”. | ||
| release: | ||
| types: [published] | ||
| # Manual fallback: run the workflow from the Actions tab with a version. | ||
| workflow_dispatch: | ||
| inputs: | ||
| version: | ||
| description: "Release tag to publish (for example btch-dev@1.0.2)" | ||
| required: true | ||
| permissions: | ||
| contents: write | ||
| jobs: | ||
| build: | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| include: | ||
| # Modern CPUs (AVX2). install.sh picks this when the CPU supports it. | ||
| - runner: ubuntu-latest | ||
| build_output: btch | ||
| asset_name: btch-linux-x64 | ||
| compile_target: bun-linux-x64 | ||
| # Baseline target for older CPUs (pre-AVX2) such as Sandy Bridge VMs | ||
| # that would otherwise crash with SIGILL. install.sh picks this when | ||
| # the CPU lacks AVX2. | ||
| - runner: ubuntu-latest | ||
| build_output: btch-baseline | ||
| asset_name: btch-linux-x64-baseline | ||
| compile_target: bun-linux-x64-baseline | ||
| - runner: macos-latest | ||
| build_output: btch | ||
| asset_name: btch-darwin-arm64 | ||
| compile_target: bun-darwin-arm64 | ||
| - runner: windows-latest | ||
| build_output: btch.exe | ||
| asset_name: btch-windows-x64.exe | ||
| compile_target: bun-windows-x64 | ||
| runs-on: ${{ matrix.runner }} | ||
| permissions: | ||
| contents: read | ||
| id-token: write | ||
| attestations: write | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - name: Set package version from release tag | ||
| shell: bash | ||
| run: | | ||
| if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then | ||
| tag="${{ github.event.inputs.version }}" | ||
| else | ||
| tag="${{ github.event.release.tag_name }}" | ||
| fi | ||
| version="${tag#btch-dev@}" | ||
| node -e "const fs=require('fs');const p=JSON.parse(fs.readFileSync('package.json','utf8'));p.version=process.argv[1];fs.writeFileSync('package.json',JSON.stringify(p,null,2)+'\n')" "$version" | ||
| echo "package.json version set to ${version}" | ||
| - name: Setup Bun | ||
| uses: oven-sh/setup-bun@v2 | ||
| with: | ||
| bun-version: 1.3.11 | ||
| - name: Install dependencies | ||
| run: bun install --frozen-lockfile | ||
| - name: Compile standalone binary | ||
| run: bun build --compile --target="${{ matrix.compile_target }}" --outfile "release/${{ matrix.build_output }}" ./src/index.ts | ||
| - name: Compute checksum (unix) | ||
| if: runner.os != 'Windows' | ||
| shell: bash | ||
| working-directory: release | ||
| run: | | ||
| cp "${{ matrix.build_output }}" "${{ matrix.asset_name }}" | ||
| shasum -a 256 "${{ matrix.asset_name }}" > "${{ matrix.asset_name }}.sha256" | ||
| - name: Compute checksum (windows) | ||
| if: runner.os == 'Windows' | ||
| shell: pwsh | ||
| working-directory: release | ||
| run: | | ||
| Copy-Item "${{ matrix.build_output }}" "${{ matrix.asset_name }}" | ||
| $hash = (Get-FileHash "${{ matrix.asset_name }}" -Algorithm SHA256).Hash.ToLower() | ||
| "$hash ${{ matrix.asset_name }}" | Out-File -Encoding ascii "${{ matrix.asset_name }}.sha256" | ||
| - name: Attest build provenance | ||
| uses: actions/attest-build-provenance@v2 | ||
| with: | ||
| subject-path: | | ||
| release/${{ matrix.asset_name }} | ||
| - name: Upload release artifact | ||
| uses: actions/upload-artifact@v4 | ||
| with: | ||
| name: ${{ matrix.asset_name }} | ||
| path: | | ||
| release/${{ matrix.asset_name }} | ||
| release/${{ matrix.asset_name }}.sha256 | ||
| publish: | ||
| needs: build | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - name: Resolve release tag | ||
| shell: bash | ||
| run: | | ||
| if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then | ||
| tag="${{ github.event.inputs.version }}" | ||
| else | ||
| tag="${{ github.event.release.tag_name }}" | ||
| fi | ||
| echo "RELEASE_TAG=${tag}" >> "$GITHUB_ENV" | ||
| - name: Download all build artifacts | ||
| uses: actions/download-artifact@v4 | ||
| with: | ||
| path: release-assets | ||
| - name: Merge checksums | ||
| shell: bash | ||
| run: | | ||
| cat release-assets/**/*.sha256 | sort > release-assets/checksums.txt | ||
| - name: Publish GitHub Release | ||
| uses: softprops/action-gh-release@v2 | ||
| with: | ||
| # When triggered by the `release` event the release already exists, so | ||
| # just attach the binaries. Manual dispatch creates it with notes. | ||
| tag_name: ${{ env.RELEASE_TAG }} | ||
| generate_release_notes: ${{ github.event_name != 'release' }} | ||
| prerelease: ${{ contains(env.RELEASE_TAG, '-rc') || contains(env.RELEASE_TAG, '-alpha') || contains(env.RELEASE_TAG, '-beta') || contains(env.RELEASE_TAG, '-pre') }} | ||
| files: | | ||
| release-assets/**/btch-linux-x64 | ||
| release-assets/**/btch-linux-x64-baseline | ||
| release-assets/**/btch-darwin-arm64 | ||
| release-assets/**/btch-windows-x64.exe | ||
| release-assets/checksums.txt | ||
| # Publish the package to npm on every GitHub release. Requires the | ||
| # NPM_TOKEN secret (Settings → Secrets and variables → Actions) with | ||
| # publish permissions. The version is taken from the release tag, so | ||
| # just create a release like btch-dev@1.0.4 and the package is | ||
| # published as 1.0.4 automatically. | ||
| npm-publish: | ||
| if: github.event_name == 'release' && secrets.NPM_TOKEN != '' | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - name: Set package version from release tag | ||
| shell: bash | ||
| run: | | ||
| tag="${{ github.event.release.tag_name }}" | ||
| version="${tag#btch-dev@}" | ||
| node -e "const fs=require('fs');const p=JSON.parse(fs.readFileSync('package.json','utf8'));p.version=process.argv[1];fs.writeFileSync('package.json',JSON.stringify(p,null,2)+'\n')" "$version" | ||
| echo "package.json version set to ${version}" | ||
| - uses: actions/setup-node@v4 | ||
| with: | ||
| node-version: 22 | ||
| registry-url: https://registry.npmjs.org | ||
| - name: Setup Bun | ||
| uses: oven-sh/setup-bun@v2 | ||
| with: | ||
| bun-version: 1.3.11 | ||
| - name: Install dependencies | ||
| run: bun install --frozen-lockfile | ||
| - name: Build | ||
| run: bun run build | ||
| - name: Publish to npm | ||
| run: npm publish --ignore-scripts --access public | ||
| env: | ||
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | ||