name: CI

on:
  push:
    branches: ["master"]
  pull_request:
  workflow_dispatch:
    inputs:
      release:
        description: Dispatch release after successful CI
        required: false
        default: false
        type: boolean

permissions:
  contents: read

concurrency:
  group: pg_local_cache_ci-${{ github.ref }}
  cancel-in-progress: true

jobs:
  source-unit:
    name: source-unit (ASan + UBSan)
    runs-on: ubuntu-24.04
    timeout-minutes: 10
    steps:
      - name: Check out source
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: Validate and test source
        env:
          ASAN_OPTIONS: detect_leaks=1:strict_string_checks=1
          UBSAN_OPTIONS: print_stacktrace=1:halt_on_error=1
        run: make verify-static source-test source-sanitize

  docker-integration:
    name: docker-integration
    runs-on: ubuntu-24.04
    timeout-minutes: 20
    steps:
      - name: Check out source
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: Build and integrate
        env:
          PG_LOCAL_CACHE_SMOKE_DATABASE: pg_local_cache_ci
          PG_LOCAL_CACHE_SMOKE_ROLE: local_cache_ci_worker
          PG_LOCAL_CACHE_SMOKE_CACHE_ENTRIES: "128"
          PG_LOCAL_CACHE_SMOKE_MAX_PREPARED_TRANSACTIONS: "10"
          PG_LOCAL_CACHE_SMOKE_REQUIRE_SMALL_CACHE: "1"
          PG_LOCAL_CACHE_SMOKE_REQUIRE_2PC: "1"
          PG_LOCAL_CACHE_SMOKE_DURATION: "1"
          PG_LOCAL_CACHE_SMOKE_MIN_OPS: "0"
          PG_LOCAL_CACHE_SMOKE_CONCURRENCY: "4"
          PG_LOCAL_CACHE_SMOKE_PIPELINE: "8"
          PG_LOCAL_CACHE_SMOKE_KEYS: "128"
        run: bash tests/docker_smoke.sh

  docker-compatibility:
    name: PostgreSQL ${{ matrix.postgres }} compile
    runs-on: ubuntu-24.04
    timeout-minutes: 20
    strategy:
      fail-fast: false
      matrix:
        postgres: [14, 15, 16, 17, 18]
    steps:
      - name: Check out source
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: Compile extension
        run: >-
          docker build --target extension
          --build-arg POSTGRES_MAJOR=${{ matrix.postgres }}
          --build-arg POSTGRES_VARIANT=bookworm
          --build-arg PGLC_BUILD_ID=${GITHUB_SHA}
          .

  dispatch-release:
    name: dispatch release
    if: github.event_name == 'workflow_dispatch' && inputs.release
    needs: [source-unit, docker-integration, docker-compatibility]
    runs-on: ubuntu-24.04
    timeout-minutes: 5
    permissions:
      actions: write
      contents: read
    env:
      GH_TOKEN: ${{ github.token }}
      GH_REPO: ${{ github.repository }}
    steps:
      - name: Dispatch release for the successful version commit
        run: |
          gh workflow run release.yml \
            --ref master \
            --raw-field "ref=${GITHUB_SHA}" \
            --raw-field "ci_run_id=${GITHUB_RUN_ID}" \
            --field publish_pgxn=true
