name: 'Stream Applications - Common'

on:
  workflow_call:
    inputs:
      branch:
        type: string
        required: false
        default: 'main'
        description: 'Version Tag'
      verbose:
        type: boolean
        required: false
        default: false
        description: 'Verbose output'
      mavenThreads:
        type: string
        required: false
        default: '1'
        description: 'Maven Thread Option. Examples 0.5C, 2'
    secrets:
      DOCKERHUB_USERNAME:
      DOCKERHUB_TOKEN:
      TMC_API_TOKEN:
      GCP_CRED_JSON:
      ARTIFACTORY_USERNAME:
      ARTIFACTORY_PASSWORD:
      GH_ARC_APP_ID:
      GH_ARC_INSTALLATION_ID:
      GH_ARC_PRIVATE_KEY:
      GH_ARC_PAT:

env:
  VERBOSE: ${{ (github.debug || inputs.verbose) && 'true' || '' }}
  BRANCH: ${{ inputs.branch }}
  MAVEN_THREADS: ${{ inputs.mavenThreads }}

jobs:
  scan:
    if: ${{ github.repository == 'spring-cloud/stream-applications' }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Run Trivy vulnerability scanner in repo mode
        uses: aquasecurity/trivy-action@master
        with:
          scan-type: 'fs'
          ignore-unfixed: true
          format: 'sarif'
          output: 'trivy-results.sarif'
          severity: 'CRITICAL,HIGH'
      - name: Upload Trivy scan results to GitHub Security tab
        uses: github/codeql-action/upload-sarif@v2
        with:
          sarif_file: 'trivy-results.sarif'
      - name: 'Scanned'
        shell: bash
        run: echo "::info ::Scanned"
  parameters:
    name: 'parameters - ${{ inputs.branch }}'
    if: ${{ github.repository == 'spring-cloud/stream-applications' }}
    runs-on: ubuntu-latest
    steps:
      - name: 'Configure: checkout'
        uses: actions/checkout@v3
        with:
          ref: 'main'
      - name: 'Configure: checkout stream-applications@${{ inputs.branch }}'
        uses: actions/checkout@v3
        with:
          ref: ${{ inputs.branch }}
          path: 'stream-applications'
      - name: 'Configure: Ensure scripts are executable'
        shell: bash
        run: find . -type f -name "*.sh" -exec chmod a+x '{}' \;
      - name: 'Configure: create streams applications matrix'
        shell: bash
        run: |
          ROOT_DIR=$(realpath $PWD)          
          
          pushd stream-applications > /dev/null
            echo "::info ::getting build version params"
            source $ROOT_DIR/get-build-version-params.sh
            echo "BUILD_VERSION=$BUILD_VERSION" >> $GITHUB_ENV
            echo "BUILD_VERSION_TYPE=$BUILD_VERSION_TYPE" >> $GITHUB_ENV
            echo "CUR_VERSION=$CUR_VERSION" >> $GITHUB_ENV
            echo "IS_VERSION_CHANGE=$IS_VERSION_CHANGE" >> $GITHUB_ENV
            echo "NEXT_DEV_VERSION=$NEXT_DEV_VERSION" >> $GITHUB_ENV
            echo "RELEASE_TRAIN_VERSION=$RELEASE_TRAIN_VERSION" >> $GITHUB_ENV
            echo "RELEASE_TRAIN_NEXT_DEV_VERSION=$RELEASE_TRAIN_NEXT_DEV_VERSION" >> $GITHUB_ENV
            echo "::info ::creating streams applications matrix"
            $ROOT_DIR/create-matrices.sh
            if [ ! -f matrix.json ]; then
              echo "Expected to find matrix.json in:"
              ls -al
              exit 2
            fi
            COUNT=$(jq '.count' matrix.json)
            MAX_PARALLEL=$((5 * COUNT / 4))
            if ((MAX_PARALLEL == COUNT)); then
              MAX_PARALLEL=$((COUNT + 1))
            fi          
            MATRIX=$(jq -c . matrix.json)
            PROCESSORS=$(jq -c '.processors' matrix.json)
            SINKS=$(jq -c '.sinks' matrix.json)
            SOURCES=$(jq -c '.sources' matrix.json)
            echo "MAX_PARALLEL=$MAX_PARALLEL" >> $GITHUB_ENV
            echo "MATRIX=$MATRIX" >> $GITHUB_ENV
            echo "PROCESSORS=$PROCESSORS" >> $GITHUB_ENV
            echo "SINKS=$SINKS" >> $GITHUB_ENV
            echo "SOURCES=$SOURCES" >> $GITHUB_ENV
          popd
          
          if [[ "${{ inputs.branch }}" == *"2021"* ]]; then
            echo "JDK_BUILD=8" >> $GITHUB_ENV
            echo "JRE_DEFAULT=11" >> $GITHUB_ENV            
          else
            echo "JDK_BUILD=17" >> $GITHUB_ENV
            echo "JRE_DEFAULT=17" >> $GITHUB_ENV
          fi   
          if [ "${{ inputs.verbose }}" == "true" ]; then
            echo "::debug ::MAX_PARALLEL=$MAX_PARALLEL"
            echo "::debug ::MATRIX=$MATRIX"
            echo "::debug ::PROCESSORS=$PROCESSORS"
            echo "::debug ::SINKS=$SINKS"
            echo "::debug ::SOURCES=$SOURCES"
            echo "::debug ::BUILD_VERSION=$BUILD_VERSION"
            echo "::debug ::RELEASE_TRAIN_VERSION=$RELEASE_TRAIN_VERSION"            
            echo "::debug ::BUILD_VERSION_TYPE=$BUILD_VERSION_TYPE"
            echo "::debug ::CUR_VERSION=$CUR_VERSION"
            echo "::debug ::IS_VERSION_CHANGE=$IS_VERSION_CHANGE"
            echo "::debug ::NEXT_DEV_VERSION=$NEXT_DEV_VERSION"
            echo "::debug ::RELEASE_TRAIN_NEXT_DEV_VERSION=$RELEASE_TRAIN_NEXT_DEV_VERSION"
            echo "::debug ::JDK_BUILD=$JDK_BUILD"
            echo "::debug ::JRE_DEFAULT=$JRE_DEFAULT"
          fi
    outputs:
      max_parallel: ${{ env.MAX_PARALLEL }}
      sources: ${{ env.SOURCES }}
      sinks: ${{ env.SINKS }}
      processors: ${{ env.PROCESSORS }}
      matrix: ${{ env.matrix }}
      build_version: ${{ env.BUILD_VERSION }}
      release_train_version: ${{ env.RELEASE_TRAIN_VERSION }}
      build_version_type: ${{ env.BUILD_VERSION_TYPE }}
      is_version_change: ${{ env.IS_VERSION_CHANGE }}
      next_dev_version: ${{ env.NEXT_DEV_VERSION }}
      release_train_next_dev_version: ${{ env.RELEASE_TRAIN_NEXT_DEV_VERSION }}
      jdk_build: ${{ env.JDK_BUILD }}
      jre_version: ${{ env.JRE_DEFAULT }}
  core:
    name: 'core - ${{ inputs.branch }}'
    if: ${{ github.repository == 'spring-cloud/stream-applications' }}
    runs-on: ubuntu-latest
    needs:
      - parameters
    steps:
      - name: 'Configure: checkout stream-applications@main'
        uses: actions/checkout@v3
        with:
          ref: 'main'
      - name: 'Configure: checkout stream-applications@${{ inputs.branch }}'
        uses: actions/checkout@v3
        with:
          ref: ${{ inputs.branch }}
          path: 'stream-applications'
      - name: 'Configure: Ensure scripts are executable'
        shell: bash
        run: find . -type f -name "*.sh" -exec chmod a+x '{}' \;
      - name: 'Configure: Install Java'
        if: ${{ needs.parameters.outputs.jdk_build == '8' }}
        uses: actions/setup-java@v1
        with:
          java-version: ${{ needs.parameters.outputs.jdk_build }}
      - name: 'Configure: Install GraalVM'
        if: ${{ needs.parameters.outputs.jdk_build != '8' }}
        uses: graalvm/setup-graalvm@v1
        with:
          version: 'latest'
          java-version: ${{ needs.parameters.outputs.jdk_build }}
          components: 'js'
          github-token: ${{ secrets.GITHUB_TOKEN }}
      - name: 'Configure: cache for maven dependencies'
        uses: actions/cache@v3
        with:
          path: ~/.m2/repository
          key: maven-repo-${{ hashFiles('**/pom.xml') }}-8-single-${{ inputs.branch }}
          restore-keys: |
            maven-repo-${{ hashFiles('**/pom.xml') }}-8-${{ inputs.branch }}
            maven-repo-${{ hashFiles('**/pom.xml') }}-${{ inputs.branch }}
            maven-repo-${{ inputs.branch }}
      - name: 'Configure: Install xsltproc'
        uses: ./.github/actions/install-xsltproc
      - name: 'Configure: Install gh cli'
        uses: ./.github/actions/install-gh
      - name: 'Action: update version'
        if: ${{ needs.parameters.outputs.is_version_change == 'true' }}
        shell: bash
        env:
          VERBOSE: ${{ (github.debug || inputs.verbose) && '-X' || '' }}
        run: |
          ROOT_DIR=$(realpath $PWD)
          pushd stream-applications >/dev/null
            echo "::notice ::updating versions to ${{ needs.parameters.outputs.build_version }} / ${{ needs.parameters.outputs.release_train_version }}"
            $ROOT_DIR/update-project-version.sh "${{ needs.parameters.outputs.build_version }}" "${{ needs.parameters.outputs.release_train_version }}"                         
            echo "::notice ::checking updated versions"
            set +e
            MESSAGE=$($ROOT_DIR/check-versions-for-release.sh ${{ needs.parameters.outputs.build_version_type }})
            RC=$?
            if ((RC!=0)); then
              echo "::error ::$MESSAGE"
              exit $RC
            else
              echo "::info ::$MESSAGE"
            fi
          popd > /dev/null
      - name: 'Action: build initial dependencies'
        shell: bash
        timeout-minutes: 30
        env:
          ARTIFACTORY_USERNAME: ${{ secrets.ARTIFACTORY_USERNAME }}
          ARTIFACTORY_PASSWORD: ${{ secrets.ARTIFACTORY_PASSWORD }}
          VERBOSE: ${{ (github.debug || inputs.verbose) && 'true' || '' }}
          MAVEN_THREADS: ${{ inputs.mavenThreads }}
        run: |
          ROOT_DIR=$(realpath $PWD)
          pushd stream-applications > /dev/null
          echo "::notice ::building - stream-applications-build,functions,applications/stream-applications-core"
          set -e
          $ROOT_DIR/build-core.sh "install -DskipTests"
          export MAVEN_THREADS=false
          $ROOT_DIR/build-core.sh "verify deploy"
          set +e
          echo "::notice ::core build completed"
          popd > /dev/null
      - name: 'Upload: Test Reports'
        if: ${{ always() }}
        uses: actions/upload-artifact@v3
        with:
          name: 'core-surefire-reports'
          path: 'stream-applications/**/target/surefire-reports'
          retention-days: 7
          if-no-files-found: ignore
      - name: 'Action: Re-run ${{ github.workflow }}'
        if: ${{ failure() }}
        shell: bash
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh run view ${{ github.run_id }} -v | grep "^X"
          FAILURES=$(gh run view ${{ github.run_id }} -v | grep "^X")
          COUNT_TESTS=$(echo "$FAILURES" | grep -c -F "tests ")
          if((COUNT_TESTS > 0)); then
            echo "::info ::Re-run ${{ github.workflow }} run ${{ github.run_id }}"
            gh run rerun ${{ github.run_id }} --failed --debug
          else
            echo "Checked ${{ github.workflow }} run ${{ github.run_id }}. No failures in tests jobs:"
            for msg in $FAILURES; do
              echo "$msg"
            done
          fi
  processors:
    if: ${{ github.repository == 'spring-cloud/stream-applications' && needs.parameters.outputs.processors != '' && needs.parameters.outputs.processors != null }}
    needs:
      - core
      - parameters
    strategy:
      fail-fast: false
      matrix:
        app: ${{ fromJson(needs.parameters.outputs.processors) }}
    runs-on: ubuntu-latest
    steps:
      - name: 'Configure: checkout stream-applications'
        uses: actions/checkout@v3
        with:
          ref: 'main'
      - name: 'Configure: checkout stream-applications@${{ inputs.branch }}'
        uses: actions/checkout@v3
        with:
          ref: ${{ inputs.branch }}
          path: 'stream-applications'
      - name: Ensure scripts are executable
        shell: bash
        run: find . -type f -name "*.sh" -exec chmod a+x '{}' \;
      - name: 'Configure: Install Java'
        if: ${{ needs.parameters.outputs.jdk_build == '8' }}
        uses: actions/setup-java@v1
        with:
          java-version: ${{ needs.parameters.outputs.jdk_build }}
      - name: 'Configure: Install GraalVM'
        if: ${{ needs.parameters.outputs.jdk_build != '8' }}
        uses: graalvm/setup-graalvm@v1
        with:
          version: 'latest'
          java-version: ${{ needs.parameters.outputs.jdk_build }}
          components: 'js'
          github-token: ${{ secrets.GITHUB_TOKEN }}
      - name: 'Configure: cache for maven dependencies'
        uses: actions/cache@v3
        with:
          path: ~/.m2/repository
          key: maven-repo-${{ hashFiles('**/pom.xml') }}-8-single-${{ inputs.branch }}
          restore-keys: |
            maven-repo-${{ hashFiles('**/pom.xml') }}-8-${{ inputs.branch }}
            maven-repo-${{ hashFiles('**/pom.xml') }}-${{ inputs.branch }}
            maven-repo-${{ inputs.branch }}
      - name: 'Configure: Install xsltproc'
        uses: ./.github/actions/install-xsltproc
      - name: 'Configure: Install Pack'
        uses: ./.github/actions/install-pack
      - name: 'Configure: Check Docker Credentials'
        shell: bash
        run: |
          if [ "${{ secrets.DOCKERHUB_USERNAME }}" = "" ]; then
            echo "Expected DOCKERHUB_USERNAME"
            exit 2
          fi
          if [ "${{ secrets.DOCKERHUB_TOKEN }}" = "" ]; then
            echo "Expected DOCKERHUB_TOKEN"
            exit 2
          fi
      - name: 'Action: update version'
        if: ${{ needs.parameters.outputs.is_version_change == 'true' }}
        shell: bash
        env:
          VERBOSE: ${{ (github.debug || inputs.verbose) && '-X' || '' }}
        run: |
          ROOT_DIR=$(realpath $PWD)
          pushd stream-applications >/dev/null
            echo "::notice ::updating versions to ${{ needs.parameters.outputs.build_version }} / ${{ needs.parameters.outputs.release_train_version }}"
            $ROOT_DIR/update-project-version.sh "${{ needs.parameters.outputs.build_version }}" "${{ needs.parameters.outputs.release_train_version }}"                        
            echo "::notice ::checking updated versions"
            set +e
            MESSAGE=$($ROOT_DIR/check-versions-for-release.sh ${{ needs.parameters.outputs.build_version_type }})
            RC=$?
            if ((RC!=0)); then
              echo "::error ::$MESSAGE"
              exit $RC
            else
              echo "::info ::$MESSAGE"
            fi
          popd > /dev/null
      - name: 'Configure: Docker login'
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: 'Build: ${{ matrix.app }}'
        shell: bash
        timeout-minutes: 30
        env:
          ARTIFACTORY_USERNAME: ${{ secrets.ARTIFACTORY_USERNAME }}
          ARTIFACTORY_PASSWORD: ${{ secrets.ARTIFACTORY_PASSWORD }}
          DEFAULT_JDK: ${{ needs.parameters.outputs.jre_version }}
          VERBOSE: ${{ (github.debug || inputs.verbose) && 'true' || '' }}
          MAVEN_THREADS: ${{ inputs.mavenThreads }}
        run: ./build-app.sh "stream-applications" "applications/processor/${{ matrix.app }}"
      - name: 'Upload: Test Reports'
        if: ${{ always() }}
        uses: actions/upload-artifact@v3
        with:
          name: '${{ matrix.app }}-surefire-reports'
          path: '**/target/surefire-reports'
          retention-days: 7
          if-no-files-found: ignore
      - name: 'Publish: ${{ matrix.app }}'
        shell: bash
        env:
          DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
          DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
          DEFAULT_JDK: ${{ needs.parameters.outputs.jre_version }}
        run: ./publish-app.sh "stream-applications" "applications/processor/${{ matrix.app }}"
  sinks:
    if: ${{ github.repository == 'spring-cloud/stream-applications' && needs.parameters.outputs.sinks != '' && needs.parameters.outputs.sinks != null }}
    needs:
      - core
      - parameters
    strategy:
      fail-fast: false
      matrix:
        app: ${{ fromJson(needs.parameters.outputs.sinks) }}
    runs-on: ubuntu-latest
    steps:
      - name: 'Configure: checkout stream-applications'
        uses: actions/checkout@v3
        with:
          ref: 'main'
      - name: 'Configure: checkout stream-applications@${{ inputs.branch }}'
        uses: actions/checkout@v3
        with:
          ref: ${{ inputs.branch }}
          path: 'stream-applications'
      - name: Ensure scripts are executable
        shell: bash
        run: find . -type f -name "*.sh" -exec chmod a+x '{}' \;
      - name: 'Configure: Install Java'
        if: ${{ needs.parameters.outputs.jdk_build == '8' }}
        uses: actions/setup-java@v1
        with:
          java-version: ${{ needs.parameters.outputs.jdk_build }}
      - name: 'Configure: Install GraalVM'
        if: ${{ needs.parameters.outputs.jdk_build != '8' }}
        uses: graalvm/setup-graalvm@v1
        with:
          version: 'latest'
          java-version: ${{ needs.parameters.outputs.jdk_build }}
          components: 'js'
          github-token: ${{ secrets.GITHUB_TOKEN }}
      - name: 'Configure: cache for maven dependencies'
        uses: actions/cache@v3
        with:
          path: ~/.m2/repository
          key: maven-repo-${{ hashFiles('**/pom.xml') }}-8-single-${{ inputs.branch }}
          restore-keys: |
            maven-repo-${{ hashFiles('**/pom.xml') }}-8-${{ inputs.branch }}
            maven-repo-${{ hashFiles('**/pom.xml') }}-${{ inputs.branch }}
            maven-repo-${{ inputs.branch }}
      - name: 'Configure: Install xsltproc'
        uses: ./.github/actions/install-xsltproc
      - name: 'Configure: Install Pack'
        uses: ./.github/actions/install-pack
      - name: 'Action: update version'
        if: ${{ needs.parameters.outputs.is_version_change == 'true' }}
        shell: bash
        env:
          VERBOSE: ${{ (github.debug || inputs.verbose) && '-X' || '' }}
        run: |
          ROOT_DIR=$(realpath $PWD)
          pushd stream-applications >/dev/null
            echo "::notice ::updating versions to ${{ needs.parameters.outputs.build_version }} / ${{ needs.parameters.outputs.release_train_version }}"
            $ROOT_DIR/update-project-version.sh "${{ needs.parameters.outputs.build_version }}" "${{ needs.parameters.outputs.release_train_version }}"                        
            echo "::notice ::checking updated versions"
            set +e
            MESSAGE=$($ROOT_DIR/check-versions-for-release.sh ${{ needs.parameters.outputs.build_version_type }})
            RC=$?
            if ((RC!=0)); then
              echo "::error ::$MESSAGE"
              exit $RC
            else
              echo "::info ::$MESSAGE"
            fi
          popd > /dev/null
      - name: 'Configure: Docker login'
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: 'Build: ${{ matrix.app }}'
        shell: bash
        timeout-minutes: 30
        env:
          ARTIFACTORY_USERNAME: ${{ secrets.ARTIFACTORY_USERNAME }}
          ARTIFACTORY_PASSWORD: ${{ secrets.ARTIFACTORY_PASSWORD }}
          DEFAULT_JDK: ${{ needs.parameters.outputs.jre_version }}
          VERBOSE: ${{ (github.debug || inputs.verbose) && 'true' || '' }}
          MAVEN_THREADS: ${{ inputs.mavenThreads }}
        run: ./build-app.sh "stream-applications" "applications/sink/${{ matrix.app }}"
      - name: 'Upload: Test Reports'
        if: ${{ always() }}
        uses: actions/upload-artifact@v3
        with:
          name: '${{ matrix.app }}-surefire-reports'
          path: '**/target/surefire-reports'
          retention-days: 7
          if-no-files-found: ignore
      - name: 'Publish: ${{ matrix.app }}'
        shell: bash
        env:
          DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
          DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
          DEFAULT_JDK: ${{ needs.parameters.outputs.jre_version }}
        run: ./publish-app.sh "stream-applications" "applications/sink/${{ matrix.app }}"
  sources:
    if: ${{ github.repository == 'spring-cloud/stream-applications' && needs.parameters.outputs.sources != '' && needs.parameters.outputs.sources != null }}
    runs-on: ubuntu-latest
    needs:
      - core
      - parameters
    strategy:
      fail-fast: false
      # will be removed when private runners are used.
      matrix:
        app: ${{ fromJson(needs.parameters.outputs.sources) }}
    steps:
      - name: 'Configure: checkout stream-applications'
        uses: actions/checkout@v3
        with:
          ref: 'main'
      - name: 'Configure: checkout stream-applications@${{ inputs.branch }}'
        uses: actions/checkout@v3
        with:
          ref: ${{ inputs.branch }}
          path: 'stream-applications'
      - name: Ensure scripts are executable
        shell: bash
        run: find . -type f -name "*.sh" -exec chmod a+x '{}' \;
      - name: 'Configure: Install Java'
        if: ${{ needs.parameters.outputs.jdk_build == '8' }}
        uses: actions/setup-java@v1
        with:
          java-version: ${{ needs.parameters.outputs.jdk_build }}
      - name: 'Configure: Install GraalVM'
        if: ${{ needs.parameters.outputs.jdk_build != '8' }}
        uses: graalvm/setup-graalvm@v1
        with:
          version: 'latest'
          java-version: ${{ needs.parameters.outputs.jdk_build }}
          components: 'js'
          github-token: ${{ secrets.GITHUB_TOKEN }}
      - name: 'Configure: cache for maven dependencies'
        uses: actions/cache@v3
        with:
          path: ~/.m2/repository
          key: maven-repo-${{ hashFiles('**/pom.xml') }}-8-single-${{ inputs.branch }}
          restore-keys: |
            maven-repo-${{ hashFiles('**/pom.xml') }}-8-${{ inputs.branch }}
            maven-repo-${{ hashFiles('**/pom.xml') }}-${{ inputs.branch }}
            maven-repo-${{ inputs.branch }}
      - name: 'Configure: Install xsltproc'
        uses: ./.github/actions/install-xsltproc
      - name: 'Configure: Install Pack'
        uses: ./.github/actions/install-pack
      - name: 'Action: update version'
        if: ${{ needs.parameters.outputs.is_version_change == 'true' }}
        shell: bash
        env:
          VERBOSE: ${{ (github.debug || inputs.verbose) && '-X' || '' }}
        run: |
          ROOT_DIR=$(realpath $PWD)
          pushd stream-applications >/dev/null
            echo "::notice ::updating versions to ${{ needs.parameters.outputs.build_version }} / ${{ needs.parameters.outputs.release_train_version }}"
            $ROOT_DIR/update-project-version.sh "${{ needs.parameters.outputs.build_version }}" "${{ needs.parameters.outputs.release_train_version }}"                        
            echo "::notice ::checking updated versions"
            set +e
            MESSAGE=$($ROOT_DIR/check-versions-for-release.sh ${{ needs.parameters.outputs.build_version_type }})
            RC=$?
            if ((RC!=0)); then
              echo "::error ::$MESSAGE"
              exit $RC
            else
              echo "::info ::$MESSAGE"
            fi
          popd > /dev/null
      - name: 'Configure: Docker login'
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: 'Build: ${{ matrix.app }}'
        shell: bash
        timeout-minutes: 30
        env:
          ARTIFACTORY_USERNAME: ${{ secrets.ARTIFACTORY_USERNAME }}
          ARTIFACTORY_PASSWORD: ${{ secrets.ARTIFACTORY_PASSWORD }}
          DEFAULT_JDK: ${{ needs.parameters.outputs.jre_version }}
          VERBOSE: ${{ (github.debug || inputs.verbose) && 'true' || '' }}
          MAVEN_THREADS: ${{ inputs.mavenThreads }}
        run: ./build-app.sh "stream-applications" "applications/source/${{ matrix.app }}"
      - name: 'Upload: Test Reports'
        if: ${{ always() }}
        uses: actions/upload-artifact@v3
        with:
          name: '${{ matrix.app }}-surefire-reports'
          path: '**/target/surefire-reports'
          retention-days: 7
          if-no-files-found: ignore
      - name: 'Publish: ${{ matrix.app }}'
        shell: bash
        env:
          DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
          DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
          DEFAULT_JDK: ${{ needs.parameters.outputs.jre_version }}
        run: ./publish-app.sh "stream-applications" "applications/source/${{ matrix.app }}"
  release-train:
    if: ${{ github.repository == 'spring-cloud/stream-applications' }}
    needs:
      - core
      - sinks
      - sources
      - processors
      - parameters
    runs-on: ubuntu-latest
    steps:
      - name: 'Configure: checkout stream-applications'
        uses: actions/checkout@v3
        with:
          ref: 'main'
      - name: 'Configure: checkout stream-applications@${{ inputs.branch }}'
        uses: actions/checkout@v3
        with:
          ref: ${{ inputs.branch }}
          path: 'stream-applications'
      - name: Ensure scripts are executable
        shell: bash
        run: find . -type f -name "*.sh" -exec chmod a+x '{}' \;
      - name: 'Configure: Install Java'
        if: ${{ needs.parameters.outputs.jdk_build == '8' }}
        uses: actions/setup-java@v1
        with:
          java-version: ${{ needs.parameters.outputs.jdk_build }}
      - name: 'Configure: Install GraalVM'
        if: ${{ needs.parameters.outputs.jdk_build != '8' }}
        uses: graalvm/setup-graalvm@v1
        with:
          version: 'latest'
          java-version: ${{ needs.parameters.outputs.jdk_build }}
          components: 'js'
          github-token: ${{ secrets.GITHUB_TOKEN }}
      - name: 'Configure: cache for maven dependencies'
        uses: actions/cache@v3
        with:
          path: ~/.m2/repository
          key: maven-repo-${{ hashFiles('**/pom.xml') }}-8-single-${{ inputs.branch }}
          restore-keys: |
            maven-repo-${{ hashFiles('**/pom.xml') }}-8-${{ inputs.branch }}
            maven-repo-${{ hashFiles('**/pom.xml') }}-${{ inputs.branch }}
            maven-repo-${{ inputs.branch }}
      - name: 'Configure: Install Pack'
        uses: ./.github/actions/install-pack
      - name: 'Action: update version'
        if: ${{ needs.parameters.outputs.is_version_change == 'true' }}
        shell: bash
        env:
          VERBOSE: ${{ (github.debug || inputs.verbose) && '-X' || '' }}
        run: |
          ROOT_DIR=$(realpath $PWD)
          pushd stream-applications >/dev/null
            echo "::notice ::updating versions to ${{ needs.parameters.outputs.build_version }} / ${{ needs.parameters.outputs.release_train_version }}"
            $ROOT_DIR/update-project-version.sh "${{ needs.parameters.outputs.build_version }}" "${{ needs.parameters.outputs.release_train_version }}"                        
            echo "::notice ::checking updated versions"
            set +e
            MESSAGE=$($ROOT_DIR/check-versions-for-release.sh ${{ needs.parameters.outputs.build_version_type }})
            RC=$?
            if ((RC!=0)); then
              echo "::error ::$MESSAGE"
              exit $RC
            else
              echo "::info ::$MESSAGE"
            fi          
          popd > /dev/null
      - name: 'Build: stream-applications-release-train'
        shell: bash
        timeout-minutes: 30
        env:
          ARTIFACTORY_USERNAME: ${{ secrets.ARTIFACTORY_USERNAME }}
          ARTIFACTORY_PASSWORD: ${{ secrets.ARTIFACTORY_PASSWORD }}
          DEFAULT_JDK: ${{ needs.parameters.outputs.jre_version }}
          VERBOSE: ${{ (github.debug || inputs.verbose) && 'true' || '' }}
          MAVEN_THREADS: ${{ inputs.mavenThreads }}
        run: |
          ROOT_DIR=$(realpath $PWD)
          pushd stream-applications > /dev/null
            echo "::notice ::building - stream-applications-release-train"
            set -e
            $ROOT_DIR/build-folder.sh stream-applications-release-train "install deploy"                                     
          popd > /dev/null
      - name: 'Configure: Install libxml2-utils'
        uses: ./.github/actions/install-libxml2-utils
      - uses: jfrog/setup-jfrog-cli@v3
        env:
          JF_URL: 'https://repo.spring.io'
          JF_ACCESS_TOKEN: '${{ secrets.ARTIFACTORY_PASSWORD }}'
          JF_USER: '${{ secrets.ARTIFACTORY_USERNAME }}'
          JF_PASSWORD: '${{ secrets.ARTIFACTORY_PASSWORD }}'
      - name: Set Stream Applications Docs Properties for ${{ needs.parameters.outputs.release_train_version }}
        run: |
          ./set-doc-properties.sh "${{ needs.parameters.outputs.release_train_version }}"                    
          echo "::notice ::stream-applications-release-train build completed"
      - name: 'Upload: Test Reports'
        if: ${{ always() }}
        uses: actions/upload-artifact@v3
        with:
          name: '${{ matrix.app }}-surefire-reports'
          path: '**/target/surefire-reports'
          retention-days: 7
          if-no-files-found: ignore
      # set all properties for buildName and buildNumber using a list of all built artifacts and version.
      # use frog download of all builds.
      # use nexus-sync
      - name: Tag Release
        if: ${{ needs.parameters.outputs.is_version_change == 'true' }}
        shell: bash
        run: |          
          pushd stream-applications > /dev/null
            git config user.name "GitHub Actions Bot"
            git config user.email "<>"
            git commit -a -m "Release ${{ needs.parameters.outputs.build_version }} / ${{ needs.parameters.outputs.release_train_version }}"
            git tag "v${{ needs.parameters.outputs.build_version }}"
            git tag "v${{ needs.parameters.outputs.release_train_version }}"
            git push --tags -v origin
          popd > /dev/null
      # add jfrog download
      # nexus-sync
      - name: 'Action: update next dev version'
        if: ${{ needs.parameters.outputs.is_version_change == 'true' }}
        shell: bash
        env:
          VERBOSE: ${{ (github.debug || inputs.verbose) && '-X' || '' }}
        run: |
          ROOT_DIR=$(realpath $PWD)
          pushd stream-applications >/dev/null
            git config user.name "GitHub Actions Bot"
            git config user.email "<>"
            echo "::notice ::updating next dev versions to ${{ needs.parameters.outputs.next_dev_version }} / ${{ needs.parameters.outputs.release_train_next_dev_version }}"
            $ROOT_DIR/update-project-version.sh "${{ needs.parameters.outputs.next_dev_version }}" "${{ needs.parameters.outputs.release_train_next_dev_version }}"
            echo "::notice ::setting released version to next dev version ${{ needs.parameters.outputs.next_dev_version }} / ${{ needs.parameters.outputs.release_train_next_dev_version }}"
            ./next-dev-version.sh
            git commit -a -m "Next development version ${{ needs.parameters.outputs.next_dev_version }} / ${{ needs.parameters.outputs.release_train_next_dev_version }}"
            git push -v origin 
          popd > /dev/null
  rerun-jobs:
    if: ${{ failure() && github.run_attempt == '1' }}
    runs-on: ubuntu-latest
    needs:
      - scan
      - sources
      - processors
      - sinks
      - release-train
    steps:
      - name: 'Configure: Checkout stream-applications'
        uses: actions/checkout@v3
      - name: 'Configure: Install gh cli'
        uses: ./.github/actions/install-gh
      - name: 'Action: Re-run ${{ github.workflow }}'
        shell: bash
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          set +e
          FAILURES=$(gh run view ${{ github.run_id }} -v | grep "^X")
          for msg in $FAILURES; do
            echo "$msg"
          done
          COUNT_TESTS=$(echo "$FAILURES" | grep -c -F "tests ")
          if((COUNT_TESTS > 0)); then
            echo "::warn ::${COUNT_TESTS} modules failed. Re-run ${{ github.workflow }} run ${{ github.run_id }}"
            gh run rerun ${{ github.run_id }} --failed --debug
          else
            echo "Checked ${{ github.workflow }} run ${{ github.run_id }}. No failures in tests jobs:"
          fi
