[英]How do I extract the Git branch in a jenkins pipeline
我正在使用通用 webhook 觸發器從拉取請求中檢索分支的名稱和哈希值。 在管道中,我想合並到一個特定的分支,因此我想提取分支的名稱。
現在分支名稱存儲在一個變量中,如果我回應它是:refs/heads/
我想執行 sh 'git merge 有什么辦法可以從 refs/heads/ 中提取 branch_name 嗎?
謝謝
您可以使用名為BRANCH_NAME
環境變量來提取分支的名稱:
pipeline {
agent any
stages {
stage ('Print branch name') {
steps {
echo "Branch is ${env.BRANCH_NAME}"
}
}
}
}
不過,如果您的 PR 分支名稱是“PR-1234”,請不要感到驚訝。
如果您使用 Github 並安裝Pipeline Github 插件,您將擁有一個名為pullRequest
的變量,可用於獲取有關拉取請求和基本分支的信息:
pipeline {
agent any
stages {
stage ('Print branch name') {
steps {
echo "Branch is ${env.BRANCH_NAME}"
script {
if (env.CHANGE_ID) {
echo "This PR is for the ${pullRequest.headRef} branch"
echo "The PR is over the ${pullRequest.base} branch."
}
}
}
}
}
}
所以我終於想出了一個優雅的解決方案:
def branchName ="ref/head/DevBranch"
`def values= branchName.tokenize( '/' )
管道{代理... {回聲“${values[2]}”...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.