4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Jenkins Pipelineでエラーをメール通知する

Last updated at Posted at 2018-03-15

概要

Jobエラーをメール通知する方法を調べたのでメモしておく。

ファイル構成

ファイル構成
├ script
│  └ mail.groovy
└ Jenkinsfile

通知メソッド作成

外部ファイルに分離してメソッドを定義します。

script/mail.groovy
/**
 * メール通知
 *
 */
// メールをGmailに送信する
def send(result) {
    mail to: "hoge@example.com",
        subject: "${env.JOB_NAME} #${env.BUILD_NUMBER} [${result}]",
        body: "Build URL: ${env.BUILD_URL}.\n\n"
}
return this

Jenkinsfile作成

テストシナリオを作成する。

Jenkinsfile
# !/usr/bin/env groovy

pipeline {
    agent any
    stages {
        stage('fail') {
            error('Fail')
        }
    }
    post {
        failure {
            mail = load "${pwd()}/script/mail.groovy"
            mail.send(currentBuild.currentResult)
        }
    }
}
4
2
2

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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?