Jenkins動態選擇分支/tag


一.簡介

一般選擇分支構建,Git Parameter插件即可。這里是應用pipline的同時,可以選擇分支進行構建。
file

Dynamic Parameter 可以動態的生成我們想要的一些參數,使用的語言是groovy。獲取參數后,將變量傳遞到pipline中,用於腳本使用。

二.配置

1.在插件中找到 Dynamic Parameter Plug-in 進行安裝

2.創建一個流水線項目

3.在參數化構建過程選擇 Dynamic choice Parameter

git后面那段地址,就是項目的地址,不過只能獲取一個項目的

def gettags = ("git ls-remote -h git@1.1.1.1:dd/xx.git").execute()
gettags.text.readLines().collect { it.split()[1].replaceAll('refs/heads/', '')  }.unique()

4.編寫pipline腳本,這里只寫變化的一段

stage 'checkout'

    dir('business_pay'){
          git branch: release_branch, credentialsId: 'jenkins', url: 'http://1.1.1.1/dd/xx.git'
    }

5.構建的時候,就會有選擇提示了

三.配置tag

1.tag和上述配置一樣,不過在Dynamic choice Parameter那里修改一下

def gettags = ("git ls-remote -t --refs git@1.1.1.1:dd/xx.git").execute()
gettags.text.readLines().collect { it.split()[1].replaceAll('refs/tags/', '')  }.unique()

2.然后pipeline里進行下修改,用git命令切換分支

stage 'checkout'
    dir('business_pay'){
          git branch: 'master', credentialsId: 'jenkins', url: 'http://1.1.1.1/dd/xx.git'
		  sh "git checkout $release_tag"
    }

四.其它方法

def branchType = BRANCH_NAME.tokenize('/').last().trim()
isReleaseBranch = branchType == 'tmo_ps_gp15' || branchType == 'master'
branch = isReleaseBranch ? branchType : BRANCH_NAME.trim()
originalBuildNumber = BUILD_NUMBER
revision = "1.0.${BUILD_NUMBER}"


def setDisplayName(){
 if (BUILD_NUMBER != originalBuildNumber) {
  currentBuild.displayName = "${revision}-${branch} (resume ${BUILD_NUMBER})"
 } else if (isReleaseBranch){
  currentBuild.displayName = "${revision}-${branch}"
 }
}

BRANCH_NAME.tokenize('/').last().trim()
可以拿到你當前checkout下來是哪個branch, tokenize('/').last().trim() 意思就是以 / 切割 然后取最后一個元素並且去除該元素左右兩邊得空格,這樣就可以拿到你checkout得branch

isReleaseBranch = branchType == 'tmo_ps_gp15' || branchType == 'master'
意思就是如果 你上面定義得branchType 等於tmo_ps_gp15 或者是master 分支得時候, isReleaseBranch 就等於呢個分支名且為true

branch = isReleaseBranch ? branchType : BRANCH_NAME.trim()
意思就是當isReleaseBranch為true得情況下,branch等於branchType ,如果為false就重新抓取你checkout得branch name

用於下面pipeline得判斷而已
你可以根據你構建/部署情況而定
一個pipeline同一個代碼可以作用於不同分支,不同分支得構建/部署不同得情況下,我都是在開頭去判斷得
后面你代碼就可以直接 if(isReleaseBranch){xxxx}了

五.List Git Branches插件

插件List Git Branches可以根據選項獲取遠程倉庫的分支或者tag,形成列表選項,除了在job進行圖形配置,還可以通過pipeline進行配置,在job過百的情況下搭配共享庫會很方便。

界面配置:

具體說明
Name:變量名詞,用於傳遞到pipeline根據分支發布
Repository URL:遠程倉庫的地址,從這個倉庫獲取分支
Credentials:選擇的憑證,根據這個來訪問遠程倉庫
Parameter Type:獲取的類型,可選TAG、分支、分支+TAG
Sort Mode:排序的方式
Tag Filter:過濾tag的正則,如果獲取類型是分支,這個配置可以忽略
Branch Filter:過濾分支的正則
Default Value:如果獲取不到默認的分支
Selected Value:不太清楚,選擇NONE或者DEFALUT即可
List Size:這是顯示的列表個數

效果:

pipeline方式:

pipeline {
  agent any
  parameters {
    listGitBranches branchFilter: 'refs/heads/(.*)',
        defaultValue: 'master',
        name: 'branch_name',
        type: 'PT_BRANCH',
        remoteURL: 'http://10.0.15.1/xxx/xxx.git',
        credentialsId: 'jenkins',
        selectedValue: 'DEFAULT',
        sortMode: 'ASCENDING'
  }

  stages {
    stage('Example') {
      steps {
        git branch: "${params.branch_name}", credentialsId: 'jenkins', url: 'http://10.0.15.1/xxx/xxx.git'
      }
    }
  }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM