7

我試圖在jenkinsfile中獲取git commit消息並阻止基於commit消息的構建。如何獲取git最新提交信息並防止jenkins生成,如果提交信息包含[ci skip]?

env.GIT_COMMIT不會在jenkinsfile中返回提交詳細信息。

如何獲取git最新提交消息並防止jenkins生成,如果提交消息中包含[ci skip]?

+2

請嘗試sh「git log -1」並grep相關文本。 – Amityo

+0

@Amityo:謝謝你真的很有幫助。我還需要一個查詢...如果郵件中包含[ci skip]內容,如何防止jenkins在jenkins文件中生成? –

回答

10

構建將通過當[CI跳過]在過去的git的日誌設置,但不會運行的實際構建代碼(更換到第一回波語句)

node { 
    checkout scm 
    result = sh (script: "git log -1 | grep '\\[ci skip\\]'", returnStatus: true) 
    if (result != 0) { 
    echo "performing build..." 
    } else { 
    echo "not running..." 
    } 
} 
+1

有關如何使用'sh'的更多信息:https://issues.jenkins-ci.org/browse/JENKINS-26133 –

3

我認爲你可以很容易地這樣做,在多分支管道作業配置 科源>其他行爲>輪詢忽略提交某些消息 multi branch pipeline job configuration

+0

使用此方法時,$ CHANGE_ID在Jenkinsfile中不可用。 –

5

我有同樣的問題。我正在使用管道。我通過實施shared library解決了這個問題。

庫的代碼是這樣的:

// vars/ciSkip.groovy 

def call(Map args) { 
    if (args.action == 'check') { 
     return check() 
    } 
    if (args.action == 'postProcess') { 
     return postProcess() 
    } 
    error 'ciSkip has been called without valid arguments' 
} 

def check() { 
    env.CI_SKIP = "false" 
    result = sh (script: "git log -1 | grep '.*\\[ci skip\\].*'", returnStatus: true) 
    if (result == 0) { 
     env.CI_SKIP = "true" 
     error "'[ci skip]' found in git commit message. Aborting." 
    } 
} 

def postProcess() { 
    if (env.CI_SKIP == "true") { 
     currentBuild.result = 'NOT_BUILT' 
    } 
} 

然後,在我的Jenkinsfile:

pipeline { 
    stages { 
    stage('prepare') { steps { ciSkip action: 'check' } } 
    // other stages here ... 
    } 
    post { always { ciSkip action: 'postProcess' } } 
} 

正如你所看到的,構建標記爲NOT_BUILT。如果您願意,可以將其更改爲ABORTED,但不能設置爲SUCCESS,因爲a build result can only get worse

+0

謝謝,它效果很好! #testedAndApproved –