Skip to content

Commit 9eac60e

Browse files
aaradhakmarmijo
andcommitted
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
1 parent aa70ee8 commit 9eac60e

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

jobs/bump-jenkins-plugins.Jenkinsfile

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
fork_repo = "coreosbot-releng/fedora-coreos-pipeline"
2+
botCreds = "github-coreosbot-releng-token-username-password"
3+
pr_branch = "pluginsupdate"
4+
5+
6+
def getVersionFromPluginUrl(pluginUrl) {
7+
//example url : https://updates.jenkins.io/download/plugins/${pluginName}/latest/${pluginName}.hpi
8+
def parts = pluginUrl.split("/")
9+
def pluginVersion
10+
if (parts.size() >= 4) {
11+
def groupId = parts[-3]
12+
pluginVersion = parts[-2]
13+
} else {
14+
error("Unable to extract plugin version from the URL.")
15+
}
16+
return pluginVersion
17+
}
18+
19+
node {
20+
checkout scm: [
21+
$class: 'GitSCM',
22+
branches: [[name: '*/pluginsupdate']],
23+
userRemoteConfigs: [[url: "https://github.com/${fork_repo}.git"]],
24+
extensions: [[$class: 'WipeWorkspace']]
25+
]
26+
// these are script global vars
27+
pipeutils = load("utils.groovy")
28+
29+
properties([
30+
pipelineTriggers([
31+
// check once a month
32+
pollSCM('H H 1 * *')
33+
]),
34+
buildDiscarder(logRotator(
35+
numToKeepStr: '100',
36+
artifactNumToKeepStr: '100'
37+
)),
38+
durabilityHint('PERFORMANCE_OPTIMIZED')
39+
])
40+
41+
try {
42+
shwrap("""
43+
git config --global user.name "CoreOS Bot"
44+
git config --global user.email "[email protected]"
45+
""")
46+
47+
def pluginslist
48+
def pluginsToUpdate = [:]
49+
def plugins_lockfile = "jenkins/controller/plugins.txt"
50+
51+
stage("Read plugins.txt") {
52+
shwrapCapture("""
53+
git clone --branch pluginsupdate https://github.com/${fork_repo}.git
54+
55+
cd fedora-coreos-pipeline
56+
57+
# Check if the branch exists
58+
if git ls-remote --heads --exit-code origin ${pr_branch} | grep ${pr_branch}; then
59+
git checkout ${pr_branch}
60+
else
61+
git checkout -b ${pr_branch}
62+
fi
63+
""")
64+
pluginslist = shwrapCapture("grep -v ^# ${plugins_lockfile}").split('\n')
65+
}
66+
67+
stage("Check for plugin updates") {
68+
def pluginUrl
69+
pluginslist.each { plugin ->
70+
def parts = plugin.split(':')
71+
if (parts.size() == 2) {
72+
def pluginName = parts[0]
73+
def currentVersion = parts[1]
74+
pluginUrl = shwrapCapture("curl -Ls -I -f -o /dev/null -w '%{url_effective}' https://updates.jenkins.io/download/plugins/${pluginName}/latest/${pluginName}.hpi")
75+
def latestVersion = getVersionFromPluginUrl(pluginUrl)
76+
if (latestVersion.toString() != currentVersion.toString()) {
77+
pluginsToUpdate["${pluginName}"] = [currentVersion, latestVersion]
78+
println("Plugin: ${pluginName} current version is ${currentVersion}, it will be updated to latest version: ${latestVersion}")
79+
shwrap("""
80+
cd fedora-coreos-pipeline
81+
sed -i '/${pluginName}:/ s/${currentVersion}/${latestVersion}/g' ${plugins_lockfile}
82+
""")
83+
} else {
84+
println("The latest version of ${pluginName} is already installed: ${currentVersion}")
85+
}
86+
} else {
87+
error("Invalid plugin format: ${plugin}")
88+
}
89+
}
90+
}
91+
92+
stage("Open a PR") {
93+
if (shwrapCapture("git diff --exit-code")){
94+
def message = "jenkins/plugins: update to latest versions"
95+
shwrap("""
96+
cd fedora-coreos-pipeline
97+
git add jenkins/controller/plugins.txt
98+
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'
99+
""")
100+
withCredentials([usernamePassword(credentialsId: botCreds,
101+
usernameVariable: 'GHUSER',
102+
passwordVariable: 'GHTOKEN')]) {
103+
shwrap("""
104+
cd fedora-coreos-pipeline
105+
git push -f https://\${GHUSER}:\${GHTOKEN}@github.com/${fork_repo} ${pr_branch}
106+
curl -H "Authorization: token ${GHTOKEN}" -X POST -d '{ "title": "${message}", "head": "${pr_branch}", "base": "main" }' https://api.github.com/repos/${fork_repo}/pulls
107+
""")
108+
}
109+
}
110+
}
111+
} catch (e) {
112+
currentBuild.result = 'FAILURE'
113+
throw e
114+
} finally {
115+
if (currentBuild.result == 'SUCCESS') {
116+
currentBuild.description = "[${gitref}@${shortcommit}] ⚡"
117+
} else {
118+
currentBuild.description = "[${gitref}@${shortcommit}] ❌"
119+
}
120+
if (currentBuild.result != 'SUCCESS') {
121+
message = "build-${containername} #${env.BUILD_NUMBER} <${env.BUILD_URL}|:jenkins:> <${env.RUN_DISPLAY_URL}|:ocean:> [${gitref}@${shortcommit}]"
122+
pipeutils.trySlackSend(color: 'danger', message: message)
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)