Skip to content

Instantly share code, notes, and snippets.

@d-rk
Created September 28, 2020 14:44
Show Gist options
  • Save d-rk/6fb75131b1efd88d929995ea30b3242b to your computer and use it in GitHub Desktop.
Save d-rk/6fb75131b1efd88d929995ea30b3242b to your computer and use it in GitHub Desktop.
check for jenkins plugin updates an publish the result via slack
#!/bin/bash
# #############
# CONFIGURATION
webhook_url="xxx"
channel="Jenkins"
pretext="**Jenkins Plugin Check**"
icon_url="https://www.jenkins.io/images/logos/general/256.png"
export JENKINS_USER_ID=xxx
export JENKINS_API_TOKEN=xxx
export jenkins_url=http://localhost:8080
# CONFIGURATION
###############
if [ ! -f jenkins-cli.jar ]; then
echo "need to download jenkins cli first..."
wget $jenkins_url/jnlpJars/jenkins-cli.jar
fi
color=danger
if [ $(whoami) != "root" ]
then
text="Could not check server status. Need to run as root."
else
# list plugins / filter updates / transform to markdown
updates=`java -jar jenkins-cli.jar -s $jenkins_url list-plugins 2>/dev/null | grep -E '\([0-9].[0-9].*\)' | sed 's/ \+ /|/g' | cut -d '|' -f1,3 | sed 's/.*/|&|/' | awk '{printf "%s\\\\n", $0}'`
if [[ $updates == "" ]]
then
text="Jenkins plugins are up-to-date"
color=#00FF00
else
text="Jenkins plugins need to be upgraded! \n\n|plugin|version(update)|\n|:----|----:|\n$updates"
color=danger
fi
fi
escapedText=$(echo $text | sed 's/"/\"/g' | sed "s/'/\'/g" )
escapedPretext=$(echo $pretext | sed 's/"/\"/g' | sed "s/'/\'/g" )
json="{\"attachments\":[{\"color\":\"$color\", \"pretext\":\"$escapedPretext\" , \"text\": \"$escapedText\"}], \"icon_url\": \"$icon_url\"}"
echo curl -s -d "payload=$json" "$webhook_url"
curl -s -d "payload=$json" "$webhook_url"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment