Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store last value of parameter in parameterized job as a default value for next build in Jenkins?

I have been using Jenkins for a few weeks and I have one small problem. I can't find any plugin or solution for storing the last value of a parameter in a parametrized job as a default value for the next build.

For example: My parameter takes build version (1.0.0.01) in the first build. In the next build it will be changed to 1.0.0.02, but I want to have a 1.0.0.01 in the default value field as a hint. Does anybody have a solution or advice?

like image 522
Bob Arctor Avatar asked Jul 11 '13 05:07

Bob Arctor


People also ask

How do parameterized build job receive parameters?

Using build parameters, we can pass any data we want: git branch name, secret credentials, hostnames and ports, and so on. Any Jenkins job or pipeline can be parameterized. All we need to do is check the box on the General settings tab, “This project is parameterized”: Then we click the Add Parameter button.

How do I use this project is parameterized in Jenkins?

Go to Jenkins Home, select New Item, add a name for your Job, for the project type, select Pipeline project and click on Ok. On the configure job page select the This project is parameterized checkbox in the general tab.


1 Answers

You can add a System groovy build step to your job (or maybe a post build Groovy step) using the Jenkins API to directly modify the project setting the default parameter value.

Here is some code that may be useful to get you started:

import hudson.model.*

paramsDef = build.getParent().getProperty(ParametersDefinitionProperty.class)
if (paramsDef) {
  paramsDef.parameterDefinitions.each{ param ->
    if (param.name == 'FOO') {
      println("Changing parameter ${param.name} default value was '${param.defaultValue}' to '${param.defaultValue} BAR'")
      param.defaultValue = "${param.defaultValue} BAR"
    }
  }
}

Have a look at the class ParameterDefinition in the Jenkins model.

You probably need to modify the default param value based on the current build executing. Some code to get that would look like this:

def thisBuildParamValue = build.buildVariableResolver.resolve('FOO')
like image 196
macg33zr Avatar answered Nov 10 '22 00:11

macg33zr