name: Build and Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        goos: [windows, linux, darwin]
        goarch: [amd64, arm64]

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

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.23.1'

      - name: Set up Environment
        run: |
          mkdir -p build
          echo "package main" > version.go
          echo "const Version = \"${{ github.ref_name }}\"" >> version.go

      - name: Build for ${{ matrix.goos }}_${{ matrix.goarch }}
        run: |
          if [ "${{ matrix.goos }}" = "windows" ]; then EXT=".exe"; else EXT=""; fi
          GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -ldflags="-s -w" -trimpath -o build/ffwebp_${{ github.ref_name }}_${{ matrix.goos }}_${{ matrix.goarch }}$EXT

      - name: Upload build artifact
        uses: actions/upload-artifact@v4
        with:
          name: ffwebp_${{ github.ref_name }}_${{ matrix.goos }}_${{ matrix.goarch }}
          path: ./build/ffwebp_${{ github.ref_name }}_${{ matrix.goos }}_${{ matrix.goarch }}*

  release:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

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

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          files: ./build/**
          name: "Release ${{ github.ref_name }}"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
