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.

Jenkins pipelineでスクリプトの実行結果を取得

Posted at

jenkins pipelineでジョブを作成中、スクリプトの実行結果を変数として取得して使用したかったのですが意外と方法が分からず、調べた結果のメモとして記録します。

pipeline {
    agent any
    environment {
        TARGET_DATE = ""
    }
    stages {
        stage('test') {
            steps {
                script {
                    def result = sh (
                        script:"date --date '7 day ago' +%Y%m%d",
                        returnStdout:true
                        )
                    TARGET_DATE = result
                
                    echo "${TARGET_DATE}"
                    // 20230707
                }
            }
        }
    }
}

script{}内に関数を定義し、returnStdout:trueにすることでスクリプトの実行結果が取得でき、グローバル変数が更新されました。

ちなみに次のようにreturnStatus:trueにすると終了コードが取得できます。

pipeline {
    agent any
    stages {
        stage('test') {
            steps {
                script {
                    def result = sh (
                        script:"date --date '7 day ago' +%Y%m%d",
                        returnStatus:true
                        )

                    if (result != 0) {
                        error("FAILED to get date.")
                    }
                }
            }
        }
    }
}
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?