name: Publish Play images for smoke tests

on:
  push:
    paths:
      - 'smoke-tests/images/play/**'
      - '.github/workflows/publish-smoke-test-play-images.yml'
    branches:
      - main
  workflow_dispatch:

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Set up JDK 11 for running Gradle
        uses: actions/setup-java@v2
        with:
          distribution: temurin
          java-version: 11

      - name: Login to GitHub Package Registry
        uses: docker/login-action@v1.10.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@v2

      - name: Build Java 8 Docker Image
        run: ./gradlew jib -PtargetJDK=8 -Djib.httpTimeout=120000 -Djib.console=plain -Ptag=${{ env.TAG }}
        working-directory: smoke-tests/images/play

      - name: Build Java 11 Docker Image
        run: ./gradlew jib -PtargetJDK=11 -Djib.httpTimeout=120000 -Djib.console=plain -Ptag=${{ env.TAG }}
        working-directory: smoke-tests/images/play

        # Play doesn't support Java 16 (or 17) yet
        # https://github.com/playframework/playframework/pull/10819
      - name: Build Java 15 Docker Image
        run: ./gradlew jib -PtargetJDK=15 -Djib.httpTimeout=120000 -Djib.console=plain -Ptag=${{ env.TAG }}
        working-directory: smoke-tests/images/play

  issue:
    name: Open issue on failure
    needs: publish
    runs-on: ubuntu-latest
    if: always()
    steps:
      # run this action to get workflow conclusion
      # You can get conclusion by env (env.WORKFLOW_CONCLUSION)
      - uses: technote-space/workflow-conclusion-action@v2.2

      - uses: actions/checkout@v3

      - uses: JasonEtco/create-an-issue@v2.6
        if: env.WORKFLOW_CONCLUSION == 'failure' # notify only if failure
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          filename: .github/templates/workflow-failed.md
