# inspired by: https://docs.github.com/en/actions/guides/publishing-docker-images
name: Publish Docker image To hub.docker

on:
  release:
    types: [published]
  workflow_dispatch:
    inputs: {}

jobs:
  push_to_registry:
    name: Push Docker image to Docker Hub
    runs-on: ubuntu-latest
    steps:
      - name: Harden Runner
        uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2
        with:
          egress-policy: audit
          allowed-endpoints: >
            api.github.com:443
            github.com:443
            hub.docker.com:443
            registry-1.docker.io:443
            auth.docker.io:443
            production.cloudflare.docker.com:443
            objects.githubusercontent.com:443
            registry.npmjs.org:443

      - name: Check out the repo
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6
        with:
          persist-credentials: false
      # work around for npm installs from git+https://github.com/johannesjo/J2M.git
      - name: Reconfigure git to use HTTP authentication
        run: >
          git config --global url."https://github.com/".insteadOf
          ssh://git@github.com/

      - name: Log in to Docker Hub
        uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      # Note: Docker Hub image is under johannesjo/ (personal account) while
      # the GitHub repo is under super-productivity/ org. This is intentional
      # to maintain existing Docker image references for users.
      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@c299e40c65443455700f0fdfc63efafe5b349051
        with:
          images: johannesjo/super-productivity

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

      - name: Build and push Docker image
        uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            BUILDKIT_CONTEXT_KEEP_GIT_DIR=1
            UNSPLASH_KEY=${{ secrets.UNSPLASH_KEY }}
            UNSPLASH_CLIENT_ID=${{ secrets.UNSPLASH_CLIENT_ID }}
          platforms: linux/amd64, linux/arm64
          cache-from: type=gha
          cache-to: type=gha,mode=max
