Maven build stage status from Jenkins to Slack

Adithya
Apr 21, 2019

Notify Slack about build stage status from a Maven Job from Jenkins. Assuming Slack incoming webhooks and few secret bindings in Jenkins are already done.
This seems to work only with Maven jobs.

set +x build_status=$(curl -su "$jenkins_login" "$BUILD_URL"/api/xml | grep result | sed 's/^.*<result>//' | sed 's/<\/result>.*$//') curl -X POST --data-urlencode "payload={\"channel\": \"#general\", \"username\": \"Jenkins\", \"text\": \"$JOB_BASE_NAME build - $BUILD_DISPLAY_NAME\nBuild status = "$build_status"\", \"icon_emoji\": \":jenkins:\"}" https://hooks.slack.com/services/$SLACK_TOKEN

Above script is entered into Post Steps of a Maven job.
Jenkins credentials and Slack webhook is stored as secrets.
Status of the build retrieved from Jenkins API and is stored in build_status. It is then used in curl to notify Slack.

--

--