name: Apps Plugin (Release)

on:
  workflow_dispatch:

env:
  MAVEN_THREADS: '-T 0.5C'

jobs:
  build_deploy_release:
    if: ${{ github.repository == 'spring-cloud/stream-applications' }}
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: spring-cloud-dataflow-apps-plugin
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-java@v3
        with:
          java-version: 17
          distribution: temurin
      - uses: jfrog/setup-jfrog-cli@v3
        env:
          JF_URL: 'https://repo.spring.io'
          JF_ENV_SPRING: ${{ secrets.JF_ARTIFACTORY_SPRING }}
      - uses: actions/cache@v2
        with:
          path: ~/.m2/repository
          key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
          restore-keys: |
            ${{ runner.os }}-m2-
      - name: Configure JFrog Cli
        run: |
          jfrog mvnc --use-wrapper \
            --server-id-resolve=${{ vars.JF_SERVER_ID }} \
            --server-id-deploy=${{ vars.JF_SERVER_ID }} \
            --repo-resolve-releases=libs-release-staging \
            --repo-resolve-snapshots=libs-snapshot \
            --repo-deploy-releases=libs-staging-local \
            --repo-deploy-snapshots=libs-snapshot-local
          echo JFROG_CLI_BUILD_NAME=spring-cloud-dataflow-apps-plugin-main-release >> $GITHUB_ENV
          echo JFROG_CLI_BUILD_NUMBER=$GITHUB_RUN_NUMBER >> $GITHUB_ENV
      - name: Print Build Info
        run: |
          echo "::notice ::JFROG_CLI_BUILD_NAME=$JFROG_CLI_BUILD_NAME"
          echo "::notice ::JFROG_CLI_BUILD_NUMBER=$JFROG_CLI_BUILD_NUMBER"
      - name: Build and Publish
        run: |
          jfrog rt mvn build-helper:parse-version versions:set \
            -gs settings.xml \
            -Pstagingrelease \
            -DprocessAllModules=true \
            -DgenerateBackupPoms=false \
            -Dartifactory.publish.artifacts=false \
            -DnewVersion='${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.incrementalVersion}' \
            -B
          jfrog rt build-clean
          jfrog mvn -gs settings.xml -Pstagingrelease,full,deploymentfiles -B clean install -DskipTests 
          jfrog rt build-publish
          echo spring_cloud_dataflow_apps_plugin_version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout) >> $GITHUB_ENV
      - name: Tag Release
        uses: jvalkeal/build-zoo-handler@v0.0.4
        with:
          tag-release-branch: apps-plugin/v${{ env.spring_cloud_dataflow_apps_plugin_version }}
          tag-release-tag: ${{ env.spring_cloud_dataflow_apps_plugin_version }}
          tag-release-tag-prefix: apps-plugin/v
      - name: Promote Build
        run: |
          jfrog rt build-promote $JFROG_CLI_BUILD_NAME $JFROG_CLI_BUILD_NUMBER libs-release-local
      - name: Clean cache
        run: |
          find ~/.m2/repository -type d -name '*SNAPSHOT' | xargs rm -fr
