name: Publish multi-platform docker images

on:
  push:
    tags:
      - 'v[0-9]+.[0-9]+'
      - 'v[0-9]+.[0-9]+.[0-9]+'
      - 'v[0-9]+.[0-9]+[0-9a-z]+'
  workflow_dispatch:
    inputs:
      version:
        description: 'Release version'
        required: true

      repository-name:
        description: 'Docker repository name'
        default: 'elementsproject'
        required: false

      platforms-to-build:
        description: 'List of platforms to build'
        default: 'linux/amd64,linux/arm64,linux/arm/v7'
        required: false
  
      push-latest:
        description: 'Push the latest tag also?'
        default: 'false'
        required: false

jobs:
  build:
    runs-on: ubuntu-22.04
    strategy:
      fail-fast: false    # Let each tag finish.
      matrix:
        include:
          - target: lightningd
            tag_suffix: ''
          - target: lightningd-vls-signer
            tag_suffix: '-vls'

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        ref: ${{ github.ref }}  # Ensures the branch triggering the workflow is checked out
        fetch-depth: 0

    - 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 values for ${{ matrix.target }}
      id: set-values
      run: |
        if [[ "${{ github.event.inputs.version }}" != "" ]]; then
          echo "Input version provided"
          VERSION=${{ github.event.inputs.version }}
        elif [[ ${{ github.ref_type }} == "tag" ]]; then
          echo "This is a tag event"
          VERSION=${{ github.ref_name }}
        else
          echo "No version provided and no tag found."
          exit 1
        fi
        echo "VERSION=$VERSION" >> $GITHUB_ENV

        if [[ "${{ github.event.inputs.repository-name }}" != "" ]]; then
          REPONAME=${{ github.event.inputs.repository-name }}
        else
          REPONAME="elementsproject"
        fi
        echo "REPONAME=$REPONAME" >> $GITHUB_ENV

        if [[ "${{ github.event.inputs.platforms-to-build }}" != "" ]]; then
          PLATFORMS=${{ github.event.inputs.platforms-to-build }}
        else
          PLATFORMS="linux/amd64,linux/arm64,linux/arm/v7"
        fi
        echo "PLATFORMS=$PLATFORMS" >> $GITHUB_ENV

        if [[ "${{ github.event.inputs.push-latest }}" == "true" ]] || 
          ([[ "${{ github.ref_type }}" == "tag" ]] && [[ ! "$VERSION" =~ rc ]]); then
          echo "Latest true"
          PUSHLATEST="true"
        else
          echo "Latest false"
          PUSHLATEST="false"
        fi
        echo "PUSHLATEST=$PUSHLATEST" >> $GITHUB_ENV

        TAGS="$REPONAME/lightningd:$VERSION${{ matrix.tag_suffix }}"
        if [[ "$PUSHLATEST" == "true" ]]; then
          TAGS="$TAGS,$REPONAME/lightningd:latest${{ matrix.tag_suffix }}"
        fi
        echo "TAGS=$TAGS" >> $GITHUB_ENV

    - name: Print GitHub Ref Values
      run: |
        echo "TARGET: ${{ matrix.target }}"
        echo "TAG SUFFIX: ${{ matrix.tag_suffix }}"
        echo "GITHUB REF TYPE: ${{ github.ref_type }}"
        echo "GITHUB REF NAME: ${{ github.ref_name }}"
        echo "EVENT INPUT VERSION: ${{ github.event.inputs.version }}"
        echo "EVENT INPUT REPO: ${{ github.event.inputs.repository-name }}"
        echo "EVENT INPUT PLATFORMS: ${{ github.event.inputs.platforms-to-build }}"
        echo "EVENT INPUT PUSH LATEST: ${{ github.event.inputs.push-latest }}"
        echo "ENV VERSION: ${{ env.VERSION }}"
        echo "ENV REPO NAME: ${{ env.REPONAME }}"
        echo "ENV PLATFORMS: ${{ env.PLATFORMS }}"
        echo "ENV PUSH LATEST: ${{ env.PUSHLATEST }}"
        echo "ENV TAGS: ${{ env.TAGS }}"

    - name: Build and push Docker tag - ${{ env.TAGS }}
      uses: docker/build-push-action@v5
      with:
        context: .
        file: ./Dockerfile
        target: ${{ matrix.target }}
        push: true
        platforms: ${{ env.PLATFORMS }}
        tags: ${{ env.TAGS }}
        build-args: |
          VERSION=${{ env.VERSION }}
        cache-from: type=gha
        cache-to: type=gha,mode=max
