name: Publish Pinepods Multi-Architecture Image to DockerHub
on:
  release:
    types: [released]
  workflow_dispatch:
    inputs:
      version:
        description: "Manual override version tag (optional)"
        required: false
env:
  REGISTRY: docker.io
  IMAGE_NAME: madeofpendletonwool/pinepods
jobs:
  set-env:
    runs-on: ubuntu-latest
    outputs:
      IMAGE_TAG: ${{ steps.set_tags.outputs.IMAGE_TAG }}
      CREATE_LATEST: ${{ steps.set_tags.outputs.CREATE_LATEST }}
    steps:
      - name: Set Image Tag and Latest Tag
        id: set_tags
        run: |
          echo "IMAGE_TAG=${{ github.event.release.tag_name || github.event.inputs.version || 'latest' }}" >> $GITHUB_OUTPUT
          if [ "${{ github.event_name }}" == "release" ]; then
            echo "CREATE_LATEST=true" >> $GITHUB_OUTPUT
          else
            echo "CREATE_LATEST=false" >> $GITHUB_OUTPUT
          fi

  build-and-push-x86:
    needs: set-env
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
      id-token: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      - name: Log in to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_KEY }}
      - name: Build and push x86 image
        run: |
          docker build --platform linux/amd64 --build-arg PINEPODS_VERSION=${{ needs.set-env.outputs.IMAGE_TAG }} -t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.set-env.outputs.IMAGE_TAG }}-amd64 -f dockerfile .
          docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.set-env.outputs.IMAGE_TAG }}-amd64
          if [ "${{ needs.set-env.outputs.CREATE_LATEST }}" == "true" ]; then
            docker tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.set-env.outputs.IMAGE_TAG }}-amd64 ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest-amd64
            docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest-amd64
          fi

  build-and-push-arm64:
    needs: set-env
    runs-on: ubuntu-24.04-arm
    permissions:
      contents: read
      packages: write
      id-token: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      - name: Log in to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_KEY }}
      - name: Build and push ARM64 image
        run: |
          docker build --platform linux/arm64 --build-arg PINEPODS_VERSION=${{ needs.set-env.outputs.IMAGE_TAG }} -t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.set-env.outputs.IMAGE_TAG }}-arm64 -f dockerfile-arm .
          docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.set-env.outputs.IMAGE_TAG }}-arm64
          if [ "${{ needs.set-env.outputs.CREATE_LATEST }}" == "true" ]; then
            docker tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.set-env.outputs.IMAGE_TAG }}-arm64 ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest-arm64
            docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest-arm64
          fi

  create-manifests:
    needs: [set-env, build-and-push-x86, build-and-push-arm64]
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
      id-token: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      - name: Log in to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_KEY }}

      - name: Create and push Docker manifest for the version tag
        run: |
          sleep 10
          # Pull the images first to ensure they're available
          docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.set-env.outputs.IMAGE_TAG }}-amd64
          docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.set-env.outputs.IMAGE_TAG }}-arm64

          # Create and push manifest
          docker manifest create ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.set-env.outputs.IMAGE_TAG }} \
            --amend ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.set-env.outputs.IMAGE_TAG }}-amd64 \
            --amend ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.set-env.outputs.IMAGE_TAG }}-arm64

          docker manifest push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.set-env.outputs.IMAGE_TAG }}

      - name: Create and push Docker manifest for the latest tag
        if: needs.set-env.outputs.CREATE_LATEST == 'true'
        run: |
          docker manifest create ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest \
            --amend ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest-amd64 \
            --amend ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest-arm64
          docker manifest push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
