You may try this, This list dynamic branch names in dropdown w.r.t inputted Git Repo.
Jenkins Plugins required:
- Validating String Parameter
- Active Choices
OPTION 1: Jenkins File:
properties([
[$class: 'JobRestrictionProperty'], parameters([validatingString(defaultValue: 'https://github.com/kubernetes/kubernetes.git', description: 'Input Git Repo (https) Url', failedValidationMessage: 'Invalid Git Url. Retry again', name: 'GIT_REPO', regex: 'https://.*'), [$class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: 'Select Git Branch Name', filterLength: 1, filterable: false, name: 'BRANCH_NAME', randomName: 'choice-parameter-8292706885056518', referencedParameters: 'GIT_REPO', script: [$class: 'GroovyScript', fallbackScript: [classpath: [], sandbox: false, script: 'return[\'Error - Unable to retrive Branch name\']'], script: [classpath: [], sandbox: false, script: ''
'def GIT_REPO_SRC = GIT_REPO.tokenize(\'/\')
GIT_REPO_FULL = GIT_REPO_SRC[-2] + \'/\' + GIT_REPO_SRC[-1]
def GET_LIST = ("git ls-remote --heads [email protected]:${GIT_REPO_FULL}").execute()
GET_LIST.waitFor()
BRANCH_LIST = GET_LIST.in.text.readLines().collect {
it.split()[1].replaceAll("refs/heads/", "").replaceAll("refs/tags/", "").replaceAll("\\\\^\\\\{\\\\}", "")
}
return BRANCH_LIST ''
']]]]), throttleJobProperty(categories: [], limitOneJobWithMatchingParams: false, maxConcurrentPerNode: 0, maxConcurrentTotal: 0, paramsToUseForLimit: '
', throttleEnabled: false, throttleOption: '
project '), [$class: '
JobLocalConfiguration ', changeReasonComment: '
']])
try {
node('master') {
stage('Print Variables') {
echo "Branch Name: ${BRANCH_NAME}"
}
}
catch (e) {
currentBuild.result = "FAILURE"
print e.getMessage();
print e.getStackTrace();
}
OPTION 2: Jenkins UI
Sample Output: