#!/usr/bin/env groovy

pipeline {
    agent {
        label 'slave-group-release'
    }

    parameters {
        string(name: 'version', defaultValue: '0.0.0.Qualifier', description: 'Release version')
        string(name: 'nextVersion', defaultValue: '', description: 'Next release (blank to stay on current SNAPSHOT)')
        gitParameter(name: 'branch', defaultValue: 'origin/master', branchFilter: 'origin/(.*)', type: 'PT_BRANCH', description: 'Branch to release from')
    }

    options {
        timeout(time: 3, unit: 'HOURS')
        timestamps()
        buildDiscarder(logRotator(numToKeepStr: '100', daysToKeepStr: '61'))
    }

    stages {
        stage('Prepare') {
            steps {
                script {
                    env.MAVEN_HOME = tool('Maven')
                    env.MAVEN_OPTS = "-Xmx1g -XX:+HeapDumpOnOutOfMemoryError"
                    env.JAVA_HOME = tool('JDK 11')
                }

                sh returnStdout: true, script: 'cleanup.sh'
            }
        }

        stage('Checkout') {
            steps {
                checkout scm
            }
        }

        stage('Commit version for documentation') {
            steps {
                script {
                    sh "sed -i \"s/^:sb_starter:.*\$/:sb_starter: ${version}/\" documentation/asciidoc/topics/attributes/community-attributes.adoc"
                    sh "git add 'documentation/asciidoc/topics/attributes/community-attributes.adoc'"
                    sh "git diff-index --quiet HEAD || git commit -m 'Committing ${version} to doc attribute file'"
                }
            }
        }

        stage('Version') {
            steps {
                sh "$MAVEN_HOME/bin/mvn -B -V versions:set -DnewVersion=${version} -DprocessAllModules=true"
                sh "$MAVEN_HOME/bin/mvn -B -V versions:set-property -Dproperty=version.infinispan -DnewVersion=${version}"
            }
        }

        stage('Create Staging repository') {
            steps {
                // Create a staging repository on Nexus
                script {
                    STAGING_REPO_ID = sh(returnStdout: true, script: "$MAVEN_HOME/bin/mvn -B -V org.sonatype.plugins:nexus-staging-maven-plugin:rc-open -pl .|grep 'Opened '|grep -oE '[^ ]+\$'").trim()
                }
                echo "Staging repository: $STAGING_REPO_ID"
            }
        }

        stage('Deploy') {
            steps {
                sh "$MAVEN_HOME/bin/mvn -B -V -Pdistribution -DskipTests clean deploy -DstagingRepositoryId=$STAGING_REPO_ID"
            }
        }

        stage('Close Staging repository') {
            steps {
                // This ensures that Nexus validates the repository for correctness
                sh "$MAVEN_HOME/bin/mvn -B -V org.sonatype.plugins:nexus-staging-maven-plugin:rc-close -pl . -DstagingRepositoryId=$STAGING_REPO_ID"
            }
        }

        stage('Tag') {
            steps {
                // Commit and tag once everything is good
                sh "$MAVEN_HOME/bin/mvn -B -V scm:checkin -Dmessage=\"Releasing version ${version}\" -DpushChanges=false"
                sh "$MAVEN_HOME/bin/mvn -B -V scm:tag -Dtag=${version}"
            }
        }

        stage('Next version') {
            when {
                expression { params.nextVersion != '' }
            }
            steps {
                sh "$MAVEN_HOME/bin/mvn -B -V versions:set -DnewVersion=${nextVersion} -DprocessAllModules=true"
                sh "$MAVEN_HOME/bin/mvn -B -V versions:set-property -Dproperty=version.infinispan -DnewVersion=${nextVersion}"
                sh "$MAVEN_HOME/bin/mvn -B -V -Dmessage='next version ${nextVersion}' -DscmVersion=${branch} -DscmVersionType=branch scm:checkin"
            }
        }
    }

    post {
        always {
            // Clean
            sh 'git clean -fdx -e "*.hprof" || echo "git clean failed, exit code $?"'
        }
        failure {
            echo "post build status: failure"
            emailext to: '${DEFAULT_RECIPIENTS}', subject: '${DEFAULT_SUBJECT}', body: '${DEFAULT_CONTENT}'
        }

        success {
            echo "post build status: success"
            emailext to: '${DEFAULT_RECIPIENTS}', subject: '${DEFAULT_SUBJECT}', body: '${DEFAULT_CONTENT}'
        }
    }
}
