【问题标题】:Jenkins Parameterized pipeline always builds master branchJenkins 参数化管道始终构建主分支
【发布时间】:2017-08-08 13:48:37
【问题描述】:

我在 Jenkins 中有一个参数化(声明性)管道。该脚本应该构建作为参数传递的分支,但最终总是构建主分支。

这是管道脚本中的 sn-p,用于在指定分支上构建 repo 后检查 pom 版本

pipeline {
agent any

tools {
    maven "localMaven"
    git "Default"
}

parameters {
    string(defaultValue: 'develop', description: 'Commit/Branch', name: 'prop1')
}

stages {
    stage('Pom-Version') {
        steps{
            echo "prop1 $prop1"

            checkout([$class: 'GitSCM', 
                      userRemoteConfigs: [[url: 'https://github.com/path/to/repo', 
                                           credentialsId: 'xxx',
                                           branches: [name: "${params.prop1}"]]]
                    ])

            script {
                pom = readMavenPom file: 'pom.xml'
                modelversion = pom.version.substring(0, pom.version.lastIndexOf("-"))
            }
            sh "echo {$pom.version}"
            sh "echo {$modelversion}"
        }
    }
  .....

我设置了参数prop1=refs/heads/TestBranchecho {$pom.version} 显示 1.1.0-RELEASE。这是 master 分支的正确版本。但我期待1.1.1-SNAPSHOT 用于我实际上正在尝试构建的分支TestBranch。 日志确认它正在构建 master 分支而不是 TestBranch。在下面的日志中查找行:refs/remotes/origin/origin/master^{commit}

 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/https://github.com/path/to/repo # timeout=10
Fetching upstream changes from https://github.com/https://github.com/path/to/repo
 > git --version # timeout=10
using GIT_ASKPASS to set credentials
 > git fetch --tags --progress https://github.com/https://github.com/path/to/repo +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 9832b614717ebf86f93d983342787b717dcfb4d9 (refs/remotes/origin/master)
Commit message: "Merge branch 'release/1.1.0-RELEASE'"
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 9832b614717ebf86f93d983342787b717dcfb4d9
 > git rev-list 9832b614717ebf86f93d983342787b717dcfb4d9 # timeout=10

应该说refs/remotes/origin/origin/TestBranch^{commit}左右。

我知道在管道配置中的 jenkins UI 上,我可以设置要构建的分支。但这已经设置为应该从中提取管道脚本的 repo + 分支。当我在 UI 上配置所有存储库时,可能会出现歧义。我需要通过管道脚本来实现这一点。

感谢您的帮助!

【问题讨论】:

    标签: git maven github jenkins jenkins-pipeline


    【解决方案1】:

    我认为您在checkout 步骤中指定了错误位置的分支,因为userRemoteConfigs class 没有branches 字段。应该是这样的:

    checkout([$class: 'GitSCM',
             branches: [[name: "${params.prop1}"]],
             userRemoteConfigs: [[url: 'https://github.com/path/to/repo', 
                                  credentialsId: 'xxx']]
             ])
    

    【讨论】:

      猜你喜欢
      • 2020-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-02
      • 1970-01-01
      • 2018-06-26
      相关资源
      最近更新 更多