Git select a branch in Jenkins using groovy script

I am trying to do a Parameterized build in Jenkins. Thus, the user can select the git branch that he wants to expand from the cascading menu.

Two ways are possible:

  • Writing the branch names in the file and setting up Jenkins to read this file ( project configuration > extend choice parameter and selecting Property file ).

    Problem. You must make the local repository a mirror of the remote repo and keep this local repo in sync with the remote repo. In other words, you need to update the file containing the name of the available branch. This requires a scheduled cron job, and I am not allowed to use this approach.

  • Using the Groovy script ( project configuration > extend choice parameter and selecting "Groovy script" ). Then you need a Groovy script to get the branch name as follows: branches=master,feature/Feature-1,feature/Feature-2,hotfix/Hotfix-1,release/Release-1 .

I found the Groovy script in here , but it does not work. I installed Groovy on my machine.

Can someone help me? To make the story short: I need a Groovy script that returns the available branch names of the remote repository.

+9
git jenkins groovy
Aug 26 '14 at 15:00
source share
4 answers

Below is the script below. It is based on scenarios from a related question . It filters the output of the git command with a simple regular expression and creates a list of branch names for the specified git repository. Tested on grails-core github repo :

 def gitURL = "https://github.com/grails/grails-core.git" def command = "git ls-remote -h $gitURL" def proc = command.execute() proc.waitFor() if ( proc.exitValue() != 0 ) { println "Error, ${proc.err.text}" System.exit(-1) } def branches = proc.in.text.readLines().collect { it.replaceAll(/[a-z0-9]*\trefs\/heads\//, '') } println branches 
+12
Aug 26 '14 at 17:19
source share

You do not need Groovy Script for this. Git Parameter plugin allows you to add git branches as a parameter.

+3
Feb 26 '15 at 10:43
source share

You can use the advanced option select plugin to extract the Git branches of your cloned repository. To do this, add the .git directory path environment variable to the Master-Node property, for example:

enter image description here

Then add the Extensible Choice parameter with the following groovy script (and check the "Use predefined variables" box):

 def envVars = jenkins.getNodeProperties()[0].getEnvVars() def NODE_PROJECT_PATH = envVars.get('NODE_PROJECT_PATH') def gettags = "git ls-remote -t --heads origin".execute(null, new File(NODE_PROJECT_PATH)) return gettags.text.readLines() .collect { it.split()[1].replaceAll('\\^\\{\\}', '').replaceAll('refs/\\w+/', '') } .unique() .findAll { !it.startsWith('Branch_') } 

This should indicate your branches (I have filtered out all the "Branch_ *" from the list):

enter image description here

Notes: If you do not see anything when checking the script (with the "Run the script now button" button), probably due to the required user / password hint - so first run "git ls-remote -t ​​-heads origin" in the directory .git. To save your credentials on Windows, try running "git config --global credential.helper wincred".

+2
Jun 14 '16 at 11:30
source share

Try using

Use the following steps:

  • Add an extension option for your work

    enter image description here

  • select Groovy System Selection Option in the Selection Option Field

    enter image description here

  • Place the following script in the Groovy script text box and replace the place holders "<>" with the required values.

     import groovy.json.JsonSlurper; try{ List<String>params = new ArrayList<String>() URL apiUrl = "https://api.github.com/users/<repo-owner>/repos?access_token=<github-access-token>".toURL() List branches = new JsonSlurper().parse(apiUrl.newReader()) for (branch in branches ) { params.add(branch.name) } return params } catch(IOException ex){ print ex } 
  • Add active active reactive parameter to your work

    enter image description here

  • Put the Reference parameter as a repository and follow the script in the Groovy script text box

     import groovy.json.JsonSlurper; try{ List<String>params = new ArrayList<String>() URL apiUrl = "https://api.github.com/repos/<repo-owner>/$repository/branches?access_token=<github-access-token>".toURL() List json = new JsonSlurper().parse(apiUrl.newReader()) for (repo in json ) { params.add(repo.name) } return params } catch(IOException ex){ print ex } 

    enter image description here

    Note:

    • $repository takes a dynamic value from a repository parameter.
    • ignore public repository.
  • Now try to complete the task with the parameter

    enter image description here enter image description here

checkout github api docs for more information. Hope this will be helpful. !!; -)

0
Dec 19 '17 at 5:23
source share



All Articles