name: Build docker images

on:
  push:
    tags: [ 'v*' ]
  workflow_dispatch:
    inputs:
      version:
        description: 'Release version'
        required: true

jobs:
  build:
    runs-on: ubuntu-22.04

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    - name: Set up QEMU
      uses: docker/setup-qemu-action@v3

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

    - name: Log in to Docker Hub
      uses: docker/login-action@v3
      with:
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}

    - name: Set up version
      id: set-version
      run: |
        if [ "${{ github.event.inputs.version }}" != "" ]; then
          VERSION=${{ github.event.inputs.version }}
        elif [ "${{ github.ref_type }}" == "tag" ]; then
          VERSION=${{ github.ref_name }}
        else
          echo "No version provided and no tag found."
          exit 1
        fi
        echo "VERSION=$VERSION" >> $GITHUB_ENV
        echo "GITHUB REF TYPE: ${{ github.ref_type }}"
        echo "GITHUB REF NAME: ${{ github.ref_name }}"
        echo "EVENT INPUT VERSION: ${{ github.event.inputs.version }}"
        echo "ENV VERSION: $VERSION"

    - name: Build and push Docker image
      uses: docker/build-push-action@v5
      with:
        context: .
        push: true
        platforms: linux/amd64,linux/arm64,linux/arm/v7
        tags: |
          shahanafarooqui/rtl:${{ env.VERSION }}
