Skip to content

Instantly share code, notes, and snippets.

@bhaku
Created June 22, 2015 18:17
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bhaku/11a959e2340e78041c14 to your computer and use it in GitHub Desktop.
Save bhaku/11a959e2340e78041c14 to your computer and use it in GitHub Desktop.
Jenkins groovy script to get a list of latest git branches (with release/ prefix) and latest tags for a GitLab managed repository, usable with Jenkins Extensible Choice plugin and gitflow.
#!/usr/bin/env groovy
/**
* CONFIGURATION
*/
def private_token = "secret_token_from_gitlab"
def repository = "group/repository-name"
/* CONFIGURATION END */
def repository_name = repository.replace("/", "%2F");
def api_tags = new URL("http://gitlab/api/v3/projects/" + repository_name + "/repository/tags?private_token=" + private_token).text
def api_branches = new URL("http://gitlab/api/v3/projects/" + repository_name + "/repository/branches?private_token=" + private_token).text
tags_list = new groovy.json.JsonSlurper().parseText(api_tags)
branches_list = new groovy.json.JsonSlurper().parseText(api_branches)
/* Get only branches with "release/" prefix */
def branchesMatch = "^release/.*"
def matched_branches = branches_list.findAll { it.name =~ /$branchesMatch/ }
def b = matched_branches.sort{ it.commit.committed_date }.reverse().take(5)
def t = tags_list.sort{ it.commit.committed_date }.reverse().take(10).each{ it.name = "tags/" + it.name}
def results = (t << b).sort{ it.commit.committed_date }.reverse().flatten()
return results.name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment