DEV Community

Alexei Moussatov
Alexei Moussatov

Posted on

Howto: Drop-down list with Gitlab projects in Jenkins

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()

Top comments (0)