hostab 0.0.2

Your dev tool to manage /etc/hosts like a pro — written in Rust
Documentation
name: Release

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    name: Build (${{ matrix.target }})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          # Linux x86_64 (glibc)
          - os: ubuntu-24.04
            target: x86_64-unknown-linux-gnu
            cross: false

          # Linux x86_64 static (musl)
          - os: ubuntu-24.04
            target: x86_64-unknown-linux-musl
            cross: true

          # Linux aarch64 static (musl)
          - os: ubuntu-24.04
            target: aarch64-unknown-linux-musl
            cross: true

          # Linux aarch64 (glibc)
          - os: ubuntu-24.04
            target: aarch64-unknown-linux-gnu
            cross: true

          # Linux armv7 static (musl)
          - os: ubuntu-24.04
            target: armv7-unknown-linux-musleabihf
            cross: true

          # macOS x86_64 (cross-compiled from Apple Silicon)
          - os: macos-14
            target: x86_64-apple-darwin
            cross: false

          # macOS aarch64 (Apple Silicon)
          - os: macos-14
            target: aarch64-apple-darwin
            cross: false

          # Windows x86_64 (MSVC)
          - os: windows-2022
            target: x86_64-pc-windows-msvc
            cross: false

          # Windows x86_64 (MinGW)
          - os: windows-2022
            target: x86_64-pc-windows-gnu
            cross: false

    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Install NASM (Windows MSVC)
        if: matrix.target == 'x86_64-pc-windows-msvc'
        uses: ilammy/setup-nasm@v1

      - name: Install cross (for cross-compilation)
        if: matrix.cross
        run: cargo install cross --git https://github.com/cross-rs/cross

      - name: Install musl tools (Linux musl)
        if: matrix.target == 'x86_64-unknown-linux-musl'
        run: |
          sudo apt-get update
          sudo apt-get install -y musl-tools

      - name: Build (native)
        if: ${{ !matrix.cross }}
        run: |
          cargo build --release --target ${{ matrix.target }}

      - name: Set binary name
        id: binary
        shell: bash
        run: |
          if [[ "${{ matrix.target }}" == *"windows"* ]]; then
            echo "name=hostab.exe" >> "$GITHUB_OUTPUT"
          else
            echo "name=hostab" >> "$GITHUB_OUTPUT"
          fi

      - name: Run integration tests
        if: ${{ !matrix.cross }}
        shell: bash
        env:
          HOSTAB_BIN: target/${{ matrix.target }}/release/${{ steps.binary.outputs.name }}
        run: cargo test --test integration_test --release

      - name: Build (cross)
        if: matrix.cross
        run: |
          cross build --release --target ${{ matrix.target }}

      - name: Rename artifact
        id: rename
        shell: bash
        run: |
          BIN="target/${{ matrix.target }}/release/${{ steps.binary.outputs.name }}"
          ARTIFACT="hostab-${{ github.ref_name }}-${{ matrix.target }}"
          if [[ "${{ matrix.target }}" == *"windows"* ]]; then
            ARTIFACT="${ARTIFACT}.exe"
          fi
          cp "$BIN" "$ARTIFACT"
          echo "artifact_name=${ARTIFACT}" >> "$GITHUB_OUTPUT"

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.target }}
          path: ${{ steps.rename.outputs.artifact_name }}
          if-no-files-found: error

  release:
    name: Create Release
    needs: build
    runs-on: ubuntu-24.04
    steps:
      - name: Checkout (for changelog + tags)
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts/

      - name: Prepare release assets
        shell: bash
        run: |
          mkdir -p release-assets
          find artifacts/ -type f | while read f; do
            cp "$f" release-assets/$(basename "$f")
          done
          cd release-assets
          sha256sum * > sha256sums.txt
          ls -la

      - name: Generate changelog
        id: changelog
        run: |
          PREV_TAG=$(git tag --sort=-creatordate | grep -v "${{ github.ref_name }}" | head -1)
          if [ -n "$PREV_TAG" ]; then
            echo "## What's Changed (${PREV_TAG} → ${{ github.ref_name }})" > /tmp/changelog.md
            echo "" >> /tmp/changelog.md
            git log --oneline "${PREV_TAG}..HEAD" | sed 's/^/- /' >> /tmp/changelog.md
          else
            echo "## Initial Release" > /tmp/changelog.md
            echo "" >> /tmp/changelog.md
            git log --oneline --max-count=30 | sed 's/^/- /' >> /tmp/changelog.md
          fi
          echo "changelog<<EOF" >> "$GITHUB_OUTPUT"
          cat /tmp/changelog.md >> "$GITHUB_OUTPUT"
          echo "EOF" >> "$GITHUB_OUTPUT"

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          body: |
            ## hostab ${{ github.ref_name }}

            Cross-platform static builds of hostab — manage /etc/hosts like a pro.

            ${{ steps.changelog.outputs.changelog }}

            ### Assets
            - **x86_64-unknown-linux-gnu** — Linux x86_64 (glibc)
            - **x86_64-unknown-linux-musl** — Linux x86_64 (static musl)
            - **aarch64-unknown-linux-musl** — Linux ARM64 (static musl)
            - **aarch64-unknown-linux-gnu** — Linux ARM64 (glibc)
            - **armv7-unknown-linux-musleabihf** — Linux ARMv7 (static musl)
            - **x86_64-apple-darwin** — macOS Intel
            - **aarch64-apple-darwin** — macOS Apple Silicon
            - **x86_64-pc-windows-msvc** — Windows x86_64 (MSVC)
            - **x86_64-pc-windows-gnu** — Windows x86_64 (MinGW)

          files: release-assets/*
          draft: false
          prerelease: ${{ contains(github.ref_name, '-') }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  publish:
    name: Publish to crates.io
    needs: release
    runs-on: ubuntu-24.04
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Publish
        run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }}