LoginSignup
6
6

More than 5 years have passed since last update.

Jenkins PipelineでSlackへ通知する

Last updated at Posted at 2018-03-15

概要

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.')
            }
        }
    }
}

参考サイト

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6