putioarr 0.6.5

put.io to sonarr/radarr/whisparr proxy
name: Docker Build and Push

on:
  push:
    branches:
      - main
      - "releases/**"
    tags:
      - v*

env:
  REGISTRY_IMAGE: ghcr.io/wouterdebie/putioarr
  DOCKERFILE_PATH: ./docker/Dockerfile

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        include:
          - platform: linux/amd64
            runner: ubuntu-latest
          - platform: linux/arm64
            runner: ubuntu-24.04-arm
    runs-on: ${{ matrix.runner }}
    steps:
      - name: Prepare platform pair
        id: prep
        run: |
          platform="${{ matrix.platform }}"
          echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_OUTPUT

      - name: Checkout code
        uses: actions/checkout@v4

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

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.CR_PAT }}

      - name: Build and push by digest
        id: build
        uses: docker/build-push-action@v6
        with:
          context: .
          file: ${{ env.DOCKERFILE_PATH }}
          platforms: ${{ matrix.platform }}
          outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true
          cache-from: type=gha,scope=${{ steps.prep.outputs.PLATFORM_PAIR }}
          cache-to: type=gha,mode=max,scope=${{ steps.prep.outputs.PLATFORM_PAIR }}

      - name: Export digest
        run: |
          mkdir -p /tmp/digests
          digest="${{ steps.build.outputs.digest }}"
          touch "/tmp/digests/${digest#sha256:}"

      - name: Upload digest
        uses: actions/upload-artifact@v4
        with:
          name: digests-${{ steps.prep.outputs.PLATFORM_PAIR }}
          path: /tmp/digests/*
          if-no-files-found: error
          retention-days: 1

  merge:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Download digests
        uses: actions/download-artifact@v4
        with:
          path: /tmp/digests
          pattern: digests-*
          merge-multiple: true

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

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.CR_PAT }}

      - name: Determine image tags
        id: docker_tags
        run: |
          REF="${{ github.ref }}"
          REF_NAME="${{ github.ref_name }}"
          REGISTRY="${{ env.REGISTRY_IMAGE }}"

          if [[ "$REF" == refs/tags/* ]]; then
            TAG_ARGS="-t $REGISTRY:latest -t $REGISTRY:$REF_NAME"
            FIRST_TAG="$REGISTRY:latest"
          elif [[ "$REF" == refs/heads/main ]]; then
            TAG_ARGS="-t $REGISTRY:main-latest"
            FIRST_TAG="$REGISTRY:main-latest"
          elif [[ "$REF" == refs/heads/releases/* ]]; then
            TAG_ARGS="-t $REGISTRY:${REF_NAME}-latest"
            FIRST_TAG="$REGISTRY:${REF_NAME}-latest"
          else
            TAG_ARGS="-t $REGISTRY:${{ github.sha }}"
            FIRST_TAG="$REGISTRY:${{ github.sha }}"
          fi
          echo "TAG_ARGS=$TAG_ARGS" >> $GITHUB_OUTPUT
          echo "FIRST_TAG=$FIRST_TAG" >> $GITHUB_OUTPUT

      - name: Create manifest list and push
        working-directory: /tmp/digests
        run: |
          docker buildx imagetools create ${{ steps.docker_tags.outputs.TAG_ARGS }} \
            $(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)

      - name: Inspect image
        run: |
          docker buildx imagetools inspect "${{ steps.docker_tags.outputs.FIRST_TAG }}"