LoginSignup
11
9

More than 3 years have passed since last update.

[Jenkins] エラーが発生しても後続Stageを実行する書き方

Last updated at Posted at 2020-04-01

Stageを複数定義している状態で、エラー発生後のStageも実行する方法のメモ。

検証バージョン

  • Jenkins 2.222.1

エラーが発生したStageでJobが終了する書き方

pipeline {
    agent any 
    stages {
        stage('Test AP1') { 
            steps {
                echo "test ap 1"
            }
        }
        stage('Test AP2') { 
            steps {
                echo "test ap 2"
            }
        }
        stage('Test AP3') { 
            steps {
                echo "test ap 3"
            }
        }
    }
}

みたいに書くと、仮に2つ目のStageでエラーが出ると3つ目のStageが実行されない。

image.png

エラーが発生しても後続Stageを実行する書き方(エラー箇所がわからない・・・)

pipeline {
    agent any 
    stages {
        stage('Test AP1') { 
            steps {
                catchError {
                    echo "test ap 1"
                }
            }
        }
        stage('Test AP2') { 
            steps {
                catchError {
                    echo "test ap 2"
                    error "error test!!" // エラーを起こすためのダミーコード
                }
            }
        }
        stage('Test AP3') { 
            steps {
                catchError {
                    echo "test ap 3"
                }
            }
        }
    }
}

という感じにすると(catchError句で囲むと)、後続Stageを実行した上でビルドがエラーになってくれるが・・・どこのStageでエラーが出たかパッと見わからない・・。

image.png

エラーが発生しても後続Stageを実行する書き方(エラー箇所もわかる!!)

pipeline {
    agent any 
    stages {
        stage('Test AP1') { 
            steps {
                catchError(stageResult:'FAILURE') {
                    echo "test ap 1"
                }
            }
        }
        stage('Test AP2') { 
            steps {
                catchError(stageResult:'FAILURE') {
                    echo "test ap 2"
                    error "error test!!" // エラーを起こすためのダミーコード
                }
            }
        }
        stage('Test AP3') { 
            steps {
                catchError(stageResult:'FAILURE') {
                    echo "test ap 3"
                }
            }
        }
    }
}

という感じで stageResultオプションに FAILURE を指定すると、後続のStageを実行した上でエラーになったStageもわかるようになる。

image.png

最後に

ま〜とりあえずやりたいことはできたけど、個別にcatchErrorじゃなくて・・・全体のオプションで同じような動作にできたりするのかな!?

11
9
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
11
9