name: Create Docker image with latest tag on manual trigger

on:
  workflow_dispatch:

jobs:

  build:

    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v3

    - name: Set up QEMU
      uses: docker/setup-qemu-action@v2
      with:
        platforms: linux/amd64,linux/arm64

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

    - name: Create a builder
      run: |
        docker buildx create --use

    - name: Build and push the Docker image
      run: |
        IMAGE_TAG="latest"
        IMAGE_NAME="ghcr.io/${{ github.repository }}"

        echo ${{ secrets.TOKEN_FOR_ACTIONS }} | docker login ghcr.io -u ${{ github.repository_owner }} --password-stdin
        
        docker buildx build --platform linux/amd64,linux/arm64 --file docker/Dockerfile --tag $IMAGE_NAME:$IMAGE_TAG --push .
        echo "IMAGE_TAG=${IMAGE_TAG}" >> $GITHUB_ENV
      if: github.event_name == 'release' || github.event_name == 'workflow_dispatch'