2018年8月11日土曜日

Jenkins Pipeline Utility Stepsを使ってpomの情報を読み取る

Pipeline Utility Stepsを使うことで、Jenkinsのpipeline上でpomの情報を簡単に読み取れるようです。

プラグインのインストール

Pipeline Utility Stepsプラグインをインストールします。

パイプラインでpomの情報を参照する

readMavenPomをパイプラインのscriptブロック内で使うことでpomの内容を参照できます。
参照(変更)できる内容は、こちらを参照
stage('test') {
    steps {
        script {
            pom = readMavenPom file: 'pom.xml'
            println(pom.version)
            pom.dependencies.each {
                println(it)
            }
        }
    }
}

実行結果

上のパイプラインを実行するとバージョン番号とdependenciesの内容が出力されます。
[Pipeline] readMavenPom
[Pipeline] echo
1.0.0
[Pipeline] echo
Dependency {groupId=org.springframework.boot, artifactId=spring-boot-starter-data-jpa, version=null, type=jar}
[Pipeline] echo
Dependency {groupId=org.springframework.boot, artifactId=spring-boot-starter-thymeleaf, version=null, type=jar}
[Pipeline] echo
Dependency {groupId=org.springframework.boot, artifactId=spring-boot-starter-web, version=null, type=jar}
[Pipeline] echo
Dependency {groupId=org.springframework.boot, artifactId=spring-boot-starter-actuator, version=null, type=jar}
[Pipeline] echo
Dependency {groupId=org.postgresql, artifactId=postgresql, version=null, type=jar}
[Pipeline] echo
Dependency {groupId=org.springframework.boot, artifactId=spring-boot-starter-test, version=null, type=jar}
[Pipeline] }