Last active
October 19, 2017 14:13
-
-
Save approximatenumber/e3054000b6553552682a314fa8376c60 to your computer and use it in GitHub Desktop.
Automatically update Jenkins plugins from pipeline job every week
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
#!/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