Jenkins Slack Integration (Português)

Eu não usei a notificação de folga porque eu quero personalizar o estilo / status / mensagem, etc. Então eu escrevi um job chamado send_slack_notification em vez disso. Toda vez que eu quero notificar a API de folga, ligue para este trabalho após o compilar.

Aqui está o código em “executar shell”, usei curl,

e jsawk para fazer o trabalho:

# URL to get the built info json# will get "http://JENKINS_PATH/job/JOB_NAME/97/api/json"NEW_URL="http://jks_username:[email protected]"$(echo ${BUILD_URL} | sed -r 's/http:\/\///g')"/api/json"# Cut the JOB_NAME part from BUILD_URLJOB_NAME=$(echo ${BUILD_URL} | sed -n 's/.*\/job\/\(.*\)\/.*/\1/p' | sed "[email protected][email protected] @g;[email protected]%@\\\\[email protected]" | xargs -0 printf "%b")# Get the built info jsonJSON=$(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 usernameUSER=$(echo $JSON | /usr/local/bin/jsawk "return this" | sed -n "s/.*Started\ by\ \(*\).*/\1/p")# Customize the message sending to slackTEXT=$JOB_NAME" Built by "$USER", it took "$TIME" seconds."# Send notification using Slack API# will send to https://hooks.slack.com/services/BLABLABLA/BLABLABLAcurl -X POST -H 'Content-type: application/json' --data '{"channel": "#production_info","username": "jenkins-bot","icon_emoji": ":lol:","text": "'"$TEXT"' (<'"$BUILD_URL"'|Open>)", "attachments": }]}' https://hooks.slack.com/services/BLABLABLA/BLABLABLA/BLABLABLABLABLABLA

Leave a Comment

O seu endereço de email não será publicado. Campos obrigatórios marcados com *