我创建了一个jenkins管道,它从scm中拉取管道脚本。
我将分支说明符设置为'all
',因此它基于对任何分支的任何更改。在Jenkins Jenkinsfile中获取git分支名称
如何从Jenkinsfile访问导致此构建的分支名称?
一切我都试过了回声除了空
sh(returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim()
这始终是master
。
我创建了一个jenkins管道,它从scm中拉取管道脚本。
我将分支说明符设置为'all
',因此它基于对任何分支的任何更改。在Jenkins Jenkinsfile中获取git分支名称
如何从Jenkinsfile访问导致此构建的分支名称?
一切我都试过了回声除了空
sh(returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim()
这始终是master
。
如果您的管道有jenkins文件,请检查您是否在执行时在您的environment variables中看到了您的分支名称。
你可以打印出来:
pipeline {
agent any
environment {
DISABLE_AUTH = 'true'
DB_ENGINE = 'sqlite'
}
stages {
stage('Build') {
steps {
sh 'printenv'
}
}
}
}
然而,PR 91表明分支名称在某些管道配置仅仅定义:
切换到多分支管道允许我访问分支名称。不建议使用常规管道。
是的,阅读https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/91,你应该在'BRANCH_NAME'中看到它。 – VonC
一位同事告诉我使用scm.branches[0].name
,它工作。我把它包在我Jenkinsfile功能:
def getGitBranchName() {
return scm.branches[0].name
}
谢谢,这很有帮助。看起来它不在我的环境变量中。是否有可能暴露导致构建的分支? –