name: Release

on:
  push:
    branches: [main]

permissions:
  contents: write
  packages: write

jobs:
  test:
    uses: ./.github/workflows/runtest.yml

  build-and-release:
    needs: test
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
      - name: Set up Go
        uses: actions/setup-go@v4
        with:
          go-version: "1.23"

      - name: Build for Windows
        run: |
          GOOS=windows GOARCH=amd64 go build -o sif-windows-amd64.exe ./cmd/sif
          GOOS=windows GOARCH=386 go build -o sif-windows-386.exe ./cmd/sif

      - name: Build for macOS
        run: |
          GOOS=darwin GOARCH=amd64 go build -o sif-macos-amd64 ./cmd/sif
          GOOS=darwin GOARCH=arm64 go build -o sif-macos-arm64 ./cmd/sif

      - name: Build for Linux
        run: |
          GOOS=linux GOARCH=amd64 go build -o sif-linux-amd64 ./cmd/sif
          GOOS=linux GOARCH=386 go build -o sif-linux-386 ./cmd/sif
          GOOS=linux GOARCH=arm64 go build -o sif-linux-arm64 ./cmd/sif

      - name: Set release version
        run: echo "RELEASE_VERSION=$(git rev-parse --short HEAD)" >> $GITHUB_ENV

      - name: Create Release and Upload Assets
        uses: softprops/action-gh-release@v2
        with:
          tag_name: automated-release-${{ env.RELEASE_VERSION }}
          name: Release ${{ env.RELEASE_VERSION }}
          body: |
            Automated release v${{ env.RELEASE_VERSION }}

            ## Assets
            - Windows (64-bit): `sif-windows-amd64.exe`
            - Windows (32-bit): `sif-windows-386.exe`
            - macOS (64-bit Intel): `sif-macos-amd64`
            - macOS (64-bit ARM): `sif-macos-arm64`
            - Linux (64-bit): `sif-linux-amd64`
            - Linux (32-bit): `sif-linux-386`
            - Linux (64-bit ARM): `sif-linux-arm64`

            For more details, check the [commit history](https://github.com/${{ github.repository }}/commits/main).
          draft: false
          prerelease: false
          files: |
            sif-windows-amd64.exe
            sif-windows-386.exe
            sif-macos-amd64
            sif-macos-arm64
            sif-linux-amd64
            sif-linux-386
            sif-linux-arm64
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
