Fel*_*lix 21 jenkins jenkins-plugins slack-api
我想在Jenkins中使用Slack插件来ping通知到Slack通道.
Jenkins说我测试连接时成功,但我的Slack频道没有收到任何通知.
有任何已知的问题吗?
如何让Jenkins向Slack发送通知?
Ngu*_*Son 17
我认为您应该在Jenkins中添加构建后操作"Slack Notification".请看下面的图片
将Jenkins作业配置为在松弛通道上发布有两个步骤.
如果您必须配置大量Jenkins作业,则只能手动配置其中一个,并验证它是否正常工作.然后检查此Jenkins作业的config.xml以查找slack插件首选项的自动生成的xml元素,并使用regex或xslt在所有Jenkins作业上应用这些配置.在这种情况下,您必须重新加载Jenkins配置才能应用作业配置更新.("管理Jenkins"/"从磁盘重新加载配置")
先决条件:
我没有使用 Slack 通知,因为我想自定义样式/状态/消息等。所以我写了一个job
调用send_slack_notification
。每次我想通知 slack API 时,我只需在构建后调用此作业即可。
这是“执行外壳”中的代码,我使用curl
,sed
和jsawk
来完成这项工作:
# URL to get the built info json
# will get "http://JENKINS_PATH/job/JOB_NAME/97/api/json"
NEW_URL="http://jks_username:jks_password@"$(echo ${BUILD_URL} | sed -r 's/http:\/\///g')"/api/json"
# Cut the JOB_NAME part from BUILD_URL
JOB_NAME=$(echo ${BUILD_URL} | sed -n 's/.*\/job\/\(.*\)\/[0-9].*/\1/p' | sed "s@+@ @g;s@%@\\\\x@g" | xargs -0 printf "%b")
# Get the built info json
JSON=$(curl $NEW_URL)
STATUS=$(echo $JSON | /usr/local/bin/jsawk "return this.result")
BUILD_INFO=$(echo $JSON | /usr/local/bin/jsawk "return this.displayName")
TIME=$(echo $JSON | /usr/local/bin/jsawk "return this.duration")
TIME=$(echo "scale=4; $TIME/1000" | bc -l)
# Cut the username
USER=$(echo $JSON | /usr/local/bin/jsawk "return this" | sed -n "s/.*Started\ by\ \([^\"]*\).*/\1/p")
# Customize the message sending to slack
TEXT=$JOB_NAME" Built by "$USER", it took "$TIME" seconds."
# Send notification using Slack API
# will send to https://hooks.slack.com/services/BLABLABLA/BLABLABLA
curl -X POST -H 'Content-type: application/json' --data '{"channel": "#production_info","username": "jenkins-bot","icon_emoji": ":lol:","text": "'"$TEXT"' (<'"$BUILD_URL"'|Open>)", "attachments": [{"color": "#36a64f", "fields": [{"title":"UPDATE INFO","value":"'"$BUILD_INFO"'","short":true},{"title":"RESULT","value":"'"$STATUS"'","short":true}]}]}' https://hooks.slack.com/services/BLABLABLA/BLABLABLA/BLABLABLABLABLABLA
Run Code Online (Sandbox Code Playgroud)