croft 0.1.0

The dev workspace you tend: one config for Docker Compose, scripts, hooks, and agents
Documentation
name: Release

on:
  push:
    tags: ["v[0-9]+.[0-9]+.[0-9]+*"]

permissions:
  contents: write
  models: read

jobs:
  build:
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
          - target: x86_64-apple-darwin
            os: macos-latest
          - target: aarch64-apple-darwin
            os: macos-latest

    outputs:
      is_prerelease: ${{ steps.detect.outputs.is_prerelease }}
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v6

      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Install cross-compilation tools
        if: matrix.target == 'aarch64-unknown-linux-gnu'
        run: |
          sudo apt-get update
          sudo apt-get install -y gcc-aarch64-linux-gnu
          echo "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc" >> "$GITHUB_ENV"

      - name: Set version from tag
        id: detect
        run: |
          VERSION="${GITHUB_REF_NAME#v}"
          # Patches `[package] version = "..."` in the root Cargo.toml.
          # BSD sed (macOS) requires a backup extension with -i; GNU sed does not.
          if [[ "$OSTYPE" == darwin* ]]; then
            sed -i '' "1,/^version = .*/s/^version = .*/version = \"${VERSION}\"/" Cargo.toml
          else
            sed -i "1,/^version = .*/s/^version = .*/version = \"${VERSION}\"/" Cargo.toml
          fi

          # Detect pre-release suffix (e.g. -alpha, -beta.1, -rc.2)
          if [[ "$VERSION" == *-* ]]; then
            echo "IS_PRERELEASE=true" >> "$GITHUB_ENV"
            echo "is_prerelease=true" >> "$GITHUB_OUTPUT"
          else
            echo "IS_PRERELEASE=false" >> "$GITHUB_ENV"
            echo "is_prerelease=false" >> "$GITHUB_OUTPUT"
          fi

      - name: Build
        # `[profile.release] strip = "symbols"` in the workspace Cargo.toml
        # handles stripping for us, so no separate strip step is needed.
        run: cargo build --release --target ${{ matrix.target }} --bin croft

      - name: Package
        run: |
          ARCHIVE="croft-${{ matrix.target }}.tar.gz"
          tar czf "$ARCHIVE" -C "target/${{ matrix.target }}/release" croft
          echo "ARCHIVE=$ARCHIVE" >> "$GITHUB_ENV"

      - name: Upload artifact
        uses: actions/upload-artifact@v6
        with:
          name: croft-${{ matrix.target }}
          path: ${{ env.ARCHIVE }}

      - name: Upload patched manifest
        if: matrix.target == 'x86_64-unknown-linux-gnu'
        uses: actions/upload-artifact@v6
        with:
          name: patched-manifest
          path: Cargo.toml

  release:
    needs: build
    runs-on: ubuntu-latest
    env:
      FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - uses: actions/download-artifact@v7
        with:
          path: artifacts
          merge-multiple: true

      - name: Generate checksums
        run: |
          cd artifacts
          sha256sum *.tar.gz > SHA256SUMS

      - name: Generate release notes
        uses: nsrosenqvist/chronikl@v1.1.0
        with:
          output: release_notes.md
          voice: prose
          rich_context: 'true'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          CHRONIKL_PROVIDER: github-models
          CHRONIKL_MODEL: openai/gpt-4o-mini

      - name: Create release
        uses: softprops/action-gh-release@v2
        with:
          body_path: release_notes.md
          prerelease: ${{ needs.build.outputs.is_prerelease == 'true' }}
          files: |
            artifacts/*.tar.gz
            artifacts/SHA256SUMS

  publish:
    needs: [release, build]
    if: needs.build.outputs.is_prerelease != 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Download patched manifest
        uses: actions/download-artifact@v7
        with:
          name: patched-manifest

      - uses: dtolnay/rust-toolchain@stable

      - name: Publish croft to crates.io
        # The version in Cargo.toml was patched by the build job and
        # downloaded over the checked-out tree, leaving the working
        # copy dirty — hence --allow-dirty. `cargo publish` is
        # idempotent only insofar as it refuses to re-publish an
        # existing version; a manual re-run after a successful publish
        # will fail loudly, which is what we want.
        run: cargo publish --allow-dirty
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

  update-homebrew:
    needs: [release, build]
    if: needs.build.outputs.is_prerelease != 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
        with:
          repository: nsrosenqvist/homebrew-croft
          token: ${{ secrets.HOMEBREW_TAP_TOKEN }}

      - name: Update formula
        run: |
          VERSION="${GITHUB_REF_NAME#v}"
          TAG="${GITHUB_REF_NAME}"
          FORMULA="Formula/croft.rb"

          # Download the checksums file from the release
          curl -sSfL "https://github.com/nsrosenqvist/croft/releases/download/${TAG}/SHA256SUMS" -o SHA256SUMS

          # Extract per-target checksums
          SHA_MACOS_ARM=$(grep 'aarch64-apple-darwin' SHA256SUMS | awk '{print $1}')
          SHA_MACOS_X86=$(grep 'x86_64-apple-darwin' SHA256SUMS | awk '{print $1}')
          SHA_LINUX_ARM=$(grep 'aarch64-unknown-linux-gnu' SHA256SUMS | awk '{print $1}')
          SHA_LINUX_X86=$(grep 'x86_64-unknown-linux-gnu' SHA256SUMS | awk '{print $1}')

          # Update version
          sed -i "s/version \".*\"/version \"${VERSION}\"/" "$FORMULA"

          # Replace sha256 values in order of appearance in the formula:
          # 1. macOS ARM, 2. macOS Intel, 3. Linux ARM, 4. Linux Intel
          python3 -c "
          import re
          shas = ['${SHA_MACOS_ARM}', '${SHA_MACOS_X86}', '${SHA_LINUX_ARM}', '${SHA_LINUX_X86}']
          with open('${FORMULA}') as f:
              content = f.read()
          i = 0
          def replace_sha(m):
              global i
              s = 'sha256 \"' + shas[i] + '\"'
              i += 1
              return s
          content = re.sub(r'sha256 \"[a-fA-F0-9]+\"', replace_sha, content)
          # Also handle PLACEHOLDER values (first-time setup)
          content = re.sub(r'sha256 \"PLACEHOLDER\"', replace_sha, content)
          with open('${FORMULA}', 'w') as f:
              f.write(content)
          "

      - name: Commit and push
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add Formula/croft.rb
          git diff --cached --quiet && echo "No changes" && exit 0
          git commit -m "croft ${GITHUB_REF_NAME}"
          git push

  update-floating-tags:
    needs: [release, build]
    if: needs.build.outputs.is_prerelease != 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Update floating major and minor version tags
        run: |
          VERSION="${GITHUB_REF_NAME#v}"
          MAJOR="v${VERSION%%.*}"
          MINOR="v${VERSION%.*}"

          for TAG in "$MAJOR" "$MINOR"; do
            git tag -f "$TAG" "$GITHUB_REF_NAME"
            git push origin "$TAG" --force
          done

  notify-web:
    # Fires croft-web's `release-published` dispatch with the new tag in
    # the payload so the site rebuilds with the current version baked in.
    # Prerelease-gated to match the other publish jobs above.
    needs: [release, build]
    if: needs.build.outputs.is_prerelease != 'true'
    runs-on: ubuntu-latest
    steps:
      - name: Trigger croft-web deploy
        run: |
          curl -sSf -X POST \
            -H "Authorization: Bearer ${{ secrets.CROFT_WEB_DISPATCH_PAT }}" \
            -H "Accept: application/vnd.github+json" \
            -H "X-GitHub-Api-Version: 2022-11-28" \
            https://api.github.com/repos/nsrosenqvist/croft-web/dispatches \
            -d "{\"event_type\":\"release-published\",\"client_payload\":{\"tag\":\"${GITHUB_REF_NAME}\"}}"