name: Release

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

env:
  CARGO_TERM_COLOR: always
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  build-binary:
    name: Build binary
    runs-on: ubuntu-latest
    # Build inside Debian bookworm (glibc 2.36) so the binary matches the
    # debian:bookworm-slim runtime. Building on ubuntu-latest (glibc 2.39)
    # produced binaries that failed at runtime with:
    #   version `GLIBC_2.39' not found (required by ./flaresolverr-rs)
    container: rust:1.96-bookworm
    steps:
      - uses: actions/checkout@v4

      - name: Install build dependencies
        run: |
          apt-get update
          apt-get install -y --no-install-recommends pkg-config libssl-dev

      - name: Cache cargo registry
        uses: actions/cache@v6
        with:
          path: |
            /usr/local/cargo/registry
            /usr/local/cargo/git
            target
          key: ${{ runner.os }}-cargo-bookworm-${{ hashFiles('**/Cargo.lock') }}

      - name: Build release binary
        run: cargo build --release --bin flaresolverr-rs

      - name: Upload binary artifact
        uses: actions/upload-artifact@v7
        with:
          name: flaresolverr-rs-linux-amd64
          path: target/release/flaresolverr-rs
          retention-days: 1

  github-release:
    name: GitHub Release
    needs: build-binary
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Download binary artifact
        uses: actions/download-artifact@v8
        with:
          name: flaresolverr-rs-linux-amd64

      - name: Rename binary
        run: mv flaresolverr-rs flaresolverr-rs-linux-amd64 && chmod +x flaresolverr-rs-linux-amd64

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          files: flaresolverr-rs-linux-amd64
          generate_release_notes: true

  docker:
    name: Docker
    needs: github-release
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Extract version from tag
        id: ver
        run: |
          TAG="${{ github.ref_name }}"
          VERSION="${TAG#v}"
          MAJOR="${VERSION%%.*}"
          REST="${VERSION#*.}"
          MINOR="${REST%%.*}"
          echo "version=${VERSION}" >> "$GITHUB_OUTPUT"
          echo "major=${MAJOR}" >> "$GITHUB_OUTPUT"
          echo "major_minor=${MAJOR}.${MINOR}" >> "$GITHUB_OUTPUT"

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v4

      - name: Log in to GHCR
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: .
          platforms: linux/amd64
          push: true
          pull: true
          tags: |
            ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.ver.outputs.version }}
            ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.ver.outputs.major_minor }}
            ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.ver.outputs.major }}
            ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
          build-args: |
            VERSION=${{ steps.ver.outputs.version }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
