【问题标题】:dynamically replace branch name in jenkinsfile动态替换 jenkinsfile 中的分支名称
【发布时间】:2018-03-18 15:51:45
【问题描述】:

我是 Jenkins 的新手,一直坚持从 Jenkins 替换 Jenkinsfile 中的分支名称。

我想编写一个通用的 Jenkinsfile,并且我想从 Jenkins 中选择分支名称。以下是我的 Jenkinsfile 中的 sn-p

checkout([$class: 'GitSCM', 分支: [[name: '*/master']],

无论我从 Jenkins 中选择什么分支名称,它都应该替换 Jenkinsfile 中的相同名称并进行构建。

我已经浏览了答案 parametrized build

有点无法做到这一点。 任何人都可以参考我可以通过并实施相同的文档/链接吗?

我的改变:

checkout([$class: 'GitSCM', branches: [[name: 'params.Branch_Select']],

在此处输入图片说明

【问题讨论】:

    标签: jenkins jenkins-pipeline


    【解决方案1】:

    我已经解决了上述问题。取消选中该框。

    【讨论】:

      【解决方案2】:

      在您的 Jenkinsfile 中,添加一个参数部分,该部分将接受 BRANCH_NAME 作为参数

      parameters {
              string(defaultValue: "master", description: 'enter the branch name to use', name: 'BRANCH_NAME')
          }
      

      并在结帐步骤中使用该变量,如下所示:

      checkout([$class: 'GitSCM', branches: [[name: "*/${BRANCH_NAME}"]]])
      

      注意:您必须运行一次构建才能看到“使用参数构建”选项。

      只有一个结帐阶段的示例管道:

      pipeline{
          agent any
          parameters {
              string(defaultValue: "develop", description: 'enter the branch name to use', name: 'BRANCH_NAME')
          }
          stages{
              stage('Checkout'){
                  steps{
                      checkout([$class: 'GitSCM', branches: [[name: "*/${BRANCH_NAME}"]], gitTool: 'jgit', userRemoteConfigs: [[url: "https://github.com/githubtraining/hellogitworld.git"]]])
                      //checkout([$class: 'GitSCM', branches: [[name: "*/${BRANCH_NAME}"]],url:'https://github.com/brianmyers/hello'])
                  }
              }
          }
      }
      

      【讨论】:

      • 它无法读取我的 Jenkins 文件。找不到 git repo
      • 我添加了示例管道。
      • 感谢您的帮助
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-12
      • 1970-01-01
      • 2018-11-16
      • 2021-12-26
      • 1970-01-01
      • 2010-09-06
      • 1970-01-01
      相关资源
      最近更新 更多