name: Build and publish docker base image for the otupy framework

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

# To manually trigger this workflow:
# % gh api   --method POST   \
#		-H "Accept: application/vnd.github+json" \
#		-H "X-GitHub-Api-Version: 2022-11-28"  \ 
#		/repos/mattereppe/otupy/dispatches   \ 
#		-f 'event_type=trigger-pypi-package' \
#		-F "client_payload[tag]=v1.1.7"

on:
  repository_dispatch:
    types: [trigger-pypi-package]

env:
  # Use docker.io for Docker Hub if empty
  REGISTRY: ghcr.io
  # github.repository as <account>/<repo>
  #IMAGE_NAME: ${{ github.repository }}
  IMAGE_NAME: mattereppe/otupy


jobs:
  build:

    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
      # This is used to complete the identity challenge
      # with sigstore/fulcio when running outside of PRs.
      id-token: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3 #v3.3.0 at the time of change

      # Install the cosign tool except on PR
      # https://github.com/sigstore/cosign-installer
      - name: Install cosign
        if: github.event_name != 'pull_request'
        uses: sigstore/cosign-installer@v2.2.0 #v3.0.1 at the time of change
        with:
          cosign-release: 'v2.2.0'  # This is the default for installer v3


      # Workaround: https://github.com/docker/build-push-action/issues/461
      - name: Setup Docker buildx
        uses: docker/setup-buildx-action@v2 #v2.4.0 at the time of change

      # Login against a Docker registry except on PR
      # https://github.com/docker/login-action
      - name: Log into registry ${{ env.REGISTRY }}
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v2 #v2.4.0 at the time of change
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      # Extract metadata (tags, labels) for Docker
      # https://github.com/docker/metadata-action
      - name: Extract Docker metadata
        id: meta
        uses: docker/metadata-action@v4 #v4.3.0 at the time of change
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

      # Build and push Docker image with Buildx (don't push on PR)
      # https://github.com/docker/build-push-action
      - name: Build and push Docker image
        id: build-and-push
        uses: docker/build-push-action@v4 #v4.0.0 at the time of change
        with:
          context: ./docker/otupy/
          push: ${{ github.event_name != 'pull_request' }}
          build-args: "VERSION=${{ github.event.client_payload.tag }}"
#tags: ${{ steps.meta.outputs.tags }}
          tags: ${{ env.REGISTRY}}/${{ env.IMAGE_NAME }}:${{ github.event.client_payload.tag }}, ${{ env.REGISTRY}}/${{ env.IMAGE_NAME }}:latest
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=gha
          cache-to: type=gha,mode=max

      # Sign the resulting Docker image digest except on PRs.
      # This will only write to the public Rekor transparency log when the Docker
      # repository is public to avoid leaking data.  If you would like to publish
      # transparency data even for private images, pass --force to cosign below.
      # https://github.com/sigstore/cosign
      - name: Sign the published Docker image
        if: ${{ github.event_name != 'pull_request' }}
        env:
          COSIGN_EXPERIMENTAL: "true"
        # This step uses the identity token to provision an ephemeral certificate
        # against the sigstore community Fulcio instance.
        run: cosign sign --yes ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ steps.build-and-push.outputs.digest }}

  trigger-docker:
    runs-on: ubuntu-latest
    needs:
      - build

    steps:
      - name: Trigger other workflows expecting base image
        uses: peter-evans/repository-dispatch@v1
        with:
          token: ${{ secrets.REPO_GHA_PAT }}
          repository: ${{ github.repository }}
          event-type: trigger-docker-images
          client-payload: '{"ref": "${{ github.event.client_payload.ref }}", "sha": "${{ github.event.client_payload.sha }}", "tag": "${{  github.event.client_payload.tag }}" }'
