name: Build artifacts

on:
  push:
    branches:
    - main

  pull_request:
    branches:
    - main

concurrency: build-${{ github.ref }}

env:
  REGISTRY: ghcr.io

defaults:
  run:
    shell: bash

jobs:
  test:
    name: Run tests
    runs-on: ubuntu-22.04

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    - name: Setup go
      uses: actions/setup-go@v5
      with:
        go-version-file: go.mod

    - name: Check that license header boilerplate is correct
      uses: sap/cs-actions/check-go-license-boilerplate@main
      with:
        boilerplate-path: hack/boilerplate.go.txt

    - name: Check that license headers are correct
      uses: sap/cs-actions/check-go-license-headers@main
      with:
        boilerplate-path: hack/boilerplate.go.txt

    - name: Check that generated artifacts are up-to-date
      run: |
        make generate
        echo "Running 'git status' ..."
        if [ -z "$(git status --porcelain)" ]; then
          echo "Generated artifacts are up-to-date."
        else
          >&2 echo "Generated artifacts are not up-to-date; probably 'make generate' was not run before committing."
          exit 1
        fi

    - name: Check that manifests are up-to-date
      run: |
        make manifests
        echo "Running 'git status' ..."
        if [ -z "$(git status --porcelain)" ]; then
          echo "Manifests are up-to-date."
        else
          >&2 echo "Manifests are not up-to-date; probably 'make manifests' was not run before committing."
          exit 1
        fi

    - name: Run tests
      run: |
        make envtest
        KUBEBUILDER_ASSETS=$(pwd)/bin/k8s/current E2E_ENABLED=${{ github.event_name == 'push' }} go test -count 1 ./...

  build-docker:
    name: Build Docker image
    runs-on: ubuntu-22.04
    needs: test
    permissions:
      contents: read
    outputs:
      image-archive: image.tar
      image-repository: ${{ steps.prepare-repository-name.outputs.repository }}
      image-tag: ${{ steps.extract-metadata.outputs.version }}

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    - name: Setup Docker Buildx
      uses: docker/setup-buildx-action@v3

    - name: Prepare repository name
      id: prepare-repository-name
      run: |
        repository=$REGISTRY/${{ github.repository }}
        echo "repository=${repository,,}" >> $GITHUB_OUTPUT

    - name: Extract metadata (tags, labels) for Docker
      id: extract-metadata
      uses: docker/metadata-action@v5
      with:
        images: ${{ steps.prepare-repository-name.outputs.repository }}

    - name: Build Docker image
      uses: docker/build-push-action@v5
      with:
        platforms: linux/amd64,linux/arm64
        context: .
        cache-from: |
          type=gha,scope=sha-${{ github.sha }}
          type=gha,scope=${{ github.ref_name }}
          type=gha,scope=${{ github.base_ref || 'main' }}
          type=gha,scope=main
        cache-to: |
          type=gha,scope=sha-${{ github.sha }},mode=max
          type=gha,scope=${{ github.ref_name }},mode=max
        outputs: |
          type=oci,dest=${{ runner.temp }}/image.tar
        tags: ${{ steps.extract-metadata.outputs.tags }}
        labels: ${{ steps.extract-metadata.outputs.labels }}

    - name: Upload Docker image archive
      uses: actions/upload-artifact@v4
      with:
        name: image.tar
        path: ${{ runner.temp }}/image.tar