name: 'Docker Build'

on:
    workflow_dispatch:
        inputs:
            kimai_tag:
                description: 'Kimai tag to build'
                required: true
    release:
        types: [released]

jobs:
    build:
        runs-on: ubuntu-latest

        steps:

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

            - name: Install buildx
              uses: docker/setup-buildx-action@v3

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

            - name: Determine version
              run: |
                  input="${{ github.event.inputs.kimai_tag }}"
                  
                  # Determine between manual trigger and release event
                  if [ -z "$input" ]; then
                      echo "Using release tag: ${{ github.event.release.tag_name }}"
                      version="${{ github.event.release.tag_name }}"
                  else
                      echo "Using tag provided: $input"
                      version="$input"
                  fi

                  if [[ ! $version =~ ^2\.(0|[1-9]*)(0?)\.(0|[0-9]*)(0?)$ ]]; then
                    echo "Invalid version number: $version"
                    exit 1
                  fi
                  
                  echo "kimai_version=$version" >> $GITHUB_ENV

            - name: FPM image
              uses: docker/build-push-action@v5
              with:
                  context: .
                  file: Dockerfile
                  build-args: |
                      KIMAI=${{ env.kimai_version }}
                      BASE=fpm
                  target: prod
                  platforms: linux/amd64,linux/arm64
                  tags: |
                      kimai/kimai2:latest
                      kimai/kimai2:fpm
                      kimai/kimai2:fpm-${{ env.kimai_version }}
                  push: true

            - name: Apache image
              uses: docker/build-push-action@v5
              with:
                  context: .
                  file: Dockerfile
                  build-args: |
                      KIMAI=${{ env.kimai_version }}
                      BASE=apache
                  target: prod
                  platforms: linux/amd64,linux/arm64
                  tags: |
                      kimai/kimai2:apache
                      kimai/kimai2:apache-${{ env.kimai_version }}
                  push: true

            - name: Development image
              uses: docker/build-push-action@v5
              with:
                  context: .
                  file: Dockerfile
                  build-args: |
                      KIMAI=${{ env.kimai_version }}
                      BASE=apache
                  target: dev
                  platforms: linux/amd64,linux/arm64
                  tags: |
                      kimai/kimai2:dev
                  push: true
