name: 'Stream Applications - Descriptors'

on:
  workflow_dispatch:
    inputs:
      verbose:
        type: boolean
        required: false
        default: false
        description: Verbose output
      branch:
        type: string
        required: false
        default: 'main'
        description: Branch

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

jobs:
  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 }}
  release-train:
    if: ${{ github.repository == 'spring-cloud/stream-applications' }}
    needs:
      - 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'
        uses: actions/setup-java@v4
        with:
          java-version: ${{ needs.parameters.outputs.jdk_build }}
          distribution: 'liberica'
      - 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
      - uses: jfrog/setup-jfrog-cli@v3
        env:
          JF_URL: 'https://repo.spring.io'
          JF_ARTIFACTORY_SPRING: ${{ secrets.JF_ARTIFACTORY_SPRING }}
      - 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 }}
          BUILD_VERSION_TYPE: ${{ needs.parameters.outputs.build_version_type }}
        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
      - name: Upload descriptors
        if: ${{ needs.parameters.outputs.build_version_type == 'snapshot' }}
        shell: bash
        env:
          RELEASE_TRAIN_VERSION: ${{ needs.parameters.outputs.release_train_version }}
        run: |
          ROOT_DIR=$(realpath $PWD)
          pushd stream-applications > /dev/null
            echo "::notice ::uploading unique version snapshot descriptors"
            set -e
            SRC_ROOT=$ROOT_DIR/stream-applications/stream-applications-release-train/stream-applications-descriptor/target/classes/META-INF
            TARGET_REPO=libs-snapshot-local/org/springframework/cloud/stream/app/stream-applications-descriptor/${RELEASE_TRAIN_VERSION}
            jfrog rt upload "$SRC_ROOT/kafka-apps-maven.properties" "${TARGET_REPO}/stream-applications-descriptor-${RELEASE_TRAIN_VERSION}.stream-apps-kafka-maven"
            jfrog rt upload "$SRC_ROOT/kafka-apps-docker.properties" "${TARGET_REPO}/stream-applications-descriptor-${RELEASE_TRAIN_VERSION}.stream-apps-kafka-docker"
            jfrog rt upload "$SRC_ROOT/kafka-apps-harbor.properties" "${TARGET_REPO}/stream-applications-descriptor-${RELEASE_TRAIN_VERSION}.stream-apps-kafka-harbor"
            jfrog rt upload "$SRC_ROOT/rabbit-apps-maven.properties" "${TARGET_REPO}/stream-applications-descriptor-${RELEASE_TRAIN_VERSION}.stream-apps-rabbit-maven"
            jfrog rt upload "$SRC_ROOT/rabbit-apps-docker.properties" "${TARGET_REPO}/stream-applications-descriptor-${RELEASE_TRAIN_VERSION}.stream-apps-rabbit-docker"
            jfrog rt upload "$SRC_ROOT/rabbit-apps-harbor.properties" "${TARGET_REPO}/stream-applications-descriptor-${RELEASE_TRAIN_VERSION}.stream-apps-rabbit-harbor"
            jfrog rt upload "$SRC_ROOT/kafka-apps-maven-repo-url.properties" "${TARGET_REPO}/stream-applications-descriptor-${RELEASE_TRAIN_VERSION}.kafka-apps-maven-repo-url.properties"
            jfrog rt upload "$SRC_ROOT/rabbit-apps-maven-repo-url.properties" "${TARGET_REPO}/stream-applications-descriptor-${RELEASE_TRAIN_VERSION}.rabbit-apps-maven-repo-url.properties"
          popd > /dev/null
