name: ci

on:
  push:
    branches:
      - main
  pull_request:
  workflow_dispatch:

env:
  # Keep these versions in sync with the Dockerfile
  BDR_VER: '6.0.1'
  GO_VER: '1.21.x'
  PGG_VER: 'v1.31.0'
  PROTOC_VER: '24.3'
  PYTHON_VER: '3.8'
  TEST_TAG: protoc-gen-validate:${{ github.sha }}

jobs:
  ci:
    runs-on: ubuntu-latest
    steps:
      - name: Cache
        uses: actions/cache@v4
        with:
          path: |
            ~/.cache/bazelisk
            ~/.cache/bazel
            ~/.cache/protoc
          key: ${{ runner.os }}-build-cache
      - name: Checkout
        uses: actions/checkout@v4
      - uses: actions/setup-go@v5
        with:
          go-version: ${{ env.GO_VER }}
          cache: true
      - uses: actions/setup-python@v5
        with:
          python-version: ${{ env.PYTHON_VER }}
          cache: 'pip'
      - name: Install Dependencies
        run: |
          mkdir -p ~/.cache/protoc
          PROTOC_ZIP="${HOME}/.cache/protoc/${PROTOC_VER}.zip"
          if [[ ! -f "${PROTOC_ZIP}" ]]; then
              curl -fsSL -o "${PROTOC_ZIP}" https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VER}/protoc-${PROTOC_VER}-linux-x86_64.zip
          fi
          unzip -q ${PROTOC_ZIP} -d ~/protoc
          echo "${HOME}/protoc/bin" >> $GITHUB_PATH
          go install google.golang.org/protobuf/cmd/protoc-gen-go@${PGG_VER}
          go install github.com/bazelbuild/buildtools/buildozer@${BDR_VER}
          pip install -r requirements.txt
      - name: Build
        run: make build
      - name: Run
        run: make ci
