croft-software 0.2.3

VSCode-style TUI written in Rust
# Prebuilt release binaries (#376).
#
# `cargo install croft-software` compiles for ten minutes on a laptop and needs
# a Rust toolchain first. This publishes four archives per tag so
# `cargo binstall croft-software` is a download instead, and so the Homebrew
# formula (#375) has something to point at.
#
# The build recipe is deliberately the same one ci.yml's `ship-path build` job
# proved for the musl targets, including its two non-obvious fixes: `rustup
# target add` runs from the checkout so rust-toolchain.toml resolves the
# channel (adding to the default toolchain instead is the bug that job exists
# to catch), and cargo-zigbuild needs `pip install --break-system-packages`
# because recent runner images mark the system Python externally managed.
#
# The profile is `release`, NOT the `remote-fast` profile ci.yml builds.
# remote-fast trades LTO and codegen-units for build speed because a user is
# waiting on it during `croft <host>`; a published artifact is built once and
# run many times, so it takes the slow, fully-optimised profile.
name: release

on:
  push:
    tags:
      - "v*"
  # Building the matrix without publishing, to prove a tag would work before
  # cutting one. Upload only ever happens on a tag.
  workflow_dispatch:

# Read at the top level; only the publish job needs write, and only to
# create the release. A build job that can write contents is a build job that
# can rewrite the repo if any step it runs is compromised.
permissions:
  contents: read

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    name: build (${{ matrix.target }})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          # Apple targets need a macOS runner: zigbuild does not cross to them
          # without an SDK, and shipping an unsigned SDK copy is not something
          # a release job should be doing.
          - target: aarch64-apple-darwin
            os: macos-latest
            cross: false
          - target: x86_64-apple-darwin
            os: macos-latest
            cross: false
          # The musl pair is what ci.yml already builds and what the remote
          # install fast path (#261) consumes.
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
            cross: true
          - target: aarch64-unknown-linux-musl
            os: ubuntu-latest
            cross: true
    steps:
      - uses: actions/checkout@v7
        with:
          # The token is not needed after the fetch; leaving it
          # in .git/config lets any later step push with it.
          persist-credentials: false

      - name: Install the pinned toolchain
        run: rustup show

      - name: Add the target to the PINNED toolchain
        # From the checkout, so rust-toolchain.toml resolves the channel.
        run: rustup target add ${{ matrix.target }}

      - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
        with:
          key: release-${{ matrix.target }}

      - name: Install zig and cargo-zigbuild
        if: matrix.cross
        run: |
          python3 -m pip install --break-system-packages ziglang \
            || python3 -m pip install ziglang
          python3 -m ziglang version
          cargo install cargo-zigbuild --locked

      - name: Build
        run: |
          if [ "${{ matrix.cross }}" = "true" ]; then
            cargo zigbuild --profile release --locked --bin croft \
              --target ${{ matrix.target }}
          else
            cargo build --profile release --locked --bin croft \
              --target ${{ matrix.target }}
          fi

      - name: The artifact must exist and be the right kind of binary
        # A cross build that silently produced a host binary is the failure
        # this catches, and catching it means reading the ARCHITECTURE `file`
        # names, not only the container format. Matching `ELF 64-bit LSB` on
        # both musl legs let an x86-64 binary pass as aarch64 - the comment
        # claimed an assertion the check did not make, which is the same
        # false green as the missing arm below (#551).
        run: |
          set -euo pipefail
          bin="target/${{ matrix.target }}/release/croft"
          test -x "$bin" || { echo "::error::no artifact at $bin"; exit 1; }
          file "$bin"
          # Exact triples, not suffixes: a typo in a matrix target then reaches
          # the catch-all rather than silently matching `*-linux-musl`.
          case "${{ matrix.target }}" in
            x86_64-unknown-linux-musl)  file "$bin" | grep -qE 'ELF 64-bit LSB.*x86-64' ;;
            aarch64-unknown-linux-musl) file "$bin" | grep -qE 'ELF 64-bit LSB.*aarch64' ;;
            x86_64-apple-darwin)        file "$bin" | grep -qE 'Mach-O 64-bit.*(x86_64|x86-64)' ;;
            aarch64-apple-darwin)       file "$bin" | grep -qE 'Mach-O 64-bit.*arm64e?' ;;
            # A `case` with no match exits 0, so without this arm the whole
            # assertion becomes a silent no-op for any target the list does
            # not name - green step, unchecked artifact (#551). Adding a
            # target now has to add its assertion.
            *) echo "::error::no artifact-kind assertion for ${{ matrix.target }}"; exit 1 ;;
          esac

      - name: Package
        # The archive layout binstall resolves against: a single `croft` at the
        # root of a tar.gz named for the target. The crate is croft-software
        # and the binary is croft, so the two names differ on purpose.
        id: package
        run: |
          name="croft-${{ matrix.target }}"
          mkdir -p "dist/$name"
          cp "target/${{ matrix.target }}/release/croft" "dist/$name/croft"
          cp README.md LICENSE "dist/$name/" 2>/dev/null || true
          tar -czf "dist/$name.tar.gz" -C dist "$name"
          echo "archive=dist/$name.tar.gz" >> "$GITHUB_OUTPUT"

      - name: Checksum
        # Per-target sums here, concatenated into one SHA256SUMS by the publish
        # job. Computing them on the runner that built the artifact means the
        # sum covers the bytes that were actually produced.
        run: |
          cd dist
          if command -v sha256sum >/dev/null; then
            sha256sum "croft-${{ matrix.target }}.tar.gz" > "croft-${{ matrix.target }}.tar.gz.sha256"
          else
            shasum -a 256 "croft-${{ matrix.target }}.tar.gz" > "croft-${{ matrix.target }}.tar.gz.sha256"
          fi
          cat "croft-${{ matrix.target }}.tar.gz.sha256"

      - uses: actions/upload-artifact@v7
        with:
          name: croft-${{ matrix.target }}
          path: |
            dist/croft-${{ matrix.target }}.tar.gz
            dist/croft-${{ matrix.target }}.tar.gz.sha256
          if-no-files-found: error

  publish:
    name: publish the release
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write
      # Keyless signing: cosign exchanges the job's OIDC token for a
      # short-lived Sigstore certificate naming this workflow and tag, so
      # there is no signing key to store, rotate or leak.
      id-token: write
    # Only a tag publishes. A workflow_dispatch run builds and stops, which is
    # how you prove the matrix works without cutting a release.
    if: startsWith(github.ref, 'refs/tags/v')
    steps:
      - uses: actions/checkout@v7
        with:
          # The token is not needed after the fetch; leaving it
          # in .git/config lets any later step push with it.
          persist-credentials: false

      - uses: actions/download-artifact@v8
        with:
          path: staged

      - name: Collect the archives and build SHA256SUMS
        run: |
          mkdir -p dist
          find staged -name 'croft-*.tar.gz' -exec cp {} dist/ \;
          cd dist
          # Four archives, or the release is incomplete. Publishing three and
          # a checksum file that says three is worse than failing: binstall
          # would resolve for some users and 404 for others.
          count=$(ls -1 croft-*.tar.gz | wc -l | tr -d ' ')
          if [ "$count" -ne 4 ]; then
            echo "expected 4 archives, found $count:"
            ls -1
            exit 1
          fi
          # Build SHA256SUMS from the sums the BUILD runners computed, not
          # by re-hashing what arrived here. Re-hashing and then checking
          # compares the downloaded bytes against themselves: it passes on a
          # corrupted archive, which is the one thing it exists to catch.
          # The per-target .sha256 files are the only independent evidence,
          # so they are the input.
          cat ../staged/*/croft-*.tar.gz.sha256 > SHA256SUMS
          cat SHA256SUMS
          # `sha256sum -c` verifies the lines that ARE there; it cannot report
          # one that is missing. A leg whose .sha256 never arrived would ship
          # its archive with no sum and this step would stay green (#557).
          # So every archive must be named by exactly one line, and there
          # must be no other lines. A bare line count is not enough: one
          # duplicated sum plus one missing sum still counts to four.
          fail=0
          for a in croft-*.tar.gz; do
            n=$(awk -v f="$a" '$2 == f || $2 == "*" f' SHA256SUMS | wc -l | tr -d ' ')
            if [ "$n" -ne 1 ]; then
              echo "::error::SHA256SUMS names $a $n times, expected once"
              fail=1
            fi
          done
          lines=$(wc -l < SHA256SUMS | tr -d ' ')
          if [ "$lines" -ne "$count" ]; then
            echo "::error::SHA256SUMS has $lines lines for $count archives"
            fail=1
          fi
          [ "$fail" -eq 0 ] || exit 1
          # Now the check has something to disagree with: these sums were
          # produced on the machine that built each artifact, and this runner
          # hashes the bytes that actually arrived.
          sha256sum -c SHA256SUMS

      - uses: sigstore/cosign-installer@6f9f17788090df1f26f669e9d70d6ae9567deba6 # v4.1.2

      # Sign the checksum file, not each archive: every archive is bound to
      # SHA256SUMS by its sum, so one signature covers the four. The bundle
      # carries the signature, the certificate and the transparency-log
      # proof, which is everything `cosign verify-blob` needs offline.
      - name: Sign SHA256SUMS
        run: |
          cosign sign-blob --yes --bundle dist/SHA256SUMS.sigstore.json dist/SHA256SUMS
          # Verify with the same identity a user will check, so a signature
          # that would not verify for them never ships.
          cosign verify-blob \
            --bundle dist/SHA256SUMS.sigstore.json \
            --certificate-identity "https://github.com/${GITHUB_REPOSITORY}/.github/workflows/release.yml@${GITHUB_REF}" \
            --certificate-oidc-issuer https://token.actions.githubusercontent.com \
            dist/SHA256SUMS

      - name: Publish
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          tag="${GITHUB_REF#refs/tags/}"
          version="${tag#v}"
          notes="src/release_notes/$version.md"
          if [ -f "$notes" ]; then
            gh release create "$tag" --title "$tag" --notes-file "$notes" \
              dist/croft-*.tar.gz dist/SHA256SUMS dist/SHA256SUMS.sigstore.json
          else
            # Every shipped version has a notes file by the release-hygiene
            # gate, so a missing one means the tag does not match any version
            # that went through it. Say so rather than inventing a body.
            echo "no src/release_notes/$version.md for tag $tag" >&2
            exit 1
          fi

  homebrew:
    name: bump the Homebrew formula
    # After publish, so the archives the formula points at already exist.
    needs: publish
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')
    steps:
      - uses: actions/checkout@v7
        with:
          persist-credentials: false

      - name: Render the formula from the published SHA256SUMS
        # The release's own SHA256SUMS, the file cosign signed, so the formula
        # cannot vouch for bytes the release does not. The generator refuses
        # a SHA256SUMS missing any of the four archives (#375).
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          tag="${GITHUB_REF#refs/tags/}"
          gh release download "$tag" --pattern SHA256SUMS --dir dist
          python3 scripts/homebrew_formula.py "${tag#v}" dist/SHA256SUMS > croft.rb
          ruby -c croft.rb
          cat croft.rb

      - name: Open a pull request against the tap
        # The tap is its own repository, so github.token cannot push to it:
        # HOMEBREW_TAP_TOKEN is a fine-grained token with contents and pull
        # requests write on vitali87/homebrew-croft only. Until it is set the
        # release still ships; this says what is missing instead of failing
        # a release whose archives are already public.
        env:
          TAP: vitali87/homebrew-croft
          GH_TOKEN: ${{ secrets.HOMEBREW_TAP_TOKEN }}
        run: |
          if [ -z "$GH_TOKEN" ]; then
            echo "::warning::HOMEBREW_TAP_TOKEN is not set; the formula for ${GITHUB_REF#refs/tags/} was rendered above but not proposed to $TAP"
            exit 0
          fi
          version="${GITHUB_REF#refs/tags/v}"
          branch="croft-$version"
          gh repo clone "$TAP" tap -- --depth 1
          cd tap
          mkdir -p Formula
          cp ../croft.rb Formula/croft.rb
          if git diff --quiet -- Formula/croft.rb && git ls-files --error-unmatch Formula/croft.rb >/dev/null 2>&1; then
            echo "Formula/croft.rb already describes $version"
            exit 0
          fi
          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git checkout -b "$branch"
          git add Formula/croft.rb
          git commit -m "croft $version"
          # gh's credential helper, not a token in the remote URL, so the
          # token never lands in .git/config or the log.
          gh auth setup-git
          git push origin "$branch"
          gh pr create --repo "$TAP" --head "$branch" \
            --title "croft $version" \
            --body "Formula for croft $version, rendered by vitali87/croft's release workflow from the release's signed SHA256SUMS."