Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameter List in Jenkins to display list of build numbers from another build

I have two Jenkins builds, one for compiling and one for deploying.

The developer wants to be able to choose a build from the compiler build when running the deploy build, not always run the most recent build.

What I am after is a method of populating a choice parameter for the deploy build with a list of successful\unstable builds from the compile build.

I will then use the the option listed in the parameter to deploy that artifact.

like image 699
Shaun Avatar asked Oct 02 '13 10:10

Shaun


4 Answers

Using the Dynamic Parameters plugin

In your promote job:

  • [x] This build is parameterized
  • Add Parameter
  • Dynamic Choice Parameter
  • Set Name to whatever
  • Paste below into Choices Script
import jenkins.model.Jenkins
import hudson.model.AbstractProject
import hudson.model.Result
import hudson.util.RunList

AbstractProject<?, ?> otherJob = Jenkins.getInstance().getItemByFullName("otherJobName", AbstractProject.class)
RunList<?> builds = otherJob.getBuilds().overThresholdOnly(Result.SUCCESS)

def list = builds.limit(5).collect { it.number }

Screenshot from wiki page:

Screenshot

like image 94
slow Avatar answered Jan 03 '23 19:01

slow


As Dynamic Parameters plugin is no more accessible. You can use Active Choice parameter plugin in Jenkins. Now you can list all successful Jenkins builds as parameterized option in Jenkins Job/pipeline

Follow below steps to access the list of successful jobs [as dropdown list]

  1. In job configuration [General section] select this project is parameterized

  2. Select add parameter as "Active choice parameter"

  3. Give name for parameter

  4. Select groovy script and paste below code in groovy script text box

    return jenkins.model.Jenkins.instance.getJob('<Jenkins-job>').builds.findAll{ it.result == hudson.model.Result.SUCCESS }.collect{ "$it.number" }

  5. It worked awesome without without powershell and BASH

  6. No need to process Jenkins API and filter JSON output

like image 26
OMKAR CHAVAN Avatar answered Jan 03 '23 21:01

OMKAR CHAVAN


One option is to use the Promoted Builds plugin to mark a specific build to be deployed. This moves the choice from the deployment build into the compilation build. Select the Promote builds when... option in the compilation build and set up how you want promotion to work. The developer could choose (or automate) the build to promote. In the deployment build, the Copy Artifact plugin can grab the appropriate build (based on a permalink to the latest promoted build).

like image 32
Dave Bacher Avatar answered Jan 03 '23 21:01

Dave Bacher


As far as I know, it is not possible to populate the choice parameter. However, you don't need to always use the newest build. I assume that you use the copy artifact plugin. This plugin provides the "Build selector for Copy Artifact" parameter. You still need to enter the build number manually, but when deploying you have all the standard choices, like "Latest successful build", but also "Specific Build". You need to enter the number and don't have a drop down, but I got my deployers trained well enough to enter the build number.

like image 38
Peter Schuetze Avatar answered Jan 03 '23 19:01

Peter Schuetze