# 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@9af89fc71515a100421586dfdb3dc9c984fbf411 # v2.19.4
        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@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # 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@af1e73f918a031802d376d3c8bbc3fe56130a9b0 # 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@dc802804100637a589fabce1cb79ff13a1411302
        with:
          images: johannesjo/super-productivity

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@bb05f3f5519dd87d3ba754cc423b652a5edd6d2c # v4.2.0

      - name: Build and push Docker image
        uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a # v7.3.0
        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
