我们使用 Jenkins 管道和 Github Multibranch。
我在一个名为 feature/my1stfeature
的功能分支上工作。 .
Jenkins 作业返回了正确的分支名称:println(env.BRANCH_NAME)
返回 feature/my1stfeature
.
然而,一旦我在 Github 中创建了我的第一个拉取请求println(env.BRANCH_NAME)
返回 PR-01
.
我当然想知道功能分支的名称。
如何解决这个问题?
最佳答案
我不确定,但这可能与您的 有关。支线
见 https://docs.cloudbees.com/docs/admin-resources/latest/multibranch-pipeline-template-syntax-guide/github
https://docs.cloudbees.com/docs/admin-resources/latest/plugins/github-branch-source
gitHubBranchDiscovery
对分支的提交使用reactgitHubPullRequestDiscovery
对拉取请求的提交使用react。 我猜你要么选择了:将拉取请求与当前目标分支修订 合并或 当前拉取请求修订版和拉取请求与当前目标分支修订版合并
Discover each pull request once with the discovered revision corresponding to the result of merging with the current revision of the target branch.
The current pull request revision
Discover each pull request once with the discovered revision corresponding to the pull request head revision without merging.
Both the current pull request revision and the pull request merged with the current target branch revision
Discover each pull request twice. The first discovered revision corresponds to the result of merging with the current revision of the target branch in each scan. The second parallel discovered revision corresponds to the pull request head revision without merging.
如果您使用“将拉取请求与当前目标分支修订合并”,那么最终会发生 Jenkins 以以下格式创建另一个临时(在代理上)分支
PR-<PR-Number>
并将其与目标分支(主)合并,然后通过你的阶段我找不到关于它如何命名为
PR-<PR-Number>
的文档,但这就是我所看到的。我特别说“另一个”,因为 Jenkins 如果 你用过
gitHubBranchDiscover
然后它将通过您的Jenkinsfile
再次为分支以及。根据我的理解你想要的是:当前的拉取请求修订版您可以拥有其他设置组合,但只需查看您的分支源并验证它是否符合您的期望。
所以基本上你有一份公关工作和一份分支机构工作。如果你有更多的触发器,你可能会有更多的工作。通常你使用:
stage('Release') {
when {
beforeAgent true
anyOf {
branch 'main'; branch 'release/*'
}
}
关于jenkins - `env.BRANCH_NAME` 变为 `PR-1`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49151984/