Skip to content

Commit dda5f19

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 dda5f19

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

jobs/bump-jenkins-plugins.Jenkinsfile

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

0 commit comments

Comments
 (0)