Jenkins参数化管道始终构建主分支

时间:2017-08-08 13:48:38

标签: git maven github jenkins jenkins-pipeline

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

这是管道脚本中的片段,它在指定分支上构建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。 该日志确认它正在构建主分支而不是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 + branch,其中应该从中拉出管道脚本。当我在UI上配置所有repos时,可能会出现歧义。我需要通过管道脚本来实现这一点。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我认为您在public override void Up() { CreateTable( "dbo.Documents", c => new { Id = c.Int(nullable: false, identity: true), FileName = c.String(maxLength: 255), ContentType = c.String(maxLength: 100), Content = c.Binary(), DocumentName = c.String(maxLength: 255), UserId = c.String(maxLength: 128), }) .PrimaryKey(t => t.Id) .ForeignKey("dbo.AspNetUsers", t => t.UserId) .Index(t => t.UserId); } public override void Down() { DropForeignKey("dbo.Documents", "UserId", "dbo.AspNetUsers"); DropIndex("dbo.Documents", new[] { "UserId" }); DropTable("dbo.Documents"); } } 步骤中的错误位置指定了分支,因为userRemoteConfigs class没有checkout字段。它应该是这样的:

branches