Define parameters in Jenkins Auxiliary template - jenkins

I have a pipeline template that has an attribute called "testJobs". The idea is to allow the implementing job to perform its build and then trigger a list of downstream jobs to perform validation.
My biggest problem with this is I cannot figure out how to pass along parameters from the calling pipeline.
For example, my pipeline has a build parameter which defines the git branch to pull. When iterating through the post-build "testJobs" I want to pass that branch along as a parameter, if that is a parameter on the downstream job.
Is there a way to define the parameters using variables within the auxiliary template?

Related

Parameterized Jenkins Jobs Conflict with WebHooks

I have a parameterized Jenkins job that has 1 parameter that accepts webooks to kick off builds.
I use the parameter in the branch specifier and it works except for one use case.
The parameter is called a, the branch specifier in the job configuration is defined as refs/${a} the default value for a is set to heads/master
this all works, but as soon as I kick off the job manually (passing in the default parameter value), WebHooks no longer kick off the job after I kick off the job manually.
Any ideas?

Jenkins Job DSL Plugin: How to Modify Parameters on other jobs

I want to create a job in Jenkins which modifies an existing parameter on another job.
I'm using the Job DSL Plugin. The code I'm using is:
job('jobname'){
using('jobname')
parameters {
choiceParam('PARAMETER1',['newValue1', 'newValue2'],'')
}
}
However, this only adds another parameter with the same name in the other job.
I'm trying the alternative to delete all parameters and start from scratch, but I haven't found the way to do that using Job DSL (not even with the Configure block).
Another alternative would be to define the other job completely and start from scratch, but that would make the job too complicated, specially if I want to apply this change to many jobs at a time.
¿Is there a way to edit or delete lines on the config.xml file using the Job DSL plugin?

Trigger Jenkins Job for every parameter

I have created a Global choice Parameter using Extensible Choice Parameter plugin.
I am using this parameter list in one of my parametrized jenkins job.
Is there a way in jenkins, where I can execute the job with each of the parameters in the Global choice Parameter list?
I have had a look on Build Flow job in jenkins, as suggested in this answer, but it seems it accepts hardcoded parameters only, and not dynamic.
I finally managed to resolve this using the following steps (with great help from this post) -
As my parameters list is dynamic in nature, it could be added or modified according to other jobs, we have managed it in a text file.
Next, We have used Extensible Choice Parameter plugin to display the parameters, using the groovy script -
def list = [];
File file = new File("D:/JenkinJob/parameterList.txt")
file.eachLine { line ->
list.add("$line")
}
return list
Now I want to call this jenkins job for each of the parameter.
For this, I have installed, BuildFlow plugin, and crated a new jenkins job of BuildFlow type -
Next, get the Extended Choice Parameter plugin, and configure it as follows -
Now in the flow step of this job, write this script, where "Feature" is the parameter, that is just created above, and within call to "build" parameter, pass in the name of job which we want to call for each parameter -
def features = params['Features'].split(',')
for (feature in features ) {
build("JobYouWantToCall", JobParameter: feature,)
}

Creating a Generic Jenkins Deployment Template Pipeline

I would like to define a deployment (CD) template that is capable of wrapping build (CI) jobs of various types. I would like the CD template to be completely ignorant of how the downstream CI job is built.
So far my best solution is to define an auxiliary template which is an attribute of the CD template. This auxiliary template includes a reference to the CI job (via the 'select item' type) and a text attribute 'branch'. I found that for the most part this is enough info to trigger the CI job, but is there a better way to do this?
Ideally I would like to be able to define the CI job's parameters directly in the CD job's configuration without needing to have the auxiliary template middle man.
EDIT:
Here's an example of how I'm calling a downstream build job in a different template:
parameterList = []
parameterList.add(new hudson.model.StringParameterValue('stashRepo', stash))
parameterList.add(new hudson.model.StringParameterValue('branch', branch))
parameterList.add(new hudson.model.StringParameterValue('configDir', configDir))
parameterList.add(new hudson.model.StringParameterValue('jdkVersion', jdkVersion))
build job: configBuildJob.buildJob.getFullName(), parameters: parameterList
The problem with this is the template makes the assumption that these build jobs have these exact parameters. It would be nice to be able to somehow set them in the templates configuration instead, so that I don't have to make assumptions about the downstream jobs in my code.

Get Active Choices Parameter value from upstream job

There is a job parametrized with Active Choices Parameters using Active Choices Plugin
I want to trigger this job from the upstream job.
The upstream job should use the default parameters of the downstream job.
The parameter UtilityPath depends on UtilityVersion to evaluate itself and to form the list of choices.
How can I
Get the list of choices returned by the groovy script of UtilityVersion from the upstream job?
Supply my choice for UtilityVersion to the parameter UtilityPath, so it could generate it's own list of choices for me (again, on the upstream job).
Trigger the job with my choices for parameters UtilityVersion and UtilityPath?
Whatever your downstream job's parameter has (in the groovy script/ code section), if you can put that in a SCRIPTLER script (see Jenkins Scriptler plugin) then you can call that scriptler script and pass the same parameters (that you were passing in the downstream job) in your upstream job's BUILD section (either execute shell or Run Groovy script) as you mentioned, you don't want to add the same downstream parameters in your upstream job due to complexities). NOTE: See conditional run plugin on how to call Scriptler script (in Build section) if you don't want to call the Scriptler script if you are dealing with TFS vs ProjectC vs someAutomationD or when parameterX is set to true (your call there).
It's pretty much same what CSchulz mentioned but Scriptler script is better as you change the code/script in one place (Scriptler Script section - left hand side section on Jenkins home page) and then use/reuse that script anywhere (i.e. either in parameters which support Groovy Scriptler script --or-- in the build section) without requiring to read a downstream job's parameter values (some hacky way before even the downstream is called, time changes everything sometimes) --OR doing something crazy with Jenkins API to make it more complex.
As I have tried, you cannot trigger upstream/downstream jobs with "Active Choice Plugin". Active choice and Reactive parameters get fired only if you trigger the job manually. For instance, if you tried to trigger the build from a bitbucket, active choice parameter get the value but reactive value will be shown as empty.
But you can achieve this in different ways.
If you are triggering the first job manually (by yourself), set the downstram job parameters as string so you can read those values directly.
Second option is to use environmental variable. Active choice is more over a conditional choice parameter. you can write groovy script to set parameters as environmental variable.This can be achieved with EnvInject Plugin. Write your conditional script in groovy and parameters are available in each and every build steps.
use environment variables to pass parameters to downstream job

Resources