Skip to content

Commit

Permalink
jobs: Add a bump-jenkins job
Browse files Browse the repository at this point in the history
bump-jenkins job added to periodically update the jenkins plugins to latest version

Co-authored-by: Michael Armijo <[email protected]>
Ref: coreos#562
  • Loading branch information
aaradhak and marmijo committed Nov 2, 2023
1 parent aa70ee8 commit 26e9b33
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 0 deletions.
127 changes: 127 additions & 0 deletions jobs/bump-jenkins-plugins.Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import groovy.json.*
node {
checkout scm
// these are script global vars
pipeutils = load("utils.groovy")
pipecfg = pipeutils.load_pipecfg()
}

properties([
pipelineTriggers([
// check once a month
pollSCM('H H 1 * *')
]),
buildDiscarder(logRotator(
numToKeepStr: '100',
artifactNumToKeepStr: '100'
)),
durabilityHint('PERFORMANCE_OPTIMIZED')
])

repo = "coreosbot-releng/fedora-coreos-pipeline"
botCreds = "github-coreosbot-releng-token-username-password"
pr_branch = "pluginsupdate"

def getPluginLatestVersion(pluginsUpdate) {
// Extract the plugin version from the URL
def versionPattern = /\/([^\/]+)\/([^\/]+)\/([^\/]+)\.hpi/
def matcher = (pluginsUpdate =~ versionPattern)
def pluginVersion

if (matcher.find()) {
def groupId = matcher.group(1)
pluginVersion = matcher.group(2)
} else {
println "Unable to extract plugin version from the URL."
}
return pluginVersion
}

lock(resource: "bump-jenkins") {
node{
try {
shwrap("""
git config --global user.name "CoreOS Bot Releng"
git config --global user.email "[email protected]"
""")

def pluginslist
def pluginsToUpdate = [:]
def haveChanges=false

stage("Read plugins.txt") {
shwrapCapture("""
if [[ -d fedora-coreos-pipeline ]]; then
rm -rf fedora-coreos-pipeline
fi
git clone --branch pluginsupdate https://github.com/coreosbot-releng/fedora-coreos-pipeline.git
cd fedora-coreos-pipeline

# Check if the branch exists
if git ls-remote --heads origin ${pr_branch} | grep ${pr_branch}; then
git checkout ${pr_branch}
else
git checkout -b ${pr_branch}
fi
""")
def plugins_lockfile = "jenkins/controller/plugins.txt"
pluginslist = shwrapCapture("cat $plugins_lockfile | grep -v ^#").split('\n')
}

stage("Check for plugin updates") {
def pluginsUpdate
pluginslist.each { plugin ->
def parts = plugin.split(':')
if (parts.size() == 2) {
def pluginName = parts[0]
def currentVersion = parts[1]
pluginsUpdate = shwrapCapture("curl -Ls -o /dev/null -w '%{url_effective}' https://updates.jenkins.io/download/plugins/${pluginName}/latest/${pluginName}.hpi")
def latestVersion = getPluginLatestVersion(pluginsUpdate)
if (latestVersion.toString() != currentVersion.toString()) {
haveChanges = true
pluginsToUpdate["${pluginName}"] = [currentVersion, latestVersion]
println("Plugin: ${pluginName} current version is ${currentVersion}, it will be updated to latest version: ${latestVersion}")
shwrap("""
cd fedora-coreos-pipeline
sed -i 's/${pluginName}:${currentVersion}/${pluginName}:${latestVersion}/g' jenkins/controller/plugins.txt
""")
}
else {
println("The latest version of ${pluginName} is already installed: ${currentVersion}")
}
}
else {
println("ERROR: unexpected")
}
}
}

stage("Push") {
if (haveChanges){
def message = "bump jenkins plugin version"
shwrap("""
cd fedora-coreos-pipeline
git add jenkins/controller/plugins.txt
git commit -m '${message}' -m 'Job URL: ${env.BUILD_URL}' -m 'Job definition: https://github.com/coreos/fedora-coreos-pipeline/blob/main/jobs/bump-jenkins-plugins.Jenkinsfile'
""")
withCredentials([usernamePassword(credentialsId: botCreds,
usernameVariable: 'GHUSER',
passwordVariable: 'GHTOKEN')]) {
shwrap("""
cd fedora-coreos-pipeline
git push -f https://\${GHUSER}:\${GHTOKEN}@github.com/${repo} ${pr_branch}
curl -H "Authorization: token ${GHTOKEN}" -X POST -d '{ "title": "Bump jenkins plugin version to the latest", "head": "${pr_branch}", "base": "main" }' https://api.github.com/repos/coreosbot-releng/fedora-coreos-pipeline/pulls
""")
}
}
}
} catch (e) {
currentBuild.result = 'FAILURE'
throw e
} finally {
if (currentBuild.result != 'SUCCESS') {
pipeutils.trySlackSend(message: "bump-lockfile #${env.BUILD_NUMBER} <${env.BUILD_URL}|:jenkins:> <${env.RUN_DISPLAY_URL}|:ocean:> [${params.STREAM}]")
}
}
}
}
28 changes: 28 additions & 0 deletions jobs/test-jenkins.Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import groovy.json.*
node {
checkout scm
// these are script global vars
pipeutils = load("utils.groovy")
pipecfg = pipeutils.load_pipecfg()
}

properties([
pipelineTriggers([]),
buildDiscarder(logRotator(
numToKeepStr: '100',
artifactNumToKeepStr: '100'
)),
durabilityHint('PERFORMANCE_OPTIMIZED')
])

node{
stage ("Test Jenkins Plugins"){
shwrap("""
oc login --token=sha256~kBPM9YPSrRT2-J_KkRmEJL31PRw7qYLAw_WUs2HFepA --server=https://api.ocp-virt.prod.psi.redhat.com:6443
oc project rhcos-devel
oc patch buildconfig jenkins -n rhcos-devel --type='json' -p='[{"op": "replace", "path": "/spec/source/git/uri", "value": "https://github.com/aaradhak/fedora-coreos-pipeline"}]'
oc patch buildconfig jenkins -n rhcos-devel --type='json' -p='[{"op": "replace", "path": "/spec/strategy/sourceStrategy/from/name", "value": "jenkins:newversion"}]'
oc patch buildconfig jenkins-s2i -n rhcos-devel --type='json' -p='[{"op": "replace", "path": "/spec/strategy/sourceStrategy/from/name", "value": "jenkins:newversion"}]'
""")
}
}

0 comments on commit 26e9b33

Please sign in to comment.