loading...

Howto: Drop-down list with Gitlab projects in Jenkins

malexe profile image Alexei Moussatov ・1 min read

In the previous receipt about Drop-down list for values we learned how-to choice any component from Jira. But in Jira ofteт isn't need in real life for development process.
At this receipt I've tell about how to create drop-down list for choosing projects's names by regex from Gitlab via API.

Prerequisites:

Step 1: In job configuration add parameter Active Choice Reactive Parameter

Step 2: Set name for your parameter (for example COMPONENT)

Step 3: Select Groovy script and add next groovy-code

import groovy.json.JsonSlurperClassic
import java.util.regex.*

def projectNamespace = JIRA_PROJECT.toLowerCase()
def regexLine = "^((${projectNamespace}-(int|sys|service|bpl|pl|stp|tst)-([a-z,0-9,-]*)))"
def gitlabUrl = "https://gitlab.domain.org"
def gitlabApiToken = "secretToken"
def per_page = 128

def data = []
def url = "curl -k -H PRIVATE-TOKEN:${gitlabApiToken} ${gitlabUrl}/api/v4/projects?per_page=${per_page}"
def json = url.execute().text.replaceAll("\r\n", "")
data += new JsonSlurperClassic().parseText(json)

def list = []
data.each { project ->
    Matcher projectMatcher = Pattern.compile(regexLine).matcher(project.path)
    if(projectMatcher && project.namespace.path == projectNamespace) {
        list += project.path
    }
}
return list.sort()

Discussion

pic
Editor guide