node {
    def version = VersionNumber(skipFailedBuilds: true, versionNumberString: '${BUILD_DATE_FORMATTED, "yyMMdd"}')+'-'+currentBuild.id
    currentBuild.displayName = "#" + version

    stage ("Build") {

        // Checkout code from repository
        checkout scm

        //get the GIT commit
        env.GIT_COMMIT = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
        env.GIT_BRANCH = sh(returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim()

        withCredentials([file(credentialsId: 'jfed-iminds-p12', variable: 'CERTIFICATE_FILE')]){
            //the certificate file needs to be fixed for install4j
            sh 'cp ${CERTIFICATE_FILE} iminds.p12'

            // clear maven cache for iMinds software to force redownload
            // without this, the latest library might not be used
            sh "rm -rf ${WORKSPACE}/.repository/be/iminds/ilabt/"

            // Run the maven build
            withMaven(jdk: 'Java-Oracle8', maven: 'maven 3', mavenOpts: '-DDISPLAY=:99',
            mavenSettingsConfig: 'jfed-settings', mavenLocalRepo: '.repository'){
                def extraGoals = ''

                if(env.BRANCH_NAME == 'develop' || env.BRANCH_NAME == 'master'){
                    extraGoals += "deploy"
                }

                sh "mvn clean package ${extraGoals} -P installers"


            }
        }

        //publish  testng results
        step([$class: 'Publisher', reportFilenamePattern: '*/target/surefire-reports/testng-results.xml'])

        step([$class: 'GitHubCommitStatusSetter',
            contextSource: [$class: 'ManuallyEnteredCommitContextSource',
                            context: 'continuous-integration/jenkins']])
    }

    stage ("Release") {
        //get the GIT commit
        gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
        // short SHA, possibly better for chat notifications, etc.
        shortCommit = gitCommit.take(6)

        sshagent(credentials: ['760ffb3d-a967-4e6a-a30d-3c680bd8626a']){

            sh "ssh jfedjenkins@10.10.3.70 mkdir /mnt/jfed-releases-staging/${version}"

            sh "rsync -avR */target/jfx/**  jfedjenkins@10.10.3.70:/mnt/jfed-releases-staging/${version}/"
            sh "rsync -avR */target/*.jar  jfedjenkins@10.10.3.70:/mnt/jfed-releases-staging/${version}/"
            sh "rsync -avR */target/lib/**  jfedjenkins@10.10.3.70:/mnt/jfed-releases-staging/${version}/"
            sh "rsync -avR distribution/target/media/**  jfedjenkins@10.10.3.70:/mnt/jfed-releases-staging/${version}/"

            sh "ssh jfedjenkins@10.10.3.70 /mnt/jfed-releases-staging/perform-release.sh ${version} ${gitCommit} ${env.BRANCH_NAME}"
        }
    }

    stage ("Integration tests") {

        // Run the maven build
        withMaven(jdk: 'Java-Oracle8', maven: 'maven 3', mavenOpts: '-DDISPLAY=:99',
        mavenSettingsConfig: 'jfed-settings', mavenLocalRepo: '.repository'){
            sh "mvn verify -Djarsigner.skip=true  -Dmaven.test.failure.ignore=true -DskipUTs -Pintegration"
        }

        step([$class: 'Publisher', reportFilenamePattern: '*/target/failsafe-reports/testng-results.xml',unstableFails: 20])
    }
}