#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

name: Publish Release Artifacts
run-name: ${{ github.actor }} is publishing release artifacts

on:
  workflow_dispatch:
    inputs:
      release:
        type: choice
        description: Select the ManifoldCF release
        options:
        - release-2.27
        - release-2.26 #Java 11
        - release-2.25
        - release-2.24
        - release-2.23
        - release-2.22
        - release-2.21
        - release-2.20
        - release-2.19
        - release-2.18
        - release-2.17
        - release-2.16
        - release-2.15
        - release-2.14
        - release-2.13
        - release-2.12
        - release-2.11
        - release-2.10
        - release-2.9
        - release-2.8
        - release-2.7 #Java 1.8
        - release-2.6
        - release-2.5
        - release-2.4
        - release-2.3
        - release-2.2
        - release-2.1
        - release-2.0 #Java 1.7
        - release-1.9
        - release-1.8
        - release-1.2 #Java 1.6
        - release-1.1 
        - release-1.0 #Java 1.5

jobs:
  deployReleaseMavenArtifacts:
    if: github.repository == 'apache/manifoldcf'
    name: "Deploy Apache ManifoldCF release artifacts"
    runs-on: ubuntu-24.04
    steps:
      - name: Checkout
        uses: actions/checkout@v6.0.2
        with:
          ref: ${{ github.event.inputs.release }}
      
      - name: Set up Java
        uses: actions/setup-java@v5.2.0
        with:
          java-version: '21'
          distribution: 'temurin'
          architecture: x64
          cache: maven
          server-id: apache.releases.https
          server-username: NEXUS_USER
          server-password: NEXUS_PW
          settings-path: ${{ github.workspace }}/.github/asf-deploy-settings.xml
          gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }}
          gpg-passphrase: GPG_PASSPHRASE # Environment variable name for the GPG private key passphrase. Default is $GPG_PASSPHRASE
          
      - name: Ant Build
        env:
         RELEASE: ${{ inputs.release }}
        run: |
          if [[ $RELEASE == 'release-2.26' ]]; then
            sed -i -e 's/<?xml version=\"1\.0\" encoding=\"UTF-8\"?>/ /g' connectors/csv/pom.xml
            sed -i -e 's/https\:\/\/maven.nuxeo.org\/nexus\/content\/repositories\/public-releases/https\:\/\/packages.nuxeo.com\/repository\/maven-public-archives/g' build.xml
            sed -i -e 's/https\:\/\/maven.nuxeo.org\/nexus\/content\/repositories\/public-releases\//https\:\/\/packages.nuxeo.com\/repository\/maven-public-archives/g' connectors/nuxeo/pom.xml
            sed -i -e 's/<\/dependencies>/<dependency><groupId>org.apache.hadoop<\/groupId><artifactId>hadoop-annotations<\/artifactId><version>\${hadoop.version}<\/version><exclusions><exclusion><groupId>jdk.tools<\/groupId><artifactId>jdk.tools<\/artifactId><\/exclusion><\/exclusions><\/dependency><\/dependencies>/g' connectors/hdfs/pom.xml
          fi
          ant make-core-deps make-deps image
      
      - name: Maven Build
        run: mvn clean install -B -DskipTests -DskipITs -Papache-release
      
      - name: Deploy
        env:
          NEXUS_USER: ${{ secrets.NEXUS_STAGE_DEPLOYER_USER }}
          NEXUS_PW: ${{ secrets.NEXUS_STAGE_DEPLOYER_PW }}
          MAVEN_SETTINGS: ${{ github.workspace }}/.github/asf-deploy-settings.xml
          MAVEN_USERNAME: ${{ secrets.NEXUS_STAGE_DEPLOYER_USER }}
          MAVEN_CENTRAL_TOKEN: ${{ secrets.NEXUS_STAGE_DEPLOYER_PW }}
          MAVEN_GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
          GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
        run: mvn -B deploy -DskipTests -DskipITs -Dmaven.javadoc.skip=true -Papache-release --threads=1
            
      - name: Deploy local staged artifacts
        if: ${{ github.event_name != 'pull_request' }}
        env:
          NEXUS_USER: ${{ secrets.NEXUS_STAGE_DEPLOYER_USER }}
          NEXUS_PW: ${{ secrets.NEXUS_STAGE_DEPLOYER_PW }}
          MAVEN_SETTINGS: ${{ github.workspace }}/.github/asf-deploy-settings.xml
          MAVEN_USERNAME: ${{ secrets.NEXUS_STAGE_DEPLOYER_USER }}
          MAVEN_CENTRAL_TOKEN: ${{ secrets.NEXUS_STAGE_DEPLOYER_PW }}
          MAVEN_GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
          GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
        run: |
          ./mvn org.sonatype.plugins:nexus-staging-maven-plugin:deploy-staged \
            -DskipStagingRepositoryClose=true \
            -DserverId=apache.releases.https \
            -DnexusUrl=https://repository.apache.org \
            -Papache-release
