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

家住魔仙堡 提交于 2019-12-22 03:22:16

问题


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?


回答1:


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')



回答2:


The Persistent Parameter Plugin is exactly what you are looking for!

You just need to download it from the official Jenkins repository and install it, no need for any additional setup.

Then on your job, you just need to add a "Persistent Parameter" in order to have default values used and saved between builds.




回答3:


The Extended Choice Parameter plugin provides this capability by using default parameter values from a properties file. A default parameter can be selected from a specified property key and this key can be programmatically modified in your current build. I would then use a groovy script in the current build to set the value of the default property key for the next build.

As an example you would have an Extended Choice Parameter whose default value is defined by a properties file version.properties with keys as follows:

versions=1.0.0.02, 1.0.0.01, 1.0.0.00
default.version=1.0.0.02

The parameter definition would include:

Property File=version.properties    
Property Key=versions
Default Property File=version.properties
Default Property Key=default.versions

The GUI for your parameter in the next build would show a selection list with 1.0.0.02 selected by default. This feature is also very useful for pipeline builds where you would want the parameters of a downstream build stage to be set by an earlier build.

The only drawback to this approach might be that the parameter UI will be a drop-down selection. You may opt to have a single value in the versions property key so not to confuse your users.




回答4:


Similar to thiagolr's answer, but for those of you using pipelines! It appears the persistent-parameter-plugin doesn't work for those using pipeline 2.0. But there is a patched version at https://github.com/ashu16815/persistent-parameter-plugin which seems to work for me.

Clone it locally: git clone https://github.com/ashu16815/persistent-parameter-plugin.git

Build it: mvn clean install

Install it in Jenkins: 1) Navigate to Jenkins > Manage Jenkins > Manage Plugins 2) Click Advanced tab 3) Scroll down to Upload Plugin 4) Click Choose file and select the persistent-parameter.hpi in the target directory of the maven build above

Now it should persist.



来源:https://stackoverflow.com/questions/17585615/how-to-store-last-value-of-parameter-in-parameterized-job-as-a-default-value-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!