From 26e9b33c139c820aef57f694a9f681945d0d028b Mon Sep 17 00:00:00 2001 From: Aashish Radhakrishnan Date: Thu, 7 Sep 2023 10:15:37 -0400 Subject: [PATCH] jobs: Add a bump-jenkins job bump-jenkins job added to periodically update the jenkins plugins to latest version Co-authored-by: Michael Armijo Ref: https://github.com/coreos/fedora-coreos-pipeline/issues/562 --- jobs/bump-jenkins-plugins.Jenkinsfile | 127 ++++++++++++++++++++++++++ jobs/test-jenkins.Jenkinsfile | 28 ++++++ 2 files changed, 155 insertions(+) create mode 100644 jobs/bump-jenkins-plugins.Jenkinsfile create mode 100644 jobs/test-jenkins.Jenkinsfile diff --git a/jobs/bump-jenkins-plugins.Jenkinsfile b/jobs/bump-jenkins-plugins.Jenkinsfile new file mode 100644 index 000000000..1e11a21d0 --- /dev/null +++ b/jobs/bump-jenkins-plugins.Jenkinsfile @@ -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 "coreosbot-releng@fedoraproject.org" + """) + + 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}]") + } + } + } +} diff --git a/jobs/test-jenkins.Jenkinsfile b/jobs/test-jenkins.Jenkinsfile new file mode 100644 index 000000000..857984bcc --- /dev/null +++ b/jobs/test-jenkins.Jenkinsfile @@ -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"}]' + """) + } +} \ No newline at end of file