Generating a dynamic parameter in Jenkins based off a parameter from the same job - jenkins

I've got a Jenkins build with a choice box for build prefixes by release. It helps trigger a job based off the value of whatever specific build the person wanted.
I wanted to take the value of that choice box and transform the variable into the correct prefix based off the naming conventions typically used on this server for triggering the job based off its name.
So let's say I've got build prefix choices specifically for,
ReleaseOne
ReleaseTwo
none
For none, meaning the parameters used won't try to access or set any specific release-based info by triggering the non-release-specified build.
I wanted to take the value of Release_Prefix and transform it, if needed, for the job that I trigger later. I was hoping to accomplish this with a dynamic parameter or similar mechanism. I'm not sure if my script is bugged, or something fundamental is not working to my intent. This might be the case, based off some alluded feedback from a similar question.
Can I do something like this snippet below? If not with Dynamic Parameter plugin + GroovyScript, what would you suggest? This currently seems to return nothing, regardless of my choice.
Formatted_Prefix parameter, Dynamic Parameter
switch(binding.getVariables().get("Release_Prefix"))
{
case "none":
return "";
case "ReleaseOne":
return "ReleaseOne_";
case "ReleaseTwo":
return "ReleaseTwo_";
default:
def prefix = binding.getVariables().get("Release_Prefix")
return "$prefix_";
}
There's multiple ways I can overcome this, but if I can do it at the initial parameter stage, that would be best for me.

You can use EnvInject Plugin for this.
check the checkbox Prepare an environment for the run and
write your script inside Evaluated Groovy script text box
def prefix1 = Release_Prefix + "mydata"
return[prefix:prefix1]

Related

Skip Jenkins Stage if its last excution was successfull

I am looking for way to speed up our Build-Pipeline.
The biggest impact would be to do certain things only if they have not been done.
So basically, I have already parameterized some optional stages which works fine, but there are some things I'd like to skip if there was a execution before which was successfull.
I have searched the docs, especially the section ref. when but the was nothing that did the job.
So the question is: Is there something I can do in a declarative pipeline to always skip a stage, except when
It has never been run successfully before
The last time it ran was not successful (eg. if I allow for its execution to be forced by passing a parameter)
I though about using the Build Number (eg only run it on the first execution of the pipeline), but this does't cut it for 2 reasons
I'm using milestones to prevent multiple executions of the pipe for a given branch/PR
If the first run fails, it would never be tried again
Oh, and I also thought about putting all of the logic in post -> regression and forcing the stage to fail on the first pipeline run, but this doesn't seem to be a good idea either.
One option ,although not ideal for large scale, can be to store the diffrent states as Global Environment Variables. (Manage Jenkins -> Configure System -> Global properties -> Environment variables).
These parameters are available for all jobs and can store parameters in a 'server wide' scope.
You can then update or set them via code using the following groovy method:
#NonCPS
def updateGlobalEnvVariable(String name, String value) {
def globalNodeProperties = Jenkins.getInstance().getGlobalNodeProperties()
def envVarsNodePropertyList = globalNodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)
if (envVarsNodePropertyList == null || envVarsNodePropertyList.size() == 0) {
def envVarsNodePropertyClass = this.class.classLoader.loadClass('hudson.slaves.EnvironmentVariablesNodeProperty')
globalNodeProperties.add(envVarsNodePropertyClass.newInstance())
}
envVarsNodePropertyList.get(0).getEnvVars().put(name, value)
}
This function will create the parameter if it does not exists or update its value in case it already exists. Also this function should better be placed in a Shared Library from which it will be available for all pipelines.
A nice advantage for this technique is that you can always 'reset' the different stages from the configuration page, However if you need to store multiple stages it can overflow the configuration page with a bit too much information.
Maybe you could use custom shared-workspace and then create/store some state file or something that could be used by your next executions like lastexecfailed.state and then try to locate the file on the shared workspace at the beginning of your execution.

How can I override a jenkinsfile's default parameters?

Sometimes, we want to create multiple jobs that use the same Jenkinsfile instead of a single one. This could happen for example because we want to maintain logs divided based on parameters, instead of having a single job on which look for the right log.
However, in this case, we can't use the parameter definition in the Jenkinsfile, because whatever default value we would define on the job instance would be overwritten by the following execution with whatever is defined in the Jenkinsfile (and this is also happening if we don't define a default value).
So, in this situation, the only way we figure out is to remove the parameter definition in the Jenkinsfile and define the parameters directly on the jobs, which is kind of not optimal.
I mean, I agree that this is the right behavior in most of the cases, as you don't want your parameter to be out of synch and not versioned, but is there a way to specify to Jenkins to skip the parameter reconfiguration or to override the default parameter written in the Jenkinsfile? Something that can be activated/deactivated job by job.
Had this problem myself, we solved it like this:
string(name: 'parameterName', defaultValue: params.parameterName ?:'your default value')
Now the default values defined through Jenkins job configuration will not be overridden.

Parameterized Jenkins job with dependent parameter

I am trying to create a Jenkins job that has dependent parameters.
Firstly I want to be able to choose a main parameter:
And then secondly to be able to choose from a set of options that are dependent parameters of the main parameter.
If I select a different main parameter:
I then want to have a different set of options as the dependencies to the second main parameter.
Please, can you help me with how I can achieve this?
I would suggest the Active Choices plugin (also known as "uno-choice"). (This question has references to both, though they're not the accepted answer.)
For your specific use case, you'll want to add two parameters to your job:
Active Choices Parameter
Name: MainOption
Script: Groovy Script
return ['A','B']
Active Choices Reactive Parameter
Name: DependentOption
Script: Groovy Script
def choices
switch(MainOption){
case 'A':
choices = ['Blue','Green','Yellow']
break
case 'B':
choices = ['Black','White','Grey']
break
default:
choices = ['N/A']
break
}
return choices
Fallback Script: Groovy Script
return ['Option error']
Referenced parameters:
MainOption
The "Referenced parameters" setting is the key - when that value is changed, the plugin will re-evaluate the Groovy script, giving you the dependent parameter effect.
For all of you who stumble upon the same kind of problem (like I did). There is a fairly new Jenkins plugin available that does exactly what is desired here: display a dependent set of drop-downs, updating dependent boxes when a main box changes selection.
For your job you just need to follow the following steps:
Install "Multiselect Parameter Plugin"
The "multiselect parameter plugin" can be installed from Jenkins plugin management, the documentation is available on its Jenkins Plugin page.
Add new parameter
Use "Multiselect Parameter" type
Set name to a sensible value
give a short description
configure like shown below:
H,Main option,Dependent option
V,SELECTED_MAIN,SELECTED_DEPENDENT
C,A,Blue
C,A,Green
C,A,Yellow
C,B,Black
C,B,White
C,B,Grey
Use selected values
When you run "build with parameters" in your job, the following boxes are displayed for selection:
In your build script you can simply use the configured environment variables SELECTED_MAIN and SELECTED_DEPENDENT, which contain the selected values from both select boxes.

Are there any jenkins plugins for choice parameters that amend themselves after a new entry?

Are there any jenkins plugins for choice parameters that amend themselves after a new entry if the build succeeds?
Basically, something like this:
-Choice Parameter-
Name: All_Choices_So_Far
Choices:
First_Default_Choice
${New_Choice}
So when someone picks ${New_Choice} and fills out the New_Choice field, whatever goes there then does the following, conceptually, not syntactically, as I don't know what the actual syntax would be:
Build Succeeded? If so then,
All_Choices_So_Far.Choices.add(${New_Choice})
All_Choices_So_Far.Choices = All_Choices_So_Far.Choices.uniq
Apply & Save Config
I don't actually care about the workflow being that specific, but that's roughly the functionality that I'm looking for.
Although I've yet to make a Jenkins plugin myself, if this doesn't already exist, I would be interested in how to pursue it.

Parameterized job always run with default parameter

I am having troubles to force Jenkins job to always run with default parameter. Does anyone know the possible plugin to help with that case? Right now I am using extended parameter choice, but still there is no option to just run the job with default value without asking user for parameter.
Solution 1
Currently there is not a straight forward solution to run a parameterized job with default parameter using a plugin. However there is a workaround to accomplish that using the EnvInject Plugin.
As #General_Code noted:
Just add the build step, set the variable like: var1=value and then
use it using ${var1}
Solution 2
As #RejeeshChandran noted:
a more robust solution is the Parameterized Build Plugin which provides the functionality of defaults values for the parameters.
Note
Note that Parameter Defaults Options is a plugin under development which will solve exactly this request. When it is released, you will be able to set it up so your parameter will get a default value when you run it manually.
you can use this plugin Parameterized Scheduler
allow you to write a cron expression with the parameters inside like this
H(0-29)/10 * * * * % name=value; othername=othervalue
Documentations
Use This build is parameterized option in Jenkins configuration. Here you can add default values to the parameters. It will run with default value if the user doesn't change it. It is good to have configurable parameter before running the job.
For configuration details see https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Build. You can have multiple parameters.
Go to your jenkins job -> Configure -> General Tab
Add all options in "Description" but the default option in "Default Value"
enter image description here

Resources