DEV Community

Alexei Moussatov
Alexei Moussatov

Posted on

1

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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay