name: Central Release

on:
  workflow_dispatch:
    inputs:
      build-zoo-handler:
        description: 'Build Zoo Handler Payload'
        required: true

jobs:
  central:
    runs-on: ubuntu22-8-32
    environment: central
    steps:

    # to get spec file in .github
    - uses: actions/checkout@v2

    # Setup jfrog cli
    - uses: jfrog/setup-jfrog-cli@v1
      with:
        version: 1.46.4
      env:
        JF_ARTIFACTORY_SPRING: ${{ secrets.JF_ARTIFACTORY_SPRING }}
    # zoo extract and ensure
    - name: Extract Zoo Context Properties
      uses: jvalkeal/build-zoo-handler@v0.0.4
      with:
        dispatch-handler-extract-context-properties: true
        ensure-env: |
          BUILD_ZOO_HANDLER_spring_cloud_deployer_buildname
          BUILD_ZOO_HANDLER_spring_cloud_deployer_buildnumber
          BUILD_ZOO_HANDLER_spring_cloud_dataflow_ui_buildname
          BUILD_ZOO_HANDLER_spring_cloud_dataflow_ui_buildnumber
          BUILD_ZOO_HANDLER_spring_cloud_dataflow_buildname
          BUILD_ZOO_HANDLER_spring_cloud_dataflow_buildnumber

    # Download released files
    - name: Download Release Files
      run: |
        jfrog rt download \
          --spec .github/release-files-spec.json \
          --spec-vars "buildname=$BUILD_ZOO_HANDLER_spring_cloud_deployer_buildname;buildnumber=$BUILD_ZOO_HANDLER_spring_cloud_deployer_buildnumber"
        jfrog rt download \
          --spec .github/release-files-spec.json \
          --spec-vars "buildname=$BUILD_ZOO_HANDLER_spring_cloud_dataflow_ui_buildname;buildnumber=$BUILD_ZOO_HANDLER_spring_cloud_dataflow_ui_buildnumber"
        jfrog rt download \
          --spec .github/release-files-spec.json \
          --spec-vars "buildname=$BUILD_ZOO_HANDLER_spring_cloud_dataflow_buildname;buildnumber=$BUILD_ZOO_HANDLER_spring_cloud_dataflow_buildnumber"

    # Create checksums, signatures and create staging repo on central and upload
    - uses: jvalkeal/nexus-sync@v0
      with:
        url: ${{ secrets.OSSRH_URL }}
        username: ${{ secrets.OSSRH_S01_TOKEN_USERNAME }}
        password: ${{ secrets.OSSRH_S01_TOKEN_PASSWORD }}
        staging-profile-name: ${{ secrets.OSSRH_STAGING_PROFILE_NAME }}
        create: true
        upload: true
        close: true
        release: true
        generate-checksums: true
        pgp-sign: true
        pgp-sign-passphrase: ${{ secrets.GPG_PASSPHRASE }}
        pgp-sign-private-key: ${{ secrets.GPG_PRIVATE_KEY }}

    # zoo success
    - name: Notify Build Success Zoo Handler Controller
      uses: jvalkeal/build-zoo-handler@v0.0.4
      with:
        dispatch-handler-token: ${{ secrets.SCDF_ACCESS_TOKEN }}
        dispatch-handler-client-payload-data: >
          {
            "event": "central-succeed"
          }

    # zoo failure
    - name: Notify Build Failure Zoo Handler Controller
      if: ${{ failure() }}
      uses: jvalkeal/build-zoo-handler@v0.0.4
      with:
        dispatch-handler-token: ${{ secrets.REPO_ACCESS_TOKEN }}
        dispatch-handler-client-payload-data: >
          {
            "event": "central-failed"
          }
