Skip to content

Instantly share code, notes, and snippets.

@approximatenumber
Last active October 19, 2017 14:13
Show Gist options
  • Save approximatenumber/e3054000b6553552682a314fa8376c60 to your computer and use it in GitHub Desktop.
Save approximatenumber/e3054000b6553552682a314fa8376c60 to your computer and use it in GitHub Desktop.
Automatically update Jenkins plugins from pipeline job every week
#!/usr/bin/env groovy
def jenkins_cli = "/var/jenkins_home/war/WEB-INF/jenkins-cli.jar"
def jenkins_url = "http://127.0.0.1:8080/"
def admin_email = "admin@domain.com"
def credentials = "--username \"someuser\" --password \"somepass\""
node {
properties(
[
[
$class: 'BuildDiscarderProperty',
strategy: [$class: 'LogRotator', numToKeepStr: '5']
],
pipelineTriggers([cron('@monthly')]),
]
)
try {
stage('Check Updates') {
updates = sh(returnStdout: true,
script: "java -jar ${jenkins_cli} -s ${jenkins_url} list-plugins | \
grep -e ')\$' | \
awk '{ print \$1 }'").trim()
}
if (updates) {
stage('Ask') {
body = "Plugins to update:<b>${updates}</b><br> \
Please <a href='${env.BUILD_URL}/input'>decide</a> what to do.<br> \
You better <a href='${env.JENKINS_URL}/pluginManager'>look</a> at plugins</a> before updating.<br> \
Will automatically proceed in 24 hours!"
emailext body: body,
mimeType: 'text/html',
subject: 'Jenkins wants to update plugins',
to: admin_email
timeout(time: 24, unit: 'HOURS') {
input message: "===Updates===\n"+
"${updates}"+
"\n======\n"+
"Proceed to install updates?"
}
}
stage('Do Update') {
updates = updates.replace("\n", " ")
sh "java -jar ${jenkins_cli} -s ${jenkins_url} install-plugin ${updates} ${credentials}"
}
stage('Safe Restart') {
sh "java -jar ${jenkins_cli} -s ${jenkins_url} safe-restart ${credentials}"
}
}
else {
echo "No updates available!"
}
}
catch (err) {
throw err
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment