# Every workflow that includes this must also include the following to make sure that it
# is triggered when a new JDK is added to the matrix:
#on:
#  push:
#    paths:
#      - ".github/workflows/reusable-smoke-test-images.yml"

name: PR build fake backend images for smoke tests

on:
  workflow_call:
    inputs:
      project:
        type: string
        required: true
      publish:
        type: boolean
        required: false
      cache-read-only:
        type: boolean
        required: false
      skip-java-8:
        type: boolean
        required: false
      skip-java-17:
        type: boolean
        required: false
      skip-java-21:
        type: boolean
        required: false

permissions:
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      packages: write # for publishing docker image to github packages
    steps:
      - uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2

      - name: Free disk space
        run: .github/scripts/gha-free-disk-space.sh

      - name: Set up JDK for running Gradle
        uses: actions/setup-java@9704b39bf258b59bc04b50fa2dd55e9ed76b47a8 # v4.1.0
        with:
          distribution: temurin
          java-version: 17.0.6

      - name: Login to GitHub package registry
        if: inputs.publish
        uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Set tag
        run: echo "TAG=$(date '+%Y%m%d').$GITHUB_RUN_ID" >> $GITHUB_ENV

      - name: Set up Gradle cache
        uses: gradle/gradle-build-action@29c0906b64b8fc82467890bfb7a0a7ef34bda89e # v3.1.0
        with:
          cache-read-only: ${{ inputs.cache-read-only }}

      - name: Build Java 8 Docker image
        if: "!inputs.skip-java-8"
        run: ./gradlew ${{ inputs.project }}:${{ inputs.publish && 'jib' || 'jibDockerBuild' }} -Ptag=${{ env.TAG }} -PtargetJDK=8 -Djib.httpTimeout=120000 -Djib.console=plain

      - name: Build Java 11 Docker image
        run: ./gradlew ${{ inputs.project }}:${{ inputs.publish && 'jib' || 'jibDockerBuild' }} -Ptag=${{ env.TAG }} -PtargetJDK=11 -Djib.httpTimeout=120000 -Djib.console=plain

      - name: Build Java 17 Docker image
        if: "!inputs.skip-java-17"
        run: ./gradlew ${{ inputs.project }}:${{ inputs.publish && 'jib' || 'jibDockerBuild' }} -Ptag=${{ env.TAG }} -PtargetJDK=17 -Djib.httpTimeout=120000 -Djib.console=plain

      - name: Build Java 21 Docker image
        if: "!inputs.skip-java-21"
        run: ./gradlew ${{ inputs.project }}:${{ inputs.publish && 'jib' || 'jibDockerBuild' }} -Ptag=${{ env.TAG }} -PtargetJDK=21 -Djib.httpTimeout=120000 -Djib.console=plain
