name: Build Docker Images

on:
  push:
    branches:
      - "**"
    tags:
      - "v*"
  workflow_dispatch:

jobs:
  prepare:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          ref: ${{ github.head_ref }}

      - name: Build binaries
        run: |
          build/build.sh

      - name: Import GPG key
        uses: crazy-max/ghaction-import-gpg@v6
        with:
          gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
          passphrase: ${{ secrets.PASSPHRASE }}
          git_user_signingkey: true
          git_commit_gpgsign: true

      - name: Commit any updated files
        uses: stefanzweifel/git-auto-commit-action@v7
        with:
          commit_message: "[skip ci] Commit changes from build-dockers action"
          commit_options: "-S"
          commit_user_email: ${{ vars.COMMIT_USER }}

      - name: Cache build output
        uses: actions/cache/save@v5
        with:
          path: build
          key: build-dockers-${{ github.sha }}

  build-publish:
    runs-on: ubuntu-latest
    needs: prepare
    permissions:
      packages: write
    strategy:
      matrix:
        include:
          - image: ghcr.io/hakwerk/labca-gui
            dockerfile: build/Dockerfile-gui
            label: org.opencontainers.image.title=labca-gui
          - image: ghcr.io/hakwerk/labca-boulder
            dockerfile: build/Dockerfile-boulder
            label: org.opencontainers.image.title=labca-boulder
          - image: ghcr.io/hakwerk/labca-control
            dockerfile: build/Dockerfile-control
            label: org.opencontainers.image.title=labca-control
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          ref: ${{ github.head_ref }}

      - name: Restore build output
        uses: actions/cache/restore@v5
        with:
          path: build
          key: build-dockers-${{ github.sha }}

      - name: Set up docker buildx
        uses: docker/setup-buildx-action@v3

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ matrix.image }}
          labels: ${{ matrix.label }}
          tags: |
            type=schedule,pattern={{date 'YYYYMMDD'}}
            type=match,pattern=v(.*),group=1
            type=edge,enable=${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }}
            type=ref,event=branch,enable=${{ github.ref != format('refs/heads/{0}', github.event.repository.default_branch) }}
            type=ref,event=pr

      - name: Login to GHCR
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: build
          file: ${{ matrix.dockerfile }}
          labels: ${{ steps.meta.outputs.labels }}
          platforms: linux/amd64
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}

