name: Publish Servlet images for smoke tests

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

jobs:
  publish:
    runs-on: ${{ matrix.os }}
    defaults:
      run:
        shell: bash # this is needed for TAG construction below on Windows
    strategy:
      matrix:
        os:
          - windows-2019
          - ubuntu-latest
        smoke-test-server:
          - jetty
          - liberty
          - payara
          - tomcat
          - tomee
          - websphere
          - wildfly
        exclude:
          - os: windows-2019
            smoke-test-server: websphere
      fail-fast: false
    steps:
      - name: Support long paths
        run: git config --system core.longpaths true
        if: matrix.os == 'windows-2019'

      - uses: actions/checkout@v3

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

      - name: Login to GitHub package registry
        uses: docker/login-action@v2
        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
        with:
          # only push cache for one matrix option per OS since github action cache space is limited
          cache-read-only: ${{ inputs.cache-read-only || matrix.smoke-test-suite != 'tomcat' }}

      - name: Build Linux docker images
        if: matrix.os != 'windows-2019'
        run: ./gradlew :smoke-tests:images:servlet:buildLinuxTestImages pushMatrix -PextraTag=${{ env.TAG }} -PsmokeTestServer=${{ matrix.smoke-test-server }}

      - name: Build Windows docker images
        if: matrix.os == 'windows-2019'
        run: ./gradlew :smoke-tests:images:servlet:buildWindowsTestImages pushMatrix -PextraTag=${{ env.TAG }} -PsmokeTestServer=${{ matrix.smoke-test-server }}

  open-issue-on-failure:
    needs: publish
    if: failure()
    uses: ./.github/workflows/reusable-open-issue-on-failure.yml
