blob: 9c7e202563e67972977a0840cacdf46fc061d72f [file] [log] [blame]
import org.jenkinsci.plugins.workflow.libs.Library
@Library('jenkins-pipeline-shared-libraries')_
// This Jenkinsfile has not been tested on Apache Jenkins yet
// and will likely fail due to restrictions on Groovy sandbox methods
// See `findPlugin` and `getJenkinsVersion` methods
branchCreated = false
pipeline {
agent {
docker {
image env.AGENT_DOCKER_BUILDER_IMAGE
args env.AGENT_DOCKER_BUILDER_ARGS
label util.avoidFaultyNodes('ubuntu')
}
}
options {
timeout(time: 60, unit: 'MINUTES')
}
environment {
KOGITO_CI_EMAIL_TO = credentials("${JENKINS_EMAIL_CREDS_ID}")
}
stages {
stage('Checkout repo') {
steps {
script {
dir(getRepoName()) {
deleteDir()
// Checkout repository
checkout(githubscm.resolveRepository(getRepoName(), getGitAuthor(), getBuildBranch(), false, getGitAuthorCredsId()))
githubscm.setUserConfig(getGitAuthorCredsId())
branchCreated = getOrCreateGitBranch(getPRBranch(), getGitAuthorPushCredsId())
}
}
}
}
stage('Update DSL Gradle dependencies') {
steps {
script {
dir(getRepoName()) {
String lookupCmd = '''
cat dsl/seed/gradle.properties | grep 'version_jenkins_plugins_' | awk -F'version_jenkins_plugins_' '{print $2}' | awk -F'=' '{print $1}' | tr '\n' ','
'''
def plugins = sh(returnStdout: true, script: lookupCmd).trim().split(',')
echo "Got plugins ${plugins}"
def managedPluginsVersion = [:]
plugins.each {
String pluginShortName = it.replaceAll('_', '-')
String pluginVarName = "version_jenkins_plugins_${it}"
def plugin = findPlugin(pluginShortName)
if (plugin) {
managedPluginsVersion[pluginVarName] = plugin.getVersion()
} else {
unstable("Could not find plugin ${pluginShortName}. Please make sure it is installed")
}
}
echo "Got managed plugins version: ${managedPluginsVersion}"
managedPluginsVersion.each { pluginVarName, pluginVersion ->
echo "Updating plugin ${pluginVarName} with version ${pluginVersion}"
sh """
sed -i 's|${pluginVarName}=.*|${pluginVarName}=${pluginVersion}|g' dsl/seed/gradle.properties
"""
}
String jenkinsVersion = getJenkinsVersion()
echo "Updating Jenkins version to version ${jenkinsVersion}"
sh """
sed -i 's|version_jenkins=.*|version_jenkins=${jenkinsVersion}|g' dsl/seed/gradle.properties
"""
}
}
}
}
stage('Update Maven tests dependencies') {
steps {
script {
dir(getRepoName()) {
String lookupCmd = '''
cat .ci/jenkins/tests/pom.xml | grep '<version.jenkins.plugins.' | awk -F'>' '{print $1}' | awk -F'<version.jenkins.plugins.' '{print $2}' | tr '\n' ','
'''
def plugins = sh(returnStdout: true, script: lookupCmd).trim().split(',')
echo "Got plugins ${plugins}"
def managedPluginsVersion = [:]
plugins.each {
String pluginShortName = it
String pluginVarName = "version.jenkins.plugins.${it}"
def plugin = findPlugin(pluginShortName)
if (plugin) {
managedPluginsVersion[pluginVarName] = plugin.getVersion()
} else {
unstable("Could not find plugin ${pluginShortName}. Please make sure it is installed")
}
}
echo "Got managed plugins version: ${managedPluginsVersion}"
managedPluginsVersion.each { pluginVarName, pluginVersion ->
echo "Updating plugin ${pluginVarName} with version ${pluginVersion}"
sh """
sed -i 's|<${pluginVarName}>.*</${pluginVarName}>|<${pluginVarName}>${pluginVersion}</${pluginVarName}>|g' .ci/jenkins/tests/pom.xml
"""
}
String jenkinsVersion = getJenkinsVersion()
echo "Updating Jenkins version to version ${jenkinsVersion}"
sh """
sed -i 's|<version.jenkins>.*</version.jenkins>|<version.jenkins>${jenkinsVersion}</version.jenkins>|g' .ci/jenkins/tests/pom.xml
"""
}
}
}
}
stage('Create PR') {
steps {
script {
dir(getRepoName()) {
if (githubscm.isThereAnyChanges()) {
def commitMsg = "[${getBuildBranch()}] Update Jenkins dependencies to DSL/Jenkins files"
githubscm.commitChanges(commitMsg, {
githubscm.findAndStageNotIgnoredFiles('pom.xml')
githubscm.findAndStageNotIgnoredFiles('gradle.properties')
})
githubscm.pushObject('origin', getPRBranch(), getGitAuthorPushCredsId())
if (branchCreated) {
def prBody = "Generated by build ${BUILD_TAG}: ${BUILD_URL}.\nPlease review and merge."
prLink = githubscm.createPR(commitMsg, prBody, getBuildBranch(), getGitAuthorCredsId())
sendNotification("Please review PR ${prLink}")
} else {
echo "Branch ${getPRBranch()} was already created so assuming the PR exists alrerady ..."
}
} else {
println '[WARN] no changes to commit'
}
}
}
}
}
}
post {
unsuccessful {
script {
sendErrorNotification()
}
}
}
}
def findPlugin(String pluginShortName) {
return Jenkins.instance.pluginManager.plugins.find { it.getShortName() == pluginShortName }
}
String getJenkinsVersion() {
return Jenkins.getVersion()
}
void sendNotification(String body) {
emailext body: "**Jenkins dependencies update to DSL/Jenkins files**\n${body}",
subject: "[${getBuildBranch()}] Kogito pipelines",
to: env.KOGITO_CI_EMAIL_TO
}
void sendErrorNotification() {
String body = """
Job #${BUILD_NUMBER} was: **${currentBuild.currentResult}**
Please look here: ${BUILD_URL}
"""
sendNotification(body)
}
String getBuildBranch() {
return env.BUILD_BRANCH_NAME
}
String getGitAuthor() {
return env.GIT_AUTHOR
}
String getGitAuthorCredsId() {
return env.GIT_AUTHOR_CREDS_ID
}
String getGitAuthorPushCredsId() {
return env.GIT_AUTHOR_PUSH_CREDS_ID
}
String getPRBranch() {
return "${getBuildBranch()}-${getJenkinsVersion()}"
}
String getRepoName() {
return env.REPO_NAME
}
boolean getOrCreateGitBranch(String branch, String credentialsId) {
sh 'git fetch origin'
String branchRemoteResult = sh(script: "git ls-remote origin ${branch} | wc -l", returnStdout: true).trim()
if (Integer.parseInt(branchRemoteResult) > 0) {
echo "Branch ${branch} already exist ... will not create it. Checking out !"
sh """
git checkout origin/${branch} -b ${branch}
git merge origin/${getBuildBranch()}
"""
return false
} else {
echo "Branch ${branch} does not exist ... gonna create it"
githubscm.createBranch(branch)
githubscm.pushObject('origin', branch, credentialsId)
return true
}
}