我有一個Jenkins管道工作,它被配置爲簽出一個git倉庫和一個特定的本地分支。如何從管道工作中訪問git分支名稱?
我如何在我的Jenkinsfile中獲取本地分支的名稱?
我試圖加載git jenkins plugin env屬性,但沒有運氣。
node {
checkout scm
echo "1 "+ env.GIT_LOCAL_BRANCH
echo "2 "+ env.GIT_BRANCH
}
兩個值是「空」
我有一個Jenkins管道工作,它被配置爲簽出一個git倉庫和一個特定的本地分支。如何從管道工作中訪問git分支名稱?
我如何在我的Jenkinsfile中獲取本地分支的名稱?
我試圖加載git jenkins plugin env屬性,但沒有運氣。
node {
checkout scm
echo "1 "+ env.GIT_LOCAL_BRANCH
echo "2 "+ env.GIT_BRANCH
}
兩個值是「空」
現在我使用sh調用來獲取分支名稱。這至少需要版本2.4的管道節點和進程插件。
def branchName = sh(returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim()
echo branchName
您可以使用scm
屬性來獲得配置爲您scm
分支列表:
// List of all configured branches
def allBranches = scm.branches
// Only the first configured branch name
def gitBranch = scm.branches[0].name
我發現我可以從checkout scm
獲取返回值,並用它來獲得分支名(和其他值)
def scmVars
node('api-sample-build') {
stage('Clone source code') {
scmVars = checkout scm
// scmVars contains the following values
// GIT_BRANCH=origin/mybranch
// GIT_COMMIT=fc8279a107ebaf806f2e310fce15a7a54238eb71
// GIT_PREVIOUS_COMMIT=6f2e319a1fc82707ebaf800fce15a7a54238eb71
// GIT_PREVIOUS_SUCCESSFUL_COMMIT=310fce159a1fc82707ebaf806f2ea7a54238eb71
// GIT_URL=https://stash.someworkplace.com/scm/poc/api-sample.git
}
stage('test scope') {
echo scmVars.GIT_BRANCH
}
}
通過在節點外定義變量,它在結帳後的階段中可用。
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException:腳本不允許使用方法hudson.plugins.git.GitSCM getBranches –