name: Release

on:
  push:
    tags:
      - 'v*'

env:
  GO_VERSION: "1.24"

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-go@v5
        with:
          go-version: ${{ env.GO_VERSION }}

      - name: Download dependencies
        run: go mod download

      - name: Run tests
        run: go test -race ./...

      - name: Run go vet
        run: go vet ./...

  build:
    name: Build (${{ matrix.goos }}, ${{ matrix.goarch }})
    needs: test
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - goos: linux
            goarch: amd64
            suffix: ""
          - goos: linux
            goarch: arm64
            suffix: ""
          - goos: darwin
            goarch: amd64
            suffix: ""
          - goos: darwin
            goarch: arm64
            suffix: ""
          - goos: windows
            goarch: amd64
            suffix: ".exe"
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-go@v5
        with:
          go-version: ${{ env.GO_VERSION }}

      - name: Download dependencies
        run: go mod download

      - name: Build binary
        env:
          GOOS: ${{ matrix.goos }}
          GOARCH: ${{ matrix.goarch }}
        run: |
          VERSION=${GITHUB_REF#refs/tags/}
          COMMIT=$(git rev-parse --short HEAD)
          DATE=$(date -u +%Y-%m-%dT%H:%M:%SZ)
          LDFLAGS="-s -w -X main.Version=$VERSION -X main.Commit=$COMMIT -X main.BuildDate=$DATE"
          mkdir -p dist
          go build -ldflags "$LDFLAGS" -o "dist/jdenticon-${{ matrix.goos }}-${{ matrix.goarch }}${{ matrix.suffix }}" ./cmd/jdenticon/

      - name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: jdenticon-${{ matrix.goos }}-${{ matrix.goarch }}
          path: dist/
