kraal 0.4.0

A terminal workspace manager for SSH fleets and coding agents
name: Release

# A tag v<version> (the version in Cargo.toml) builds kraal for Linux and macOS, publishes a
# release with one archive per target, its checksum, and SHA256SUMS (install.sh and
# `kraal update` download from there), then publishes the crate on crates.io. Run by hand, it
# builds the archives without publishing anything.

on:
  push:
    tags: ["v*"]
  workflow_dispatch:

permissions:
  contents: write

jobs:
  build:
    strategy:
      fail-fast: true
      matrix:
        include:
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
          - target: aarch64-unknown-linux-musl
            os: ubuntu-latest
          - target: x86_64-apple-darwin
            os: macos-latest
          - target: aarch64-apple-darwin
            os: macos-latest
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
      - name: The tag is the version in Cargo.toml
        if: startsWith(github.ref, 'refs/tags/')
        shell: bash
        run: |
          version=$(sed -n 's/^version = "\(.*\)"$/\1/p' Cargo.toml | head -1)
          if [ "v$version" != "$GITHUB_REF_NAME" ]; then
            echo "tag $GITHUB_REF_NAME, but Cargo.toml says $version"
            exit 1
          fi
      # Static Linux binaries, linked by zig for both architectures. zig and cargo-zigbuild are
      # pinned: zig's libc is part of these binaries, and a new zig changes it without a word
      # (0.16 replaced musl's allocator with its own, which cost every session of 0.3.0 some
      # 10 MB). Another version goes in once a build made with it has been through the benchmark.
      - name: Build (Linux)
        if: runner.os == 'Linux'
        run: |
          pip install ziglang==0.17.0
          cargo install --locked cargo-zigbuild@0.23.4
          cargo zigbuild --release --locked --target ${{ matrix.target }}
      - name: Build (macOS)
        if: runner.os == 'macOS'
        run: cargo build --release --locked --target ${{ matrix.target }}
      - name: The binary runs
        if: matrix.target == 'x86_64-unknown-linux-musl' || matrix.target == 'aarch64-apple-darwin'
        run: target/${{ matrix.target }}/release/kraal --version
      - name: Package
        shell: bash
        run: |
          name=kraal-${{ matrix.target }}
          mkdir -p dist/$name
          cp target/${{ matrix.target }}/release/kraal LICENSE README.md dist/$name/
          tar -C dist -czf dist/$name.tar.gz $name
          (cd dist && shasum -a 256 $name.tar.gz > $name.tar.gz.sha256)
      - uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.target }}
          path: dist/kraal-${{ matrix.target }}.tar.gz*

  release:
    needs: build
    if: startsWith(github.ref, 'refs/tags/')
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v4
        with:
          path: dist
          merge-multiple: true
      - name: Publish
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          cd dist
          cat *.sha256 > SHA256SUMS
          gh release create "$GITHUB_REF_NAME" --repo "$GITHUB_REPOSITORY" --title "$GITHUB_REF_NAME" \
            --generate-notes *.tar.gz *.sha256 SHA256SUMS

  # crates.io trusts this workflow (crate settings, Trusted Publishing): no token is stored, the
  # one used here lasts for the job.
  crates:
    needs: release
    if: startsWith(github.ref, 'refs/tags/')
    runs-on: ubuntu-latest
    permissions:
      contents: read
      id-token: write
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: rust-lang/crates-io-auth-action@v1
        id: auth
      - run: cargo publish --locked
        env:
          CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}