概要
Jenkins PipelineでエラーをSlackに通知したかったので調べた内容をメモしておきます。
ファイル構成
ファイル構成
├ script
│ └ slack.groovy
└ Jenkinsfile
slackメソッド作成
外部ファイルに分離してメソッドを定義します。
script/slack.groovy
/**
* Slack通知
*
*/
def post(color, message) {
def slack_channel = "#jenkins"
def slack_domain = "hoge-inc"
def slack_icon = ""
def detail_link = "(<${env.BUILD_URL}|Open>)"
def slack_msg = "job ${env.JOB_NAME}[No.${env.BUILD_NUMBER}] was test ${currentBuild.result}. ${detail_link} \n\n ${message}"
withCredentials([string(credentialsId: 'slack', variable: 'Token')]) {
slackSend channel: "${slack_channel}", color: "${color}", message: "${slack_msg}", teamDomain: "${slack_domain}", token: "${Token}"
}
}
return this
Jenkinsfile作成
テストシナリオを作成する。
Jenkinsfile
#!/usr/bin/env groovy
pipeline {
agent any
stages {
stage('run1') {
steps {
slack = load "${pwd()}/script/slack.groovy"
slack.post('good', 'credential test.')
}
}
}
}