2017-08-08 85 views
1

我在Jenkins有一個參數化(聲明)管道。該腳本應該構建作爲參數傳遞的分支,但始終以構建主分支結束。Jenkins參數化管道總是建立主分支

這是從流水線腳本在指定的分支

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/TestBranch建設回購後檢查POM版本的片段。 echo {$pom.version}顯示1.1.0-RELEASE。這是主分支的正確版本。但我正在爲1.1.1-SNAPSHOT預測我正在嘗試構建的分支TestBranch。 日誌確認它正在構建主分支而不是TestBranch。查找行:在下面的日誌

> 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/master^{commit}應該說refs/remotes/origin/origin/TestBranch^{commit}左右。

我知道,在管道配置jenkins UI我可以設置要建立的分支。但是這已經被設置到repo +分支,其中管道腳本應該被從中拉出。當我在UI上配置所有回購站時,可能會出現歧義。我需要通過管道腳本實現這一點。

感謝您的幫助!

回答

0

我想你在checkout步驟中指定了錯誤的分支,因爲userRemoteConfigs class沒有branches字段。它應該是這樣的:

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