name: Build
on:
  push:
    branches:
      - main
  pull_request:

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - name: Install Go
        uses: actions/setup-go@v5
        with:
          go-version: stable
      - name: Git checkout
        uses: actions/checkout@v4
      - name: Build
        uses: goreleaser/goreleaser-action@v6
        with:
          version: latest
          args: release --clean --snapshot
      - name: Test
        run: go test -race -coverprofile=coverage.txt -covermode=atomic ./...
      - name: Upload coverage
        uses: codecov/codecov-action@v4
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          fail_ci_if_error: true
  build_1_18:
    name: Build with Go 1.18
    runs-on: ubuntu-latest
    if: (github.event_name == 'push' && github.ref == 'refs/heads/main') || github.event_name == 'pull_request'
    steps:
      - name: Install Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.18'
      - name: Git checkout
        uses: actions/checkout@v4
      - name: Build and test
        run: |
          go get ./...
          go test -race ./...
