jenkins - `env.BRANCH_NAME` 变为 `PR-1`

标签 jenkins github jenkins-pipeline

我们使用 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

  • 因此 GitHub 可以使用 gitHubBranchDiscovery 对分支的提交使用react
  • 它还可以使用 gitHubPullRequestDiscovery 对拉取请求的提交使用react。
  • 您也可以对两者使用react。只要确保你没有反应,如果你不需要。
  • 你也可以对其他东西使用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再次为分支以及。根据我的理解你想要的是:当前的拉取请求修订版
    您可以拥有其他设置组合,但只需查看您的分支源并验证它是否符合您的期望。
    https://www.jenkins.io/images/post-images/gsoc-gitlab-branch-source-plugin/branch-source.png
    所以基本上你有一份公关工作和一份分支机构工作。如果你有更多的触发器,你可能会有更多的工作。通常你使用:
  • 公关工作 更多地用于集成目的。它们可能发生在代理上的临时合并中。
  • 分支机构工作 用于发布目的。就像您不想在每次提交 PR 时发布。您只希望它仅在主分支或发布分支上通过发布过程,因此您这样做:
  • stage('Release') {
      when {
        beforeAgent true
        anyOf {
          branch 'main'; branch 'release/*'
        }
      }
    

    关于jenkins - `env.BRANCH_NAME` 变为 `PR-1`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49151984/

    相关文章:

    debugging - 从 Jenkins 收集生成的 PDB 文件

    jenkins - Junit 测试在 Jenkins Gradle 构建中失败,但不是在本地

    Jenkins 管道 "cd"命令不起作用, "cd"vs "dir"?

    sql-server-2008 - 如何从 GitHub 将 SQL 数据库部署到 AppHarbor?

    git - 如何取消github上的 pull 请求?

    git - Windows 上的 GitHub Desktop 上的子模块总是出现 checkout 错误

    docker - 无法通过Jenkin的Blue Ocean运行docker命令:ERRO [0000]在/etc/subuid中找不到用户 “jenkins”的subuid范围

    node.js - 如何在 Jenkins 管道中使用 SSH?

    Jenkins 电子邮件发送远程服务器 cucumber 报告

    docker - 我如何运行所有 jenkins 管道步骤,除了 docker 容器中的一个步骤