forked from coreos/fedora-coreos-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
jobs: Add a bump-jenkins-plugins job
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
Showing
1 changed file
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("Open a PR") { | ||
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}]") | ||
} | ||
} | ||
} | ||
} |