#!groovy
/**
 * Jenkins pipeline to build Corda Opensource Pull Requests with JDK11.
 */

@Library('corda-shared-build-pipeline-steps')
import static com.r3.build.BuildControl.killAllExistingBuildsForJob

killAllExistingBuildsForJob(env.JOB_NAME, env.BUILD_NUMBER.toInteger())

pipeline {
    agent {
        dockerfile {
            label 'standard'
            additionalBuildArgs '--build-arg USER="${USER}"' // DON'T change quotation - USER variable is substituted by SHELL!!!!
            filename '.ci/dev/compatibility/DockerfileJDK11'
        }
    }
    options {
        timestamps()
        timeout(time: 3, unit: 'HOURS')
        buildDiscarder(logRotator(daysToKeepStr: '14', artifactDaysToKeepStr: '14'))
    }

    environment {
        ARTIFACTORY_CREDENTIALS = credentials('artifactory-credentials')
        BUILD_CACHE_CREDENTIALS = credentials('gradle-ent-cache-credentials')
        BUILD_CACHE_PASSWORD = "${env.BUILD_CACHE_CREDENTIALS_PSW}"
        BUILD_CACHE_USERNAME = "${env.BUILD_CACHE_CREDENTIALS_USR}"
        CORDA_ARTIFACTORY_PASSWORD = "${env.ARTIFACTORY_CREDENTIALS_PSW}"
        CORDA_ARTIFACTORY_USERNAME = "${env.ARTIFACTORY_CREDENTIALS_USR}"
        CORDA_GRADLE_SCAN_KEY = credentials('gradle-build-scans-key')
        CORDA_USE_CACHE = "corda-remotes"
    }

    stages {
        stage('JDK 11 Compile') {
            steps {
                authenticateGradleWrapper()
                sh "./gradlew --no-daemon --parallel --build-cache -Pcompilation.allWarningsAsErrors=true -Ptests.failFast=false " +
                "-Ptests.ignoreFailures=true clean compileAll --stacktrace"
            }
        }
        stage('Deploy nodes') {
            steps {
                sh "./gradlew --no-daemon --build-cache deployNodes"
            }
        }
    }
    post {
        always {
            findBuildScans()
        }
        cleanup {
            deleteDir() /* clean up our workspace */
        }
    }
}
