blob: 869aa0938990c894229d83d37c912b1bc88837a2 [file] [log] [blame]
import org.jenkinsci.plugins.workflow.libs.Library
@Library('jenkins-pipeline-shared-libraries')_
import org.kie.jenkins.MavenCommand
import org.kie.jenkins.MavenStagingHelper
deployProperties = [:]
pipeline {
agent {
label 'kie-rhel7 && kie-mem16g'
}
tools {
maven 'kie-maven-3.6.2'
jdk 'kie-jdk11'
}
options {
timestamps()
timeout(time: 360, unit: 'MINUTES')
}
// parameters {
// For parameters, check into .jenkins/dsl/jobs.groovy file
// }
environment {
// Static env is defined into .jenkins/dsl/jobs.groovy file
// Keep here for visitibility
MAVEN_OPTS = '-Xms1024m -Xmx4g'
BOT_BRANCH_HASH = "${util.generateHash(10)}"
MAVEN_DEPLOY_LOCAL_DIR="${WORKSPACE}/maven_deploy_dir"
}
stages {
stage('Initialize') {
steps {
script {
cleanWs()
if (params.DISPLAY_NAME) {
currentBuild.displayName = params.DISPLAY_NAME
}
if (isRelease() || isCreatePr()) {
// Verify version is set
assert getProjectVersion()
assert getOptaPlannerVersion()
if(isRelease()) {
// Verify if on right release branch
assert getBuildBranch() == util.getReleaseBranchFromVersion(getProjectVersion())
}
}
checkoutRepo()
}
}
post {
success {
script {
setDeployPropertyIfNeeded('git.branch', getBuildBranch())
setDeployPropertyIfNeeded('git.author', getGitAuthor())
setDeployPropertyIfNeeded('project.version', getProjectVersion())
setDeployPropertyIfNeeded('release', isRelease())
setDeployPropertyIfNeeded('optaplanner.version', getOptaPlannerVersion())
}
}
}
}
stage('Prepare for PR'){
when {
expression { return isRelease() || isCreatePr() }
}
steps {
prepareForPR()
}
}
stage('Update project version'){
when {
expression { return getProjectVersion() != '' }
}
steps {
script {
maven.mvnVersionsUpdateParentAndChildModules(getMavenCommand(), getProjectVersion(), !isRelease())
// Update Optaplanner version
maven.mvnSetVersionProperty(getMavenCommand(), 'version.org.optaplanner', getOptaPlannerVersion())
}
}
}
stage('Build kogito-examples') {
steps {
script {
getMavenCommand().skipTests(params.SKIP_TESTS).run('clean install')
}
}
post {
always {
saveReports(params.SKIP_TESTS)
}
}
}
stage('Check kogito-examples with persistence') {
steps {
script {
sh "cp -r ${getRepoName()} ${getRepoName()}-persistence"
getMavenCommand("${getRepoName()}-persistence")
.skipTests(params.SKIP_TESTS)
.withProfiles(['persistence'])
.run('clean install')
}
}
post {
always {
saveReports(params.SKIP_TESTS)
}
}
}
stage('Check kogito-examples with events') {
steps {
script {
sh "cp -r ${getRepoName()} ${getRepoName()}-events"
getMavenCommand("${getRepoName()}-events")
.skipTests(params.SKIP_TESTS)
.withProfiles(['events'])
.run('clean install')
}
}
post {
always {
saveReports(params.SKIP_TESTS)
}
}
}
stage('Deploy artifacts') {
steps {
script {
if (env.MAVEN_DEPLOY_REPOSITORY && env.MAVEN_REPO_CREDS_ID) {
// Deploy to specific repository with credentials
runMavenDeployLocally()
maven.uploadLocalArtifacts(env.MAVEN_REPO_CREDS_ID, getLocalDeploymentFolder(), getMavenRepoZipUrl())
} else if(!isRelease() || env.MAVEN_DEPLOY_REPOSITORY){
// Normal deploy
runMavenDeploy()
} else {
// Deploy locally and then to staging
runMavenDeployLocally()
runMavenStage()
}
}
}
}
stage('Create PR'){
when {
expression { return isRelease() || isCreatePr() }
}
steps {
commitAndCreatePR()
}
post {
success {
script {
setDeployPropertyIfNeeded("${getRepoName()}.pr.source.uri", "https://github.com/${getBotAuthor()}/${getRepoName()}")
setDeployPropertyIfNeeded("${getRepoName()}.pr.source.ref", getBotBranch())
setDeployPropertyIfNeeded("${getRepoName()}.pr.target.uri", "https://github.com/${getGitAuthor()}/${getRepoName()}")
setDeployPropertyIfNeeded("${getRepoName()}.pr.target.ref", getBuildBranch())
}
}
}
}
stage('Update nightly branch'){
when {
expression { return isUpdateNightlyBranch() && !isCreatePr() && !isRelease() }
}
steps {
script {
String nightlyBranch = "nightly-${getBuildBranch()}"
dir(getRepoName()) {
githubscm.createBranch(nightlyBranch)
githubscm.pushObject('origin', nightlyBranch, getGitAuthorCredsID())
}
}
}
}
}
post {
always {
script {
def propertiesStr = deployProperties.collect{ entry -> "${entry.key}=${entry.value}" }.join("\n")
writeFile(text: propertiesStr, file: env.PROPERTIES_FILE_NAME)
archiveArtifacts(artifacts: env.PROPERTIES_FILE_NAME)
}
}
cleanup {
script {
util.cleanNode('docker')
}
}
}
}
void saveReports(boolean allowEmpty=false){
junit testResults: '**/target/surefire-reports/**/*.xml, **/target/failsafe-reports/**/*.xml', allowEmptyResults: allowEmpty
}
void checkoutRepo() {
dir(getRepoName()) {
deleteDir()
checkout(githubscm.resolveRepository(getRepoName(), getGitAuthor(), getBuildBranch(), false))
}
}
void prepareForPR() {
dir(getRepoName()) {
githubscm.forkRepo(getBotAuthorCredsID())
githubscm.createBranch(getBotBranch())
}
}
void addNotIgnoredPoms() {
// based on https://stackoverflow.com/a/59888964/8811872
sh '''
find . -type f -name 'pom.xml' > found_poms.txt
poms_to_add=""
while IFS= read -r pom; do
if ! git check-ignore -q "\$pom"; then
poms_to_add="\$poms_to_add \$pom"
fi
done < found_poms.txt
rm found_poms.txt
git add \$poms_to_add
git status
'''
}
void commitAndCreatePR() {
def commitMsg = "[${getBuildBranch()}] Update version to ${getProjectVersion()}"
def prBody = "Generated by build ${BUILD_TAG}: ${BUILD_URL}."
if (isRelease()) {
prBody += '\nPlease do not merge, it should be merged automatically after testing.'
} else {
prBody += '\nPlease review and merge.'
}
// Not using githubscm.commitChanges() because globbing won't work.
// See: https://github.com/kiegroup/kogito-runtimes/pull/570#discussion_r449268738
dir(getRepoName()) {
addNotIgnoredPoms()
sh "git commit -m '${commitMsg}'"
githubscm.pushObject('origin', getBotBranch(), getBotAuthorCredsID())
deployProperties["${getRepoName()}.pr.link"] = githubscm.createPR(commitMsg, prBody, getBuildBranch(), getBotAuthorCredsID())
}
}
boolean isRelease() {
return env.RELEASE.toBoolean()
}
boolean isCreatePr() {
return params.CREATE_PR
}
boolean isUpdateNightlyBranch() {
return params.UPDATE_NIGHTLY_BRANCH
}
String getRepoName(){
return env.REPO_NAME
}
String getGitAuthor() {
// GIT_AUTHOR can be env or param
return "${GIT_AUTHOR}"
}
String getBuildBranch(){
return params.BUILD_BRANCH_NAME
}
String getProjectVersion(){
return params.PROJECT_VERSION
}
String getOptaPlannerVersion(){
return params.OPTAPLANNER_VERSION
}
String getBotBranch(){
return "${getProjectVersion()}-${env.BOT_BRANCH_HASH}"
}
String getBotAuthor(){
return env.GIT_AUTHOR_BOT
}
String getGitAuthorCredsID(){
return env.AUTHOR_CREDS_ID
}
String getBotAuthorCredsID(){
return env.BOT_CREDENTIALS_ID
}
void setDeployPropertyIfNeeded(String key, def value){
if (value != null && value != ''){
deployProperties[key] = value
}
}
MavenCommand getMavenCommand(String directory = ''){
directory = directory ?: getRepoName()
MavenCommand mvnCmd = new MavenCommand(this, ['-fae'])
.withSettingsXmlId(env.MAVEN_SETTINGS_CONFIG_FILE_ID)
.inDirectory(directory)
.withProperty('full')
if (env.MAVEN_DEPENDENCIES_REPOSITORY) {
mvnCmd.withDependencyRepositoryInSettings('deps-repo', env.MAVEN_DEPENDENCIES_REPOSITORY)
}
return mvnCmd
}
void runMavenDeploy(){
mvnCmd = getMavenCommand()
if(env.MAVEN_DEPLOY_REPOSITORY){
mvnCmd.withDeployRepository(env.MAVEN_DEPLOY_REPOSITORY)
}
mvnCmd.skipTests(true).run('clean deploy')
}
void runMavenDeployLocally() {
getMavenCommand()
.skipTests(true)
.withLocalDeployFolder(getLocalDeploymentFolder())
.run('clean deploy')
}
void runMavenStage() {
MavenStagingHelper stagingHelper = getStagingHelper()
deployProperties.putAll(stagingHelper.stageLocalArtifacts(env.NEXUS_STAGING_PROFILE_ID, getLocalDeploymentFolder()))
stagingHelper.promoteStagingRepository(env.NEXUS_BUILD_PROMOTION_PROFILE_ID)
}
MavenStagingHelper getStagingHelper() {
return new MavenStagingHelper(this, getMavenCommand())
.withNexusReleaseUrl(env.NEXUS_RELEASE_URL)
.withNexusReleaseRepositoryId(env.NEXUS_RELEASE_REPOSITORY_ID)
}
String getLocalDeploymentFolder(){
return "${env.MAVEN_DEPLOY_LOCAL_DIR}/${getRepoName()}"
}
String getMavenRepoZipUrl() {
return "${env.MAVEN_DEPLOY_REPOSITORY.replaceAll('/content/', '/service/local/').replaceFirst('/*$', '')}/content-compressed"
}