2
1

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 3 years have passed since last update.

Jenkins2.0のPipelineでupstrem buildを取得する

Last updated at Posted at 2017-03-13

諸事情でjenkinsのUpstreamJob(上流ジョブ)の差分が欲しくなったのでそれをどうしたもんかなーと思って作ってみました。
これやるとpermissionがめちゃくちゃ出るので頑張って許可してあげてください。

currentBuild.rawBuild.causesでビルドの原因がとれます。
そのなかの最初のものが上流ジョブだったら上流ビルドをラップして返してます。
あとはbuild.changeSetsで差分が取れるのでそれを文字列にしておしまいです。
ただ、build.changeSetsはCPSに引っかかるので取り扱い注意

あと直近の原因が[0]でとれるのかは知りません。だれか確認してー

getUpstream

@NonCPS
def getUpstreamBuild(){
    //ビルドの原因
    def causes = currentBuild.rawBuild.causes
    if(causes.isEmpty()) { //ないけど一応
        return null
    }
    def upstream = causes[0]
    //上流以外の要因でのビルド
    if(upstream.class != hudson.model.Cause.UpstreamCause.class) {
        return null
    }
    
    def r = new org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper(upstream.upstreamRun,false)

    return r
}

@NonCPS
def getChangeLog(build)
{
    def changeSets = null
    // empty List
    if( build.changeSets.isEmpty() ) {
        return "差分がありません"
    }
    changeSets = build.changeSets.toArray()
    
    def diff = ""
    if( changeSets.size() != 1 ){ // Multiple SCM

        diff += "差分が複数あります。最初の一つを採用します\n"
    }
    changes = changeSets[0]
    for( change in changes )
    {
        diff += change.msg + ":" + change.author.fullName + "(" + change.commitId + ")" + System.properties['line.separator']
    }
    changes = null // avoid CPS
    return diff
    
}
echo getChangeLog(getUpstream())

2
1
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?