name: Build Docker image and push to DockerHub

on:
  push:
    branches:
      - master
    tags:
      - v*

jobs:
  build-and-push:
    runs-on: self-hosted-amd64-1cpu
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Import secrets
        uses: hashicorp/vault-action@v3
        id: secrets
        with:
          exportEnv: false
          url: ${{ secrets.VAULT_URL }}
          role: ${{ secrets.VAULT_ROLE }}
          method: kubernetes
          path: kubernetes-ci
          secrets: |
            kv-gitlab-ci/data/github/shared/dockerhub-creds user | DOCKERHUB_USER ;
            kv-gitlab-ci/data/github/shared/dockerhub-creds password | DOCKERHUB_PASSWORD ;

      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ steps.secrets.outputs.DOCKERHUB_USER }}
          password: ${{ steps.secrets.outputs.DOCKERHUB_PASSWORD }}

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

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

      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: wallarm/gotestwaf
          tags: |
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=raw,value=edge,enable={{is_default_branch}}

      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=gha
          cache-to: type=gha,mode=max

  clean-old-cache:
    needs: build-and-push
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Delete old cached docker layers
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          set -eu

          LAST_WORKFLOW_TIME=$(
              gh run list \
                  --json workflowName,startedAt \
                  --jq ".[] | select( .workflowName == \"${{ github.workflow }}\") | .startedAt" \
              | head -n 1
          )

          echo "Time of the last running '${{ github.workflow }}' workflow: $LAST_WORKFLOW_TIME"

          while true; do
              OLD_CACHE_IDS=$(
                  gh api \
                      -H "Accept: application/vnd.github+json" \
                      --jq ".actions_caches[] | select(.last_accessed_at < \"$LAST_WORKFLOW_TIME\") | .id" \
                      /repos/wallarm/gotestwaf/actions/caches \
                  | tr '\n' ' '
              )

              if [ -z "$OLD_CACHE_IDS" ]; then
                  echo "Done"
                  break
              fi

              echo "ID of caches to delete: $OLD_CACHE_IDS"

              for cache_id in $OLD_CACHE_IDS; do
                  gh api \
                      --method DELETE \
                      -H "Accept: application/vnd.github+json" \
                      /repos/wallarm/gotestwaf/actions/caches/$cache_id
              done
          done
