0
0

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 1 year has passed since last update.

Jenkinsfileの記載方法いろいろ

Last updated at Posted at 2023-01-22

概要

Jenkinsfileを記載する際のTips

処理の外だし

pipelineのstageやstepに処理を記載すると可読性が悪くなってしまう。
関数として処理を外だしすることで、スッキリ処理を記載することが可能。

Jenkinsfileの処理は基本的にGroovyで記載できる
例外

pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                script {
                    sampleFunc(引数) //関数呼び出し
                }
            }
        }
    }
}

def sampleFunc(引数){
    処理記載
}

各stageでerrorが発生しても、後続のstage処理を実行

参考
catchError句で処理を囲むと、処理がストップせずに後続のstage処理が実行される

pipeline {
    agent any 
    stages {
        stage('Test2') { 
            steps {
                catchError {
                    echo "test ap 1"
                    error "error test"
                }
            }
        }
        stage('Test2') { 
            steps {
                echo "test ap 2"
            }
        }
    }
}

繰り返し処理によるstageの作成

参考
script内でfor文を記載し、stageを作成することが可能

pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                script {
                    def list = ['a', 'b', 'c']
                    for (int i = 0; i < list.length; i++) {
                        stage("Test ${list[i]}") {
                            sh '....'
                        }
                    }
                }
            }
        }
    }
}
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?