name: release-docker-fips
on:
  release:
    types:
      - published

jobs:
  docker-image-fips-alpine:
    runs-on: ubuntu-22.04
    if: contains(github.ref, 'v2.')
    steps:
      -
        name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 1
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      -
        name: Docker meta fips-alpine
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: |
            joseluisq/static-web-server
            ghcr.io/static-web-server/static-web-server
          flavor: |
            latest=false
            suffix=-fips-alpine
          tags: |
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=semver,pattern={{major}}
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      -
        name: Login to DockerHub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Login to ghcr.io
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      -
        name: Prepare Docker envs
        shell: bash
        run: |
          echo "SERVER_VERSION=${GITHUB_REF##*/v}" >> $GITHUB_ENV
      -
        name: Build and push (fips-alpine)
        uses: docker/build-push-action@v6
        with:
          push: true
          context: .
          platforms: linux/amd64,linux/arm64
          file: ./docker/alpine-fips/Dockerfile
          tags: ${{ steps.meta.outputs.tags }}
          build-args: |
            SERVER_VERSION=${{ env.SERVER_VERSION }}

  docker-image-fips-debian:
    needs: ['docker-image-fips-alpine']
    runs-on: ubuntu-22.04
    if: contains(github.ref, 'v2.')
    steps:
      -
        name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 1
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      -
        name: Docker meta fips-debian
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: |
            joseluisq/static-web-server
            ghcr.io/static-web-server/static-web-server
          flavor: |
            latest=false
            suffix=-fips-debian
          tags: |
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=semver,pattern={{major}}
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      -
        name: Login to DockerHub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Login to ghcr.io
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      -
        name: Prepare Docker envs
        shell: bash
        run: |
          echo "SERVER_VERSION=${GITHUB_REF##*/v}" >> $GITHUB_ENV
      -
        name: Build and push (fips-debian)
        uses: docker/build-push-action@v6
        with:
          push: true
          context: .
          platforms: linux/amd64,linux/arm64
          file: ./docker/debian-fips/Dockerfile
          tags: ${{ steps.meta.outputs.tags }}
          build-args: |
            SERVER_VERSION=${{ env.SERVER_VERSION }}

  docker-image-fips-scratch:
    needs: ['docker-image-fips-alpine']
    runs-on: ubuntu-22.04
    steps:
      -
        name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 1
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      -
        name: Docker meta fips-scratch
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: |
            joseluisq/static-web-server
            ghcr.io/static-web-server/static-web-server
          flavor: |
            latest=false
            suffix=-fips
          tags: |
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=semver,pattern={{major}}
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      -
        name: Login to DockerHub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Login to ghcr.io
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      -
        name: Prepare Docker envs
        shell: bash
        run: |
          echo "SERVER_VERSION=${GITHUB_REF##*/v}" >> $GITHUB_ENV
      -
        name: Build and push (fips-scratch)
        uses: docker/build-push-action@v6
        with:
          push: true
          context: .
          platforms: linux/amd64,linux/arm64
          file: ./docker/scratch-fips/Dockerfile
          tags: ${{ steps.meta.outputs.tags }}
          build-args: |
            SERVER_VERSION=${{ env.SERVER_VERSION }}
