Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the git parameter plugin (for branches) in a jenkins pipeline?

I want to list my branches as parameter in Jenkins. It's possible in the freestyle job (using the git parameter plugin). But I don't know how to make it work inside a pipeline?

The plugin tells us they have added pipeline support but there isn't an example somewhere.

like image 524
DenCowboy Avatar asked Jan 29 '23 15:01

DenCowboy


2 Answers

For a declarative Pipeline, you can add a git Parameter like this:

pipeline{
  agent any 

  parameters {
    gitParameter(
      branch: '',
      branchFilter: ".*",
      defaultValue: "",
      description: '',
      listSize: '10',
      name: 'Version',
      quickFilterEnabled: false,
      selectedValue: 'NONE',
      sortMode: 'ASCENDING_SMART',
      tagFilter: "*",
      type: 'PT_BRANCH_TAG',
      useRepository: '[email protected]:foo/bar.git')
   }
   stages{
      stage ("echo Git Tag") {
        steps {
          echo "${params.Version}"
        }
     }
  } 
}

The example above will show you all branches and tags available on the repo. if you want to display only tags, change the type to

type: 'PT_TAG'

if you only want to show specific tags you can filter, for example, only show tags that start with "foo"

tagFilter: "foo*"

If you want to see more details, just check out the Pipeline Syntax Generator. you will find this at:

Sample Step -> properties -> This project is parameterised -> add Parameter -> git Parameter

like image 113
Tinky_ Avatar answered Feb 07 '23 09:02

Tinky_


I advice you to please go through multi-branch pipeline plugin

Let's say you have more than one branch available in GIT. Creating a multi-branch pipeline Job allows you to distinguish and run branch based Jenkins Jobs under single project.

Apart from GIT, It also support Bit-bucket, GitHub, Subversion, Mercurial, Single repository & branch.

like image 20
Shwetank Vashishtha Avatar answered Feb 07 '23 10:02

Shwetank Vashishtha