name: CI
on:
  pull_request:
  workflow_dispatch:
jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Setup Go
        uses: actions/setup-go@v5
        with:
          go-version: 1.21
      - name: Lint
        uses: golangci/golangci-lint-action@v4
        with:
          version: v1.54.2
  unit-test:
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Setup Go
        uses: actions/setup-go@v5
        with:
          go-version: 1.21
      - name: Unit Tests
        run: ./script/test
  e2e-test:
    needs: unit-test
    runs-on: ubuntu-latest
    strategy:
      matrix:
        platform: [linux, windows]
        arch: [amd64, arm64]

    steps:
      - name: Setup QEMU
        uses: docker/setup-qemu-action@v3

      - name: Setup Go
        uses: actions/setup-go@v5
        with:
          go-version: 1.21

      - name: Checkout
        uses: actions/checkout@v4

      - name: Build Binary for ${{ matrix.platform }}-${{ matrix.arch }}
        run: |
          if [[ "${{ matrix.platform }}" == "linux" ]]; then
            docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
          fi

          GOOS=${{ matrix.platform }} GOARCH=${{ matrix.arch }} go build -o bin/actions-sync-${{ matrix.platform }}-${{ matrix.arch }} main.go

      - name: Run End-to-End Tests for ${{ matrix.platform }}-${{ matrix.arch }}
        run: ./script/test-build-ci bin/actions-sync-${{ runner.platform }}-${{ runner.arch }}
