Jenkins Extended Choice Parameter associated to multiple environments variable - jenkins

I am using in a Test Jenkins configuration the Choice Parameter in order to select the target environment against the tests are running.
Like this:
I use the selected value to pass to the maven test run as selected profile: -Dprofile=${TargetEnv}.
I would like to extend the implementation to make some additional git merge operation before the repository is build and the tests are triggered to run (I don't want to go into detail).
My question is how can I use the "Extended Choice Parameter" Jenkin plugin to have multiple environment variable set when a each choice value.
Example: In case the 'dev' is select then I would like to have two environment variables: targetEnv: qa and lowerEnvBranchName: develop-dev.
Somebody know how is possible to specify this kind of variables?

Related

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.

Key Value store option for Jenkins

Is there any plugin available for Jenkins which provides a key-value store option for Jenkins?
The plugin which's functionality is close to that is the credentials plugin.
The goal is to have a plugin which stores global configuration parameters and this parameters are available to Jenkins jobs.
Go to Manage jenkins -> Configure System -> Global Properties -> Environment Variables:
Check the box and Click on ADD
Enter Key-value and Save
To access the variable simply ${<Your-key>}
Could the enthronement variables fit your need?
They are like regular shell variable.
If you are using the pipeline you can define it this way:
environment {
VAR = 'your_value'
}
and use it later in your build.
This is explained there: https://jenkins.io/doc/pipeline/tour/environment/
If you are writing your pipeline from the UI, you can add a 'source' step in your build step.
source your_environnement_setting
test='Hello'
And then the variables can simply be used like any shell var:
echo $test
If you have variables that you do not know in advance, but you know when you are triggering your job, you can also use the parametrized plugin:
https://wiki.jenkins.io/display/JENKINS/Parameterized+Build

Jenkins Addon in Jenkins Pipeline

I have a parameterized project. With the variable VAR1.
I'm using the the Xray for JIRA Jenkins Plugin for Jenkins. There you can fill four parameters:
JIRA Instance
Issues
Filter
File Path
I'm new to Jenkins but what I have learned so far, that you can't fill this fields with environment variables. Something like
Issues: ${VAR1} - doesn't work.
So I thought I can do this with a pipeline. When I click on Pipeline Syntax and chose step: General Build Step I can choose Xray: Cucumber Features Export Task. Then I fill the fields with my environment variable and click Generate Pipeline Script The output is as follows:
step <object of type com.xpandit.plugins.xrayjenkins.task.XrayExportBuilder>
That doesn't work. What I'm doing wrong?
All you're doing is OK, but what you want is not supported by Jenkins whether it is pipeline or not, since the parameters' load is happening prior to the pipeline-flow or the definition of the ${VAR1}.
You can try to overcome this by defining the 'Issues' value as a pipeline internal value instead of a parameter and base it on the ${VAR1} value.
If it must be a parameter, use 2 jobs where one defines the value of 'Issues' based on a the ${VAR1} and pass it to the other job that gets the 'Issues' as a fixed value.

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

How to create a custom Jenkins "New item" type

I've just started working with Jenkins plugin development and have some questions:
How to create a custom Jenkins "New item" type which should have some predefined build parameters ?
Could you please provide a tutorial or an example how to do such things.
I am thinking of the following solutions:
- develop a custom job type which will have build parameters predefined inside it
- develop a parametrized job custom build parameter type which will get more than one value during the build setep
I have tried to create parametrized job custom build type for my plugin but in my case I need to define more than one build parameter which as I see is not possible because Jenkins's buildWithParameters function gets only one value from my type regarding of this:
https://gitorious.org/jenkinsci/jenkins/source/bbdf5c21153c7e9d60d6ed158d32aa8852f00501:core/src/main/java/hudson/model/ParametersDefinitionProperty.java
Thanks,
Mane H

Resources