Skip to content

Jenkins Contextual List of Values

doronshai edited this page Jul 20, 2015 · 2 revisions

Think about a case where you want to have a build that will use other build results. Now think that there are 10 jobs, with almost same name but the prefix which is the name of the branch that this job is handling Let’s say now that you want to give to the user the option to choose what is the branch and then you will expect from the build number paramerter to list the relevant build numbers and even only those that were successful…

Here is how:

  1. download this plugin from https://github.com/biouno/uno-choice-plugin
  2. delete the phantomJS part from the pom.xml so the build will not fail on tests...
  3. execute mvn package -s/opt/maven/conf/settings-jenkins.xml -Damven.test.skip=true -DskipTests=true
  4. upload the plugin to your Jenkins

Wiki about this plugin is here

Now on any job that you want to use this do as follows:

  1. Add a Choice Parameter (as you see in the attached image) this parameter have to be BEFORE the next one. The name of it is important. in the attached example it is BRANCH
  2. Add a Uno-Choice Cascade Dynamic Choice Parameter and give it a name
  3. Copy the following text into it and change the suffix of the job name to match your needs... (see attached example)
    import jenkins.model.Jenkins
    import hudson.model.AbstractProject
    import hudson.model.Result
    import hudson.util.RunList
    branch=binding.variables.get('BRANCH')
    AbstractProject<?, ?> otherJob = Jenkins.getInstance().getItemByFullName(branch + "-SUFFIX_OF_THE_JOBS", AbstractProject.class)
    RunList<?> builds = otherJob.getBuilds().overThresholdOnly(Result.SUCCESS)
    def list = builds.limit(10).collect { it.number }
  4. For the Referenced parameters put the name of the parameter that you created before (i.e. BRANCH) That is it, now it will work :)