diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..f833dfd --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,222 @@ +name: Release + +on: + push: + tags: + - 'v*' + # Manual escape hatch if GitHub stops delivering push events: + # gh workflow run release.yml --ref v + workflow_dispatch: + +permissions: + contents: read + +env: + CARGO_TERM_COLOR: always + FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true + +jobs: + # CI only runs on PRs (not pushes to main). For release tags, verify the + # commit is on main and skip CI polling entirely. + wait-for-ci: + name: Verify main branch + runs-on: ubuntu-latest + steps: + - name: Confirm commit is on main + env: + GH_TOKEN: ${{ github.token }} + run: | + SHA="${{ github.sha }}" + REPO="${{ github.repository }}" + MAIN_SHA=$(gh api "repos/$REPO/branches/main" --jq '.commit.sha' 2>/dev/null || echo "") + if [ -z "$MAIN_SHA" ]; then + echo "ERROR: Could not fetch main branch HEAD" + exit 1 + fi + MERGE_BASE=$(gh api "repos/$REPO/compare/${MAIN_SHA}...${SHA}" --jq '.status' 2>/dev/null || echo "error") + case "$MERGE_BASE" in + "identical"|"behind") + echo "Commit $SHA is on main (main is at $MAIN_SHA)" + ;; + *) + if [ "$SHA" = "$MAIN_SHA" ]; then + echo "Commit $SHA is main HEAD" + else + echo "WARNING: Commit $SHA may not be on main (status: $MERGE_BASE). Proceeding anyway." + fi + ;; + esac + echo "Ready to release." + + build-release: + name: Build Release + needs: wait-for-ci + runs-on: ${{ matrix.os }} + permissions: + contents: write + strategy: + fail-fast: false + matrix: + include: + - os: ubuntu-latest + target: x86_64-unknown-linux-gnu + artifact_name: ratifact + asset_suffix: linux-amd64 + + - os: ubuntu-24.04-arm + target: aarch64-unknown-linux-gnu + artifact_name: ratifact + asset_suffix: linux-arm64 + + - os: windows-latest + target: x86_64-pc-windows-msvc + artifact_name: ratifact.exe + asset_suffix: windows-amd64 + + # Both darwin targets build on our own Mac runner. GitHub bills + # macOS minutes at 10x; the Mac runs them for free and natively. + # A tag cannot finish its release while the Mac runner is offline: + # the darwin jobs queue until it comes back rather than failing + # over to a hosted runner. + - os: self-hosted + target: x86_64-apple-darwin + artifact_name: ratifact + asset_suffix: macos-amd64 + + - os: self-hosted + target: aarch64-apple-darwin + artifact_name: ratifact + asset_suffix: macos-arm64 + + steps: + - uses: actions/checkout@v5 + + - name: Set version + id: version + shell: bash + run: echo "version=${GITHUB_REF#refs/tags/v}" >> "$GITHUB_OUTPUT" + + - name: Refuse to administer the host's rustup + # dtolnay/rust-toolchain manages rustup on whatever machine it runs + # on, and defaults CARGO_HOME to ~/.cargo when unset. On a + # self-hosted runner that is somebody's workstation, that already + # destroyed the owner's toolchain once (opencrabs #1162). The runner + # service sets both vars to its own directories; fail if that stops + # being true. + if: runner.environment == 'self-hosted' + shell: bash + run: | + for var in CARGO_HOME RUSTUP_HOME; do + value="${!var}" + if [ -z "$value" ]; then + echo "::error::$var is unset, so rustup would manage the host's own installation. Set it in the runner service environment." + exit 1 + fi + case "$value" in + "$HOME/.cargo"|"$HOME/.rustup") + echo "::error::$var points at the host's own installation ($value). CI must not administer it." + exit 1 + ;; + esac + echo "$var=$value" + done + + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + with: + targets: ${{ matrix.target }} + + - name: Build + shell: bash + env: + CFLAGS: ${{ matrix.target == 'aarch64-apple-darwin' && '-march=armv8-a+crypto' || '' }} + CXXFLAGS: ${{ matrix.target == 'aarch64-apple-darwin' && '-march=armv8-a+crypto' || '' }} + # No --locked: Cargo.lock is gitignored in this repo (*.lock), so a + # fresh dependency resolve is the only option today. Tracking the + # lockfile would allow --locked and reproducible releases; that is a + # separate change. + run: cargo build --release --target ${{ matrix.target }} + + - name: Strip binary (Unix) + if: matrix.os != 'windows-latest' + run: strip target/${{ matrix.target }}/release/${{ matrix.artifact_name }} + + - name: Create archive + shell: bash + run: | + ASSET_NAME="ratifact-v${{ steps.version.outputs.version }}-${{ matrix.asset_suffix }}" + cd target/${{ matrix.target }}/release + if [ "${{ matrix.os }}" = "windows-latest" ]; then + 7z a ../../../${ASSET_NAME}.zip ${{ matrix.artifact_name }} + else + tar czf ../../../${ASSET_NAME}.tar.gz ${{ matrix.artifact_name }} + fi + + - name: Upload artifact + uses: actions/upload-artifact@v6 + with: + name: ratifact-v${{ steps.version.outputs.version }}-${{ matrix.asset_suffix }} + path: | + ratifact-v${{ steps.version.outputs.version }}-${{ matrix.asset_suffix }}.tar.gz + ratifact-v${{ steps.version.outputs.version }}-${{ matrix.asset_suffix }}.zip + + create-release: + name: Create Release + needs: build-release + if: needs.build-release.result == 'success' + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - uses: actions/checkout@v5 + + - name: Extract version from tag + id: version + run: echo "version=${GITHUB_REF#refs/tags/v}" >> "$GITHUB_OUTPUT" + + - name: Extract changelog for this version + id: changelog + run: | + version="${{ steps.version.outputs.version }}" + body=$(awk "/^## \\[${version}\\]/{found=1; next} /^## \\[/{if(found) exit} found{print}" CHANGELOG.md) + echo "$body" > /tmp/release_notes.md + echo "" >> /tmp/release_notes.md + prev_version=$(awk "/^## \\[${version}\\]/{found=1; next} found && /^## \\[/{match(\$0, /\\[([^]]+)\\]/, m); print m[1]; exit}" CHANGELOG.md) + if [ -n "$prev_version" ]; then + echo "**Full Changelog:** https://github.com/${{ github.repository }}/compare/v${prev_version}...v${version}" >> /tmp/release_notes.md + fi + + - name: Download all artifacts + uses: actions/download-artifact@v7 + with: + path: artifacts + + - name: Collect release assets + run: | + mkdir -p release-assets + find artifacts -type f \( -name '*.tar.gz' -o -name '*.zip' \) -exec cp {} release-assets/ \; + ls -la release-assets/ + + - name: Generate checksums + run: | + cd release-assets + sha256sum *.tar.gz *.zip > SHA256SUMS + cat SHA256SUMS + + - name: Create GitHub Release + env: + GH_TOKEN: ${{ github.token }} + run: | + # --clobber so binaries can be attached to a release that was + # already created manually (gh release create). + if gh release view "v${{ steps.version.outputs.version }}" &>/dev/null; then + gh release upload "v${{ steps.version.outputs.version }}" \ + release-assets/* --clobber + gh release edit "v${{ steps.version.outputs.version }}" --draft=false + else + gh release create "v${{ steps.version.outputs.version }}" \ + --title "v${{ steps.version.outputs.version }}" \ + --notes-file /tmp/release_notes.md \ + --latest \ + release-assets/* + fi