# Validates the FIPS Dockerfiles by exercising the build pipeline the
# same way devel.docker.yml validates the non-FIPS Dockerfiles. The
# build steps download a *-fips.tar.gz release artifact produced by
# release.fips.yml, which does not exist until the first FIPS release
# is published. `continue-on-error: true` on the build/push/inspect
# steps suppresses the resulting failures until the first FIPS release
# ships, after which the suppression should be removed so real build
# regressions fail loudly.
name: devel-docker-fips
on:
  merge_group:
    branches:
      - 2.x
  pull_request:
    paths:
      - docker/**/Dockerfile
      - scripts/ci/**/*.sh
      - .github/workflows/devel.docker.fips.yml
  push:
    branches:
      - 2.x
    paths:
      - docker/**/Dockerfile
      - scripts/ci/**/*.sh
      - .github/workflows/devel.docker.fips.yml

env:
  DOCKER_IMAGE: localhost:5000/github.com/static-web-server/static-web-server

jobs:
  docker-alpine-fips:
    name: Docker test (fips-alpine)
    strategy:
      matrix:
        arch:
          - linux/amd64
          - linux/arm64
    runs-on: ubuntu-22.04
    services:
      registry:
        image: registry:2
        ports:
          - 5000:5000
    steps:
      -
        name: Dependencies
        run: |
          sudo apt-get install jq
      -
        name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 1
      -
        name: Cache Docker layers
        uses: actions/cache@v4
        with:
          path: /tmp/.buildx-cache
          key: static-web-server-${{ matrix.arch }}-alpine-fips-buildx-${{ github.sha }}
          restore-keys: |
            static-web-server-${{ matrix.arch }}-alpine-fips-buildx-
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      -
        name: Docker meta alpine-fips
        id: meta_alpine_fips
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.DOCKER_IMAGE }}
          flavor: |
            latest=false
            suffix=-fips-alpine
          tags: |
            type=schedule
            type=ref,event=branch
            type=ref,event=pr
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=semver,pattern={{major}}
            type=sha
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
        with:
          driver-opts: network=host
      -
        name: Prepare Docker envs
        shell: bash
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          scripts/ci/get_latest_release.sh
          cat /tmp/version >> $GITHUB_ENV
          cat /tmp/version
          echo "SERVER_DOCKERFILE=./docker/alpine-fips/Dockerfile" >> $GITHUB_ENV
      -
        name: Build and export to Docker client
        continue-on-error: true
        uses: docker/build-push-action@v6
        with:
          context: .
          platforms: ${{ matrix.arch }}
          file: ${{ env.SERVER_DOCKERFILE }}
          load: true
          tags: ${{ steps.meta_alpine_fips.outputs.tags }}
          labels: ${{ steps.meta_alpine_fips.outputs.labels }}
          build-args: |
            SERVER_VERSION=${{ env.SERVER_VERSION }}
      -
        name: Build and push to local registry
        continue-on-error: true
        uses: docker/build-push-action@v6
        with:
          context: .
          platforms: ${{ matrix.arch }}
          file: ${{ env.SERVER_DOCKERFILE }}
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta_alpine_fips.outputs.tags }}
          labels: ${{ steps.meta_alpine_fips.outputs.labels }}
          build-args: |
            SERVER_VERSION=${{ env.SERVER_VERSION }}
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
      -
        name: Inspect image
        continue-on-error: true
        run: |
          docker image inspect ${{ env.DOCKER_IMAGE }}:${{ steps.meta_alpine_fips.outputs.version }}
      -
        name: Check manifest
        if: github.event_name != 'pull_request'
        continue-on-error: true
        run: |
          docker buildx imagetools inspect ${{ env.DOCKER_IMAGE }}:${{ steps.meta_alpine_fips.outputs.version }}
      -
        # Temp fix
        # https://github.com/docker/build-push-action/issues/252
        # https://github.com/moby/buildkit/issues/1896
        name: Move cache
        run: |
          rm -rf /tmp/.buildx-cache
          mv /tmp/.buildx-cache-new /tmp/.buildx-cache

  docker-debian-fips:
    name: Docker test (fips-debian)
    strategy:
      matrix:
        arch:
          - linux/amd64
          - linux/arm64
    runs-on: ubuntu-22.04
    services:
      registry:
        image: registry:2
        ports:
          - 5000:5000
    steps:
      -
        name: Dependencies
        run: |
          sudo apt-get install jq
      -
        name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 1
      -
        name: Cache Docker layers
        uses: actions/cache@v4
        with:
          path: /tmp/.buildx-cache
          key: static-web-server-${{ matrix.arch }}-debian-fips-buildx-${{ github.sha }}
          restore-keys: |
            static-web-server-${{ matrix.arch }}-debian-fips-buildx-
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      -
        name: Docker meta debian-fips
        id: meta_debian_fips
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.DOCKER_IMAGE }}
          flavor: |
            latest=false
            suffix=-fips-debian
          tags: |
            type=schedule
            type=ref,event=branch
            type=ref,event=pr
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=semver,pattern={{major}}
            type=sha
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
        with:
          driver-opts: network=host
      -
        name: Prepare Docker envs
        shell: bash
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          scripts/ci/get_latest_release.sh
          cat /tmp/version >> $GITHUB_ENV
          cat /tmp/version
          echo "SERVER_DOCKERFILE=./docker/debian-fips/Dockerfile" >> $GITHUB_ENV
      -
        name: Build and export to Docker client
        continue-on-error: true
        uses: docker/build-push-action@v6
        with:
          context: .
          platforms: ${{ matrix.arch }}
          file: ${{ env.SERVER_DOCKERFILE }}
          load: true
          tags: ${{ steps.meta_debian_fips.outputs.tags }}
          labels: ${{ steps.meta_debian_fips.outputs.labels }}
          build-args: |
            SERVER_VERSION=${{ env.SERVER_VERSION }}
      -
        name: Build and push to local registry
        continue-on-error: true
        uses: docker/build-push-action@v6
        with:
          context: .
          platforms: ${{ matrix.arch }}
          file: ${{ env.SERVER_DOCKERFILE }}
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta_debian_fips.outputs.tags }}
          labels: ${{ steps.meta_debian_fips.outputs.labels }}
          build-args: |
            SERVER_VERSION=${{ env.SERVER_VERSION }}
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
      -
        name: Inspect image
        continue-on-error: true
        run: |
          docker image inspect ${{ env.DOCKER_IMAGE }}:${{ steps.meta_debian_fips.outputs.version }}
      -
        name: Check manifest
        if: github.event_name != 'pull_request'
        continue-on-error: true
        run: |
          docker buildx imagetools inspect ${{ env.DOCKER_IMAGE }}:${{ steps.meta_debian_fips.outputs.version }}
      -
        # Temp fix
        # https://github.com/docker/build-push-action/issues/252
        # https://github.com/moby/buildkit/issues/1896
        name: Move cache
        run: |
          rm -rf /tmp/.buildx-cache
          mv /tmp/.buildx-cache-new /tmp/.buildx-cache
