How can I use the Conditional Step Jenkins plugin, to check if an environment variable is set? If it is set, proceed as normal. If it is not set, skip the post-build actions and mark build as a pass (not a failure).
Or if there is a way to do this without using the Conditional Step plugin, I would be open to that as well.
You could do this with the Flexible Publish plugin. Under Conditional action select Not, then under that select Strings match. Put your environment variable as String 1 and leave String 2 empty. Then add your post-build actions below.
Related
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.
I have a Jenkins job in which I have defined 2 Execute Shell Build Steps (call them Step1 and Step2. I also have as part of this job a Choice Parameter with 2 choices (for the sake of this example let's also call those values Step1 and Step2). What I am trying to do is if the user selects Step1 as the choice parameter then the job would only execute the Step1 build step, if user selects Step2 it would only select the Step2 build step. I was thinking that I could put a groovy step in front of these to control this behavior but not sure how to construct this. I could also just define a single choice parameter and condition such that if choice = step1 then execute step1 else execute step2......still not sure how to construct that syntax. I'll keep trying a few different things but if anyone has done this any assistance would be appreciated.
From Parameterized Build:
The parameter are available as environment parameters variables. So e.g. a shell ($FOO, %FOO%) or Ant ( ${env.FOO} ) can access these values.
[Correction by me.]
Use the environment variable defined in your Choice Parameter in two Conditional BuildSteps, one for each of your steps.
How can I make generic parameters in jenkins that will be updated automatically for example I want to be able to create a parameter which hold today's date and it will be update automatically and not manually ?
thanks!
You might want to use EnvInject.
Setup your job and then add a build step "Inject envornment variables" from a file with an absolute path or the path relative to current job's workspace, which will contain something like:
DATE_VARIABLE="20150708"
OTHER_OPTIONAL_VARIABLE="value"
In previous execute shell step you may for example do:
echo "DATE_VARIABLE="`date +"%d%m%Y"` > env_inject.txt
After all that you don't need Parameters for a build, since you will have the needed parameter injected as an environment variable.
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?
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.
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')
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.
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.
Is it possible to define a new build parameter/ENV variable in a build step so it was available in the next one?
Let's say I have 2 different "Execute shell" steps and want in the second step access the variable defined in the first one.
PS: the value for the variable is set in runtime - read from 3rd party resource, so I cannot harcode it, thus need to set it from the shell script.
The plugin EnvInject will do that for you.
It can be configured as pre-SCM step or as build steps. Put it in between your two existing build steps.
Update
In your case, it may be easier to just read the value of the "3rd party" file as part of your second build step:
var=$(<3rdpartyfile.txt)
After the above line, the contents of your 3rdpartyfile.txt will be available in environment variable var. You can now use $var as you would any other variable
You can also use something like
stage('stage-1') {
steps {
script{
env.variable = ${value};
}
}
}
Now you can use the variable env.variable throughout the pipeline