Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DROOLS-7533 Setup CI for Drools 8 #86

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
289 changes: 289 additions & 0 deletions .ci/jenkins/Jenkinsfile.deploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
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-rhel8 && docker && kie-mem16g && !built-in'
}

tools {
maven env.BUILD_MAVEN_TOOL
jdk env.BUILD_JDK_TOOL
}

options {
timestamps()
timeout(time: 180, unit: 'MINUTES')
}

environment {
DROOLS_CI_EMAIL_TO = credentials("${JENKINS_EMAIL_CREDS_ID}")
MAVEN_OPTS = '-Xms1024m -Xmx4g'
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()

if (isRelease()) {
// Verify if on right release branch
assert getBuildBranch() == util.getReleaseBranchFromVersion(getProjectVersion())
}
}

dir(getRepoName()) {
checkoutRepo()
}
}
}
post {
success {
script {
setDeployPropertyIfNeeded('git.branch', getBuildBranch())
setDeployPropertyIfNeeded('git.author', getGitAuthor())
setDeployPropertyIfNeeded('project.version', getProjectVersion())
setDeployPropertyIfNeeded('release', isRelease())
}
}
}
}
stage('Prepare for PR') {
when {
expression { return isRelease() || isCreatePr() }
}
steps {
script {
dir(getRepoName()) {
if (githubscm.isBranchExist('origin',getPRBranch())) {
githubscm.removeRemoteBranch('origin', getPRBranch())
}
githubscm.createBranch(getPRBranch())
}
}
}
}
stage('Update project version') {
when {
expression { return getProjectVersion() }
}
steps {
script {
maven.mvnVersionsSet(getMavenCommand(), getProjectVersion(), !isRelease())
}
}
}
stage('Build & Test') {
steps {
script {
getMavenCommand().withProperty('maven.test.failure.ignore', true).skipTests(params.SKIP_TESTS).run('clean install')
}
}
post {
always {
script {
saveReports()
util.archiveConsoleLog()
}
}
}
}
stage('Deploy locally') {
steps {
script {
runMavenDeploy(true)
}
}
}
stage('Stage artifacts') {
when {
expression { return shouldStageArtifacts() }
}
steps {
script {
// Stage release artifacts
runMavenStage()
}
}
}
stage('Create PR') {
when {
expression { return isRelease() || isCreatePr() }
}
steps {
script {
dir(getRepoName()) {
if (githubscm.isThereAnyChanges()) {
commitAndCreatePR()
} else {
println '[WARN] no changes to commit'
}
}
}
}
post {
success {
script {
setDeployPropertyIfNeeded("${getRepoName()}.pr.source.uri", "https://github.com/${getGitAuthor()}/${getRepoName()}")
setDeployPropertyIfNeeded("${getRepoName()}.pr.source.ref", getPRBranch())
setDeployPropertyIfNeeded("${getRepoName()}.pr.target.uri", "https://github.com/${getGitAuthor()}/${getRepoName()}")
setDeployPropertyIfNeeded("${getRepoName()}.pr.target.ref", getBuildBranch())
}
}
}
}
}
post {
always {
script {
def propertiesStr = deployProperties.collect { entry -> "${entry.key}=${entry.value}" }.join('\n')
writeFile(text: propertiesStr, file: PROPERTIES_FILE_NAME)
archiveArtifacts(artifacts: PROPERTIES_FILE_NAME)
}
}
unsuccessful {
sendNotification()
}
cleanup {
script {
util.cleanNode('docker')
}
}
}
}

void saveReports() {
junit testResults: '**/target/surefire-reports/**/*.xml, **/target/failsafe-reports/**/*.xml, **/target/invoker-reports/**/TEST-*.xml', allowEmptyResults: true
}

void checkoutRepo() {
deleteDir()
checkout(githubscm.resolveRepository(getRepoName(), getGitAuthor(), getBuildBranch(), false))
// need to manually checkout branch since on a detached branch after checkout command
sh "git checkout ${getBuildBranch()}"
}

void commitAndCreatePR() {
def commitMsg = "[${getBuildBranch()}] Update version to ${getProjectVersion()}"
def prBody = "Generated by build ${BUILD_TAG}: ${BUILD_URL}.\nPlease do not merge, it should be merged automatically."

githubscm.commitChanges(commitMsg, {
githubscm.findAndStageNotIgnoredFiles('pom.xml')
githubscm.findAndStageNotIgnoredFiles('antora.yml')
})
githubscm.pushObject('origin', getPRBranch(), getGitAuthorCredsID())
deployProperties["${getRepoName()}.pr.link"] = githubscm.createPRWithLabels(commitMsg, prBody, getBuildBranch(), ['skip-ci'] as String[], getGitAuthorCredsID())
}

void sendNotification() {
if (params.SEND_NOTIFICATION) {
mailer.sendMarkdownTestSummaryNotification('Deploy', "[${getBuildBranch()}] KIE jpmml integration", [env.DROOLS_CI_EMAIL_TO])
} else {
echo 'No notification sent per configuration'
}
}

boolean shouldStageArtifacts() {
return isRelease()
}

boolean shouldDeployToRepository() {
return env.MAVEN_DEPLOY_REPOSITORY || getGitAuthor() == 'kiegroup'
}

boolean isRelease() {
return env.RELEASE ? env.RELEASE.toBoolean() : false
}

boolean isCreatePr() {
return params.CREATE_PR
}

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 getPRBranch() {
return params.DROOLS_PR_BRANCH
}

String getGitAuthorCredsID() {
return env.AUTHOR_CREDS_ID
}

void setDeployPropertyIfNeeded(String key, def value) {
if (value) {
deployProperties[key] = value
}
}

MavenCommand getMavenCommand(String directory = '') {
directory = directory ?: getRepoName()
def mvnCmd = new MavenCommand(this, ['-fae', '-ntp'])
.withSettingsXmlId(env.MAVEN_SETTINGS_CONFIG_FILE_ID)
.inDirectory(directory)
if (!isStream8()) { // Workaround as enforcer rules may not be fixed on other streams
mvnCmd.withProperty('enforcer.skip')
}
return mvnCmd
}

void runMavenDeploy(boolean localDeployment = false) {
mvnCmd = getMavenCommand()

if(localDeployment) {
mvnCmd.withLocalDeployFolder(getLocalDeploymentFolder())
} else if (env.MAVEN_DEPLOY_REPOSITORY) {
mvnCmd.withDeployRepository(env.MAVEN_DEPLOY_REPOSITORY)
}

mvnCmd.skipTests(true).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()}"
}

boolean isStream8() {
return env.DROOLS_STREAM == '8'
}
Loading
Loading