簡體   English   中英

使用Groovy確定Jenkins的上游Git變化

[英]Determine Upstream Git Changes in Jenkins using Groovy

我們的CI / CD流程有多個存儲庫,可觸發一個下游任務來測試整個產品。

我知道對於當前的Jenkins構建,我可以使用currentBuild.changeSets獲得scm更改的列表,然后可以查看這些changeSet的所有items ,以確定提交消息,提交ID等。

但是,從Jenkins進行的測試完成后,從下游Job的角度來看,我希望能夠提及所有已發生的上游更改。

Jenkins測試作業不允許並發構建。 這意味着多個上游構建可能會觸發一個Jenkins測試工作。

我以為可以利用Causes來確定上游作業,但是我真的不知道如何從那里開始擁有changeSets。

    def upstreamChanges = ''

    def causes = currentBuild.rawBuild.causes

    if(causes && causes.upstreamCauses) { // upstream cause visibility
       def upstreamCauses = causes.upstreamCauses

        for (int i = 0; i < upstreamCauses.size; i++) {
            upstreamChanges += upstreamCauses[i].shortDescription
        }
    }

這至少為我提供了原因描述,但我希望有實際的currentBuild.changeSets可供我使用。

編輯:我基本上需要一種獲取所有上游Build對象的方法,如果有的話,我可以使用該構建的changeSets。

這很痛苦,無法弄清楚,不妨分享解決方案。

首先,我們檢查所有上游更改並添加它們,如果未返回任何內容,則嘗試獲取currentBuild.changeSets或默認為不進行任何新更改。

這里可能還有改進的余地...

 /**
 * The max commit message length
 */
final static int MAX_COMMIT_MSG_LEN = 100

 /**
 * Build the list of Changes triggering this Jenkins build, including any upstreams Jenkins jobs <br/>
 * @param s Current build object, it's a 'this' call from the pipeline
 * @return A string with all the changes in the current and upstream build, 'no new changes' is returned if empty
 */
static buildChangeList(s) {
    def changes = ''

    Run<?, ?> cur = s.currentBuild.rawBuild
    Cause.UpstreamCause upc = cur.getCause(Cause.UpstreamCause.class)
    while (upc != null) {
        Job<?, ?> p = (Job<?, ?>) Jenkins.getActiveInstance().getItemByFullName(upc.getUpstreamProject())
        if (p == null) {
            s.echo 'There is a break in the build linkage, could not retrieve upstream build information'
            break
        }
        cur = p.getBuildByNumber(upc.getUpstreamBuild());
        if (cur == null) {
            s.echo 'There is a break in the build linkage, could not retrieve upstream build information'
            break
        }
        changes += "\nJenkins Trigger Job - $upc.upstreamProject"
        changes +=  retrieveChangeSet(cur.changeSets)

        upc = cur.getCause(Cause.UpstreamCause.class)
    }

    if (!changes) { // no upstream changes at all, see if current build has any changes
        def currentBuildChanges = retrieveChangeSet(s.currentBuild.changeSets)

        changes = currentBuildChanges ?: '\n - No new changes'  // if no current build changes, use default message
    }
    return changes
}

/**
 * Retrieve all the change sets available. Include the git author, commit message and commit id
 * @param changeSets The changeSet object from Jenkins
 * @return A string with all the changes found within the change set
 */
static retrieveChangeSet(changeSets) {
    def changes = ''

    for (int i = 0; i < changeSets.size(); i++) {   // iterate through all the available change sets
        for (int j = 0; j < changeSets[i].items.length; j++) { // iterate through all the items of a single changeset
            def entry = changeSets[i].items[j]
            def commitmsg = entry.msg.take(MAX_COMMIT_MSG_LEN)
            changes += "\n[${entry.author}] - ${commitmsg}  -  Commit ${entry.commitId}"
        }
    }
    return changes
}

編輯:如果您在運行上述代碼時遇到問題,則可能是導入問題。

import hudson.model.*
import hudson.util.*
import jenkins.model.*
import hudson.FilePath
import hudson.node_monitors.*
import java.time.LocalDateTime

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM