I wanted to be notified, when Jenkins / Jenkins-Plugins Update are available.
Administrations may see Update / Security-Notifications when logging in to Jenkins. Or Plugin-Updates on the Plugin-Update Site.
First i thougt: isn’t there a plugin available that sends an EMail.
I only found one that would integrade into the sidemenu.
After thinking: “Let’s write your own Plugin” the result was: It’s easier to make a Pipeline Script that is triggered periodically.
So i want’t to share my Solution with you.
Create a new Element “_Jenkins-UpdateNotifications” of Type Pipeline.
And as Definition this Pipeline-Script with your EMail:
def getUpdateNotificationReceiver() {
return "YOUR_MAIL@YOURDOMAIN"
}
pipeline {
agent any
options {
buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: "31")
timestamps()
ansiColor('xterm')
}
triggers {
cron('H 5 * * 0-5')
}
stages {
stage('Check Update Sites') {
steps {
script {
// https://javadoc.jenkins.io/hudson/model/UpdateCenter.html
jenkins.model.Jenkins.getInstance().getUpdateCenter().getSites().each { site ->
echo "... site: " + site.getUrl()
// https://javadoc.jenkins.io/hudson/model/UpdateSite.html#updateDirectlyNow(boolean)
site.updateDirectlyNow(hudson.model.DownloadService.signatureCheck)
}
}
}
}
stage('Update Downloadables') {
steps {
echo "downloadables update (Tools like Ant/Maven...)"
hudson.model.DownloadService.Downloadable.all().each { downloadable ->
echo "DL=" + downloadable.getUrl()
downloadable.updateNow();
}
}
}
stage('Core Update Check') {
steps {
script {
def html = checkCoreUpdate()
if(html != null){
emailext body: html, subject: "[JENKINS][UPDATES] Core Update available", to: getUpdateNotificationReceiver()
}
}
}
}
stage('Plugin Update Check') {
steps {
script {
def html = checkPluginUpdates()
if(html != null){
emailext body: html, subject: "[JENKINS][UPDATES] Plugin Updates available", to: getUpdateNotificationReceiver()
}
}
}
}
}
}
@NonCPS
def sortPluginsByShortName(l) {
// https://www.jenkins.io/doc/book/pipeline/cps-method-mismatches/#calling-non-cps-transformed-methods-with-cps-transformed-arguments
l.sort { it.getShortName() }
}
def checkCoreUpdate(){
// https://javadoc.jenkins.io/hudson/model/UpdateCenter.CoreUpdateMonitor.html
def coreUpdateMonitor = (UpdateCenter.CoreUpdateMonitor) Jenkins.getInstance().getAdministrativeMonitor(UpdateCenter.CoreUpdateMonitor.class.getName())
if(coreUpdateMonitor.isActivated()){
// https://javadoc.jenkins.io/hudson/model/UpdateSite.Data.html
def cData = coreUpdateMonitor.getData().core
def hStr = '''<html>
<body>
<title>Jenkins Core Update ''' + cData.version + ''' available</title>
<p>
<strong>Neue Jenkins Version available.</strong>
<br>
<a href="''' + cData.url + '''"><strong>Download</strong></a>
</p>
</body>
</html>
'''
return hStr
}else{
echo "NO CORE UPDATE"
return null
}
}
def checkPluginUpdates(){
echo "Resolving Plugin Updates"
def pluginList = new ArrayList()
// https://javadoc.jenkins.io/hudson/PluginManager.html
Jenkins.instance.pluginManager.plugins.each{ plugin ->
if(plugin.hasUpdate()){
pluginList.add(plugin)
}
}
sortPluginsByShortName(pluginList)
if (pluginList.isEmpty()) {
return null
} else {
String tBRows = '';
for(def plugin in pluginList){
// https://javadoc.jenkins.io/hudson/PluginWrapper.html
println ("${plugin.getDisplayName()} (${plugin.getShortName()}): ${plugin.getVersion()} : ${plugin.getUpdateInfo().version}")
tBRows = tBRows + '<tr><td>' + plugin.getDisplayName() + '</td><td><a href="' + plugin.getUrl() + '">' + plugin.getShortName() + '</a></td><td>' + plugin.getVersion() + '</td><td>' + plugin.getUpdateInfo().version + '</td></tr>\n'
}
def pCount = pluginList.size()
// https://wiki.jenkins.io/display/JENKINS/Hyperlinks+in+HTML
def hStr = '''<html>
<body>
<title>Jenkins Plugin Updates available</title>
<p><a href="''' + Jenkins.getInstance().getRootUrl() + '''/manage/pluginManager/"><strong>Plugin Manager</strong></a></p>
<p>
<table>
<thead>
<tr>
<th>Plugin</th>
<th>Shortname</th>
<th>Installed</th>
<th>Update</th>
</tr>
</thead>
<tbody>
''' + tBRows + '''</tbody>
</tfoot>
<tr><td colspan="4"><strong>Count: ''' + pCount + '''</strong></td></tr>
</tfoot>
</table>
</p>
</body>
</html>'''
return hStr
}
}
- We keep last 31 Buildresults
- It will be checked on Workdays at 5
- Replace YOURMAIL@YOURDOMAIN with your EMail
- EMailExt Plugin needs to be installed
- Deactivate “Use groovy Sandbox” on the Job
You have to approve this script. The Reason it is not put into SCM (you should keep your Original in an SCM) is that than you would have to allow a lot of internal Methods (Accessing Jenkins Instance / PluginManager…). So only this explicit Script needs to be approved!
I hope it helps other people keeping your Jenkins updated.