1

I need to use Active Choices Reactive Parameter in my jenkinsfile with below groovy script:

def gettags = ("git ls-remote -t -h https://USER:PASS@bitbucket.org/project_name.git").execute()
return gettags.text.readLines().collect { 
  it.split()[1].replaceAll('refs/heads/', '').replaceAll('refs/tags/', '').replaceAll("\\^\\{\\}", '')
}

How do I define this param in my jenkinsfile?

I have used below code :

properties([ 
    parameters([
        [$class: 'ChoiceParameter', choiceType: 'PT_SINGLE_SELECT', 
                  description: 'Select code branch name', 
                  name: 'code_release_branch', 
                  script: [$class: 'GroovyScript', 
                  fallbackScript: [classpath: [], sandbox: false, 
                  script: 'return ["error"]'], 
                  script: [classpath: [], sandbox: false, 
                  script: '''def gettags=("git ls-remote -t -h ssh://git@abc.com/code/code.git").execute()return gettags.text.readLines().collect{it.split([1].replaceAll('refs/heads/','').replaceAll('refs/tags/','').replaceAll("\\^\\{\\}",'')}}''']]],
    ])
])

This gives "error" output in parameter

Please note that this groovy script is to get branch names from git dynamically

I have tried one more thing of putting this groovy script in Jenkins managed files and give the id as a parameter to script as below , but still same issue:

properties([ 
    parameters([
        [$class: 'ChoiceParameter', 
         choiceType: 'PT_SINGLE_SELECT', 
         description: 'Select code branch name', 
         name: 'code_release_branch', script: [
             $class: 'GroovyScript', 
             fallbackScript: [
                   classpath: [],
                   sandbox: false, 
                   script: 'return ["ERROR"]'
             ], 
             script: [
                   classpath: [], 
                   sandbox: false, 
                   script: '6cd2a674-c02d-44b0-87cb-b32f1fcdc0c3'
             ]
        ]],
    ])
 ])
papanito
  • 2,349
  • 2
  • 32
  • 60
user312307
  • 153
  • 6
  • 21
  • Why not use https://wiki.jenkins.io/display/JENKINS/Git+Parameter+Plugin? – Joao Vitorino Aug 20 '19 at 13:22
  • @Joao . But with this option, branch names will be available only after job is triggered. I want the user to select branch name and then run the job – user312307 Aug 20 '19 at 15:07
  • Nope. Just add the git repo and then the branch name will be available BEFORE you start the build. – Joao Vitorino Aug 20 '19 at 16:23
  • I tried , did not work```properties([ parameters([ gitParameter(branch: '', branchFilter: 'origin/(.*)', defaultValue: 'master', description: '', name: 'BRANCH', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'PT_BRANCH') ]) ]) node { git branch: "${params.BRANCH}", url: 'https://github.com/jenkinsci/git-parameter-plugin.git' }``` – user312307 Aug 20 '19 at 16:27
  • Is there anything that speaks against just using the `input` step (except your requirement "I need to use Active Choices Reactive Parameter")? – StephenKing Oct 07 '19 at 19:58
  • Duplicate of this: https://stackoverflow.com/a/65154953/6330670 – M.S. Arun Dec 05 '20 at 09:48

0 Answers0