/**
 * Pipeline to construct a build version of the Silverpeas Project POM that is used as the POM
 * parent of all the other Silverpeas projects. The Silverpeas Project POM is made up of three
 * projects: Silverpeas-Project that defines the model of a project in Silverpeas,
 * silverpeas-dependencies-bom that defines the dependencies required by the Silverpeas application,
 * and silverpeas-test-dependencies-bom that defines the dependencies required to run the tests
 * for a given Silverpeas project or module.
 *
 * The build is performed within a dedicated Docker image in order to ensure the reproducibility of
 * the builds and to containerize them from the host OS.
 */
pipeline {

  agent {
    docker {
      image 'silverpeas/silverbuild'
      args '''
          -v $HOME/.m2/settings.xml:/home/silverbuild/.m2/settings.xml 
          -v $HOME/.m2/settings-security.xml:/home/silverbuild/.m2/settings-security.xml 
          -v $HOME/.gitconfig:/home/silverbuild/.gitconfig 
          -v $HOME/.ssh:/home/silverbuild/.ssh 
          -v $HOME/.gnupg:/home/silverbuild/.gnupg
          '''
    }
  }

  environment {
    nexusRepo = 'https://nexus3.silverpeas.org/repository/builds'
    gitRepoOfProject = 'https://github.com/Silverpeas/Silverpeas-Project'
    gitRepoOfDependencies = 'https://github.com/Silverpeas/silverpeas-dependencies-bom'
    gitRepoOfTestDependencies = 'https://github.com/Silverpeas/silverpeas-test-dependencies-bom'
    gitCredential = 'cacc0467-7c85-41d1-bf4e-eaa470dd5e59'
    buildNumber = (new Date()).format('yyMMdd')
    buildVersion = ''
    release = ''
    artifact = 'target/build.yaml'
  }

  stages {
    stage('Prepare the build') {
      steps {
        echo "Prepare the build ${buildNumber}..."
        sh "curl -fsSL -o pom1.xml https://raw.githubusercontent.com/Silverpeas/Silverpeas-Project/master/pom.xml"
        script {
          def pom = readMavenPom file: 'pom1.xml'
          release = pom.properties['next.release']
          buildVersion = "${release}-build${buildNumber}"
          wildfly = pom.properties['wildfly.version']
        }
        sh "curl -fsSL -o pom2.xml https://raw.githubusercontent.com/Silverpeas/silverpeas-dependencies-bom/master/pom.xml"
        script {
          def pom = readMavenPom file: 'pom2.xml'
        }
        sh 'rm -f pom?.xml'
      }
    }
    stage('Build and Publish dependencies definition') {
      steps {
        dir('dependencies') {
          git([url: gitRepoOfDependencies, credentialsId: gitCredential])
          buildAndPublish(buildVersion, nexusRepo)
        }
      }
    }
    stage('Build and Publish test dependencies definition') {
      steps {
        dir('test-dependencies') {
          git([url: gitRepoOfTestDependencies, credentialsId: gitCredential])
          buildAndPublish(buildVersion, nexusRepo)
        }
      }
    }
    stage('Build and publish project definition') {
      steps {
        dir('project') {
          git([url: gitRepoOfProject, credentialsId: gitCredential])
          sh """
sed -i -e "s/<silverpeas.bom.version>[0-9a-zA-Z.-]\\+/<silverpeas.bom.version>${buildVersion}/g" pom.xml
"""
          buildAndPublish(buildVersion, nexusRepo)
        }
      }
    }
    stage('Create YAML build report') {
      steps {
        writeYaml file: artifact, data: ['version': buildVersion,
                                         'release': release,
                                         'wildfly': wildfly]
      }
    }
  }
  post {
    success {
      script {
        currentBuild.displayName = buildVersion
      }
      archiveArtifacts artifacts: artifact, fingerprint: true
    }
    always {
      step([$class                  : 'Mailer',
            notifyEveryUnstableBuild: true,
            recipients              : "miguel.moquillon@silverpeas.org, silveryocha@chastagnier.com",
            sendToIndividuals       : true])
    }
  }
}

void buildAndPublish(version, repo) {
  sh """
mvn -U versions:set -DgenerateBackupPoms=false -DnewVersion=${version}
mvn clean deploy -DaltDeploymentRepository=silverpeas::default::${repo} -Djava.awt.headless=true
"""
}
