name: Update Docker Hub images

on:
  release:
    types:
      - published
  workflow_dispatch:
    inputs:
      release:
        description: 'Tag name to build'
        required: true

jobs:
  build:
    name: Update Docker Hub images
    runs-on: ubuntu-22.04

    steps:
      - uses: actions/checkout@v6
        with:
          submodules: true #needed for build/php submodule

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

      - name: Login to DockerHub
        uses: docker/login-action@v4
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

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

      - name: Get tag name
        id: tag-name
        run: |
          if [[ "${{ github.event_name }}" == "release" ]]; then
            echo TAG_NAME="${{ github.event.release.tag_name }}" >> $GITHUB_OUTPUT
          elif [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
            echo TAG_NAME="${{ github.event.inputs.release }}" >> $GITHUB_OUTPUT
          else
            echo "Unsupported event type: ${{ github.event_name }}"
            exit 1
          fi

      - name: Parse version
        id: version
        run: |
          VERSION="${{ steps.tag-name.outputs.TAG_NAME }}"
          echo MAJOR=$(echo $VERSION | cut -d. -f1) >> $GITHUB_OUTPUT
          echo MINOR=$(echo $VERSION | cut -d. -f1-2) >> $GITHUB_OUTPUT

      - name: Download new release information
        run: curl -f -L ${{ github.server_url }}/${{ github.repository }}/releases/download/${{ steps.tag-name.outputs.TAG_NAME }}/build_info.json -o new_build_info.json

      - name: Detect channel
        id: channel
        run: echo CHANNEL=$(jq -r '.channel' new_build_info.json) >> $GITHUB_OUTPUT

      - name: Get name of Docker repository name
        id: docker-repo-name
        run: echo NAME=$(echo "${GITHUB_REPOSITORY,,}") >> $GITHUB_OUTPUT

      - name: Build image for tag
        uses: docker/build-push-action@v7.1.0
        with:
          push: true
          file: ./docker/Dockerfile
          tags: |
            ${{ steps.docker-repo-name.outputs.NAME }}:${{ steps.tag-name.outputs.TAG_NAME }}
            ghcr.io/${{ steps.docker-repo-name.outputs.NAME }}:${{ steps.tag-name.outputs.TAG_NAME }}
          build-args: |
            GIT_HASH=${{ github.sha }}

      - name: Build image for major tag
        if: steps.channel.outputs.CHANNEL == 'stable'
        uses: docker/build-push-action@v7.1.0
        with:
          push: true
          file: ./docker/Dockerfile
          tags: |
            ${{ steps.docker-repo-name.outputs.NAME }}:${{ steps.version.outputs.MAJOR }}
            ghcr.io/${{ steps.docker-repo-name.outputs.NAME }}:${{ steps.version.outputs.MAJOR }}
          build-args: |
            GIT_HASH=${{ github.sha }}

      - name: Build image for minor tag
        if: steps.channel.outputs.CHANNEL == 'stable'
        uses: docker/build-push-action@v7.1.0
        with:
          push: true
          file: ./docker/Dockerfile
          tags: |
            ${{ steps.docker-repo-name.outputs.NAME }}:${{ steps.version.outputs.MINOR }}
            ghcr.io/${{ steps.docker-repo-name.outputs.NAME }}:${{ steps.version.outputs.MINOR }}
          build-args: |
            GIT_HASH=${{ github.sha }}

      - name: Build image for latest tag
        if: steps.channel.outputs.CHANNEL == 'stable'
        uses: docker/build-push-action@v7.1.0
        with:
          push: true
          file: ./docker/Dockerfile
          tags: |
            ${{ steps.docker-repo-name.outputs.NAME }}:latest
            ghcr.io/${{ steps.docker-repo-name.outputs.NAME }}:latest
          build-args: |
            GIT_HASH=${{ github.sha }}