name: ci
on:
  pull_request:
    types: [opened, reopened, synchronize, ready_for_review, review_requested]
    branches:
      - master
  push:
    branches:
      - master
      - prepare-release
  # Support triggering the CI workflow manually.
  workflow_dispatch:

permissions:
  contents: read

jobs:
  test:
    name: Test go${{ matrix.goVersion}}.x ${{ matrix.goArch }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - goVersion: "1"
            GOARCH: "amd64"
            SKIP_PYTHON_BINDINGS_TESTS: "0"
          # - goVersion: "1"
          #   GOARCH: "arm64"
          #   SKIP_PYTHON_BINDINGS_TESTS: "0"
          # - goVersion: "1"
          #   GOARCH: "386"
          #   SKIP_PYTHON_BINDINGS_TESTS: "1"
          # - goVersion: "1"
          #   GOARCH: "ppc64le"
          #   SKIP_PYTHON_BINDINGS_TESTS: "0"
          - goVersion: "1.24"
            GOARCH: "amd64"
            SKIP_PYTHON_BINDINGS_TESTS: "0"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-go@v6
        with:
          go-version: ${{ matrix.goVersion }}
      - run: sudo apt install python3-dev python3-setuptools
      - run: pip install -U wheel
      - run: pip install -U pytest setuptools
      - run: make install.dependencies
      - run: make test
        env:
          GOARCH: ${{ matrix.GOARCH }}
          CGO_ENABLED: "1"
          SKIP_PYTHON_BINDINGS_TESTS: ${{ matrix.SKIP_PYTHON_BINDINGS_TESTS }}

  bazel:
    name: bazel test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions/cache@v5
        with:
          path: |
            ~/.cache/bazel
            ~/.cache/bazelisk
          key: ${{ runner.os }}-bazel-cache
      - run: bazelisk build --lockfile_mode=error //...
      - run: bazelisk test --lockfile_mode=error //...

  bazel_module_example:
    name: bazel module example test
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        bazel_version: ["7.*", "8.*", "9.*"]
    steps:
      - uses: actions/checkout@v6
      - uses: actions/cache@v5
        with:
          path: |
            ~/.cache/bazel
            ~/.cache/bazelisk
          key: ${{ runner.os }}-bazel-cache
      - name: Build
        env:
          USE_BAZEL_VERSION: ${{ matrix.bazel_version }}
        run: |
          # We leave the lockfile off for this. lockfile_mode=off is also
          # set in the .bazelrc in the examples/bazel/ directory.
          # The example directly references the local jsonnet_go module from
          # its parent directory, so the hash will change on almost every
          # commit anyway.
          cd examples/bazel && bazelisk build --lockfile_mode=off //...

  all:
    name: Check all
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - run: sudo apt install python3-dev python3-setuptools
      - run: pip install -U wheel
      - run: pip install -U pytest setuptools
      - run: make all
      - name: Install goveralls
        run: |
          export GOPATH=$GITHUB_WORKSPACE
          go install github.com/mattn/goveralls@v0.0.12
      - name: Send coverage
        env:
          COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          ./bin/goveralls -coverprofile=coverage.out -service=github

  goreleaser:
    name: Goreleaser
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: goreleaser/goreleaser-action@ec59f474b9834571250b370d4735c50f8e2d1e29 # v7.0.0
        with:
          args: release --snapshot --skip=publish --clean
