name: Build Container Image Builds

on:
  workflow_dispatch:
  repository_dispatch:
  schedule:
    # rebuild image every sunday
    - cron: "0 0 * * 0"

jobs:
  build-images:
    strategy:
      matrix:
        build:
          - stable
          - edge
    name: "Build Images"
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Setup container meta information
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ vars.IMAGE_REGISTRY }}/${{ github.repository }}-build
          labels: |
            org.opencontainers.image.vendor=Greenbone
            org.opencontainers.image.base.name=greenbone/gvm-libs
          flavor: latest=false # no latest container tag for git tags
          tags: |
            type=raw,value=latest,enable=${{ matrix.build == 'stable' }}
            type=raw,value=stable,enable=${{ matrix.build == 'stable' }}
            type=raw,value=edge,enable=${{ matrix.build == 'edge' }}
      - name: Set container build options
        id: container-opts
        run: |
          if [[ "${{ matrix.build }}" = 'stable' ]]; then
            echo "gvm-libs-version=oldstable" >> $GITHUB_OUTPUT
          else
            echo "gvm-libs-version=oldstable-edge" >> $GITHUB_OUTPUT
          fi
      - name: Login to GitHub Docker registry
        uses: docker/login-action@v3
        with:
            registry: ghcr.io
            username: ${{ secrets.GREENBONE_BOT }}
            password: ${{ secrets.GREENBONE_BOT_PACKAGES_WRITE_TOKEN }}
      - run: echo "Build and push ${{ steps.meta.outputs.tags }}"
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          build-args: |
            GVM_LIBS_VERSION=${{ steps.container-opts.outputs.gvm-libs-version }}
          file: .docker/build.Dockerfile
          platforms: linux/amd64,linux/arm64
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
