Using Jenkins Active Choice Parameter Plugin for Dynamic Builds and GitLab API Integration
This guide explains how to install and configure the Jenkins Active Choice Parameter plugin, create Groovy scripts for dynamic build options, debug GitLab API calls with curl or Postman, and implement both basic and optimized Groovy code to retrieve project branches and tags using stored Jenkins credentials.
The article introduces the Jenkins Active Choice Parameter plugin, which enables parameterized builds with dynamic option lists such as checkboxes, radio buttons, and multi‑select values.
Installation is performed via Manage Jenkins → Manage Plugins → Available , searching for "Active Choice" and restarting Jenkins after installation.
Dynamic options are generated with Groovy scripts; the script must return an array of values, and conditional logic (e.g., if (buildType == "maven") { … }) can be used to adjust the options based on other parameters.
To interact with GitLab, the guide shows how to debug the API using curl or Postman, providing an example POST request that lists branches.
Unoptimized Groovy implementation fetches branches or tags directly from GitLab by constructing a URL, opening an HttpURLConnection, setting the request method and token, and parsing the JSON response with JsonSlurper. The code returns a list of names for either branches or tags.
import groovy.json.JsonSlurper
JsonSlurper slurper = new JsonSlurper()
if (refType.equals("branch")) {
try {
String apiServer = "https://gitlab.com/api/v4/"
URL url = new URL(apiServer + "projects/18803707/repository/branches")
HttpURLConnection con = (HttpURLConnection) url.openConnection()
con.setRequestMethod("GET")
con.setRequestProperty("PRIVATE-TOKEN", "DTyTRQ-ky1wZJ-zSSKRW")
con.setDoOutput(true)
con.getInputStream()
responseCode = con.getResponseCode()
println(responseCode)
response = slurper.parse(con.getInputStream())
refs = []
for (item in response) { refs.add(item.name) }
return refs
} catch (Exception e) { return [] }
}
// similar block for tags …Optimized version stores credentials in Jenkins, defines a reusable HttpReq function for HTTP calls, and provides separate functions GetTagList and GetBranchList. The final logic selects the appropriate list based on refType and returns the result.
def GetJenkinsCredentials(id, type){
def jenkinsCredentials = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
com.cloudbees.plugins.credentials.Credentials.class,
Jenkins.instance,
null,
null);
for (creds in jenkinsCredentials) {
if (creds.id == id && type == "secret") { return creds.secret }
}
}
def HttpReq(data, method, apiUrl){
JsonSlurper slurper = new JsonSlurper()
String gitlabToken = GetJenkinsCredentials("bba72a35-6857-4be6-9e71-cb6eed4db403/update", "secret")
try {
String apiServer = "https://gitlab.com/api/v4/"
URL url = new URL(apiServer + apiUrl)
HttpURLConnection con = (HttpURLConnection) url.openConnection()
con.setRequestMethod(method)
con.setRequestProperty("PRIVATE-TOKEN", gitlabToken)
con.setDoOutput(true)
if (data != "") { con.getOutputStream().write(data.getBytes("UTF-8")) }
con.getInputStream()
responseCode = con.getResponseCode()
response = con.getInputStream()
return slurper.parse(response)
} catch (Exception e) { return [] }
}
def GetTagList(){
def refs = []
ArrayList response = HttpReq("", "GET", "projects/18803707/repository/tags")
for (item in response) { refs.add(item.name) }
return refs
}
def GetBranchList(){
def refs = []
ArrayList response = HttpReq("", "GET", "projects/18803707/repository/branches")
for (item in response) { refs.add(item.name) }
return refs
}
if (refType.equals("tag")) { return GetTagList() }
if (refType.equals("branch")) { return GetBranchList() }The final result is a Jenkins pipeline that can dynamically present branch or tag options to users, improving CI/CD flexibility and demonstrating practical DevOps automation.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
