Parameterised build in jenkins: Not getting parameters as shell env variables - jenkins

Jenkins version: 2.6, Linux
Problem: The parameterized build variables are not are not visible (as env variables) in the Execution step "shell script", They used to be visible in the older 1.x jenkins version.
Steps:
Create a parameterized build with a multi configuration project.
Add a parameter to the build (using This project is parameterized-> string parameter, {if that matters} ).
Add a build step "Execute shell" to the job.
Access these parameters in this shell script as env variables.
echo "++++++++++++ building $lib_name ($lib_version) ++++++++++++++"
To solve this, I have tried to create a groovy script in "Prepare an environment for the run" section. I created env variables using hardcoded values which are pased to shell script as env vars.
def map = ['lib_name':'lib1']
map['lib_version'] = 'master'
return map
But, without hardcoding, I cannot access these variable values even when using solution from
How to retrieve Jenkins build parameters using the Groovy API?
I dont know what else has to be done. Can some one please suggest?
---> Updating based on the comments on this question:
When I run the following lines in jenkins, I get exception:
def buildVariablesMap = Thread.currentThread().executable.buildVariables
buildVariablesMap.each{ k, v -> println "${k}:${v}" }
FATAL: No such property: executable for class: hudson.model.OneOffExecutor
groovy.lang.MissingPropertyException: No such property: executable for class: hudson.model.OneOffExecutor
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at org.codehaus.groovy.runtime.callsite.GetEffectivePojoPropertySite.getProperty(GetEffectivePojoPropertySite.java:66)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:296)

I have had also similar problem. This is a solution which worked for me. I created a method which always takes and delivers exactly one build parameter which I need.
method:
String readingBuildParameters(VariableResolver varRes, String paramName){
return varRes.resolve(paramName)
}
the line how I use it in a code:
Build currentBuild = Executor.currentExecutor().currentExecutable
VariableResolver varResolver = currentBuild.getBuildVariableResolver()
df_parameter = readingBuildParameters(varResolver, "parameter_name")
BR,
Zoltan

To access the parameters in your shell script:
to evaluate them in echo: e.g. echo "${myParam}"
to use them in code: def myNewvalueParam = ${myOtherParam}

To retrieve build variables from groovy script as a build step:
def buildVariablesMap = Thread.currentThread().executable.buildVariables
println buildVariablesMap['BUILD_NUMBER']
But please note that for your custom/altered environment variables to be visible for the next steps you should use EnvInject plugin, with it you can define a step that will export new env variables as key-value pair just like properties file.

This was a bug in jenkins, probably in the credentials plugin:
https://issues.jenkins-ci.org/browse/JENKINS-35921
Thanks for all your help!

Related

How to invoke Inject environment variables to the build process plugin in jenkinsFIle jenkins 2.x with pipeline

I'm trying to migrate my project from jenkins 1 to jenkins 2.x using pipeline as code or Jenkinsfile.
But I don't see any option in snippet generator to generate environment injector plugin into a script in Jenkinsfile.
Anyone can help?
I'm assuming that you want to read properties from a specific file and inject them as environment variables?
If so, this is a solution:
Create the file that will contain the environment properties
You create some properties file called project.properties with following content:
PROJECT_VERSION='1.4.34'
Then, on your pipeline code, you've to add the following code in order to be able to read the file and inject read variables as environment variables:
node {
load "${WORKSPACE}\\project.properties" // assuming that props file is in
Jenkins Job's workspace
echo "PROJECT VERSION: ${PROJECT_VERSION}"
}
First line read and inject variable PROJECT_VERSION as environment variable
Second line is just to print read variable to make sure that everything worked seamlessly
Result:
Wanted to just comment on your question, but my lack of reputation is hindering me.
The list of supported steps is here: https://jenkins.io/doc/pipeline/steps/
In general, you can use other plugins by using their Java style invocation.
i.e.
step([$class: 'classname', parametername: 'value'])
I used this example for read a properties file and use it in a pipeline stage:
node(){
file = readFile('params.txt')
prop=[:]
file.eachLine{ line ->
l=line.split("=")
prop[l[0]]=l[1]
}
withEnv(['MyVar1='+prop["MyVar1"],'MyVar2='+prop["MyVar2"],'MyVar3='+prop["MyVar3]]){
stage('RUN_TEST'){
echo env.MyVar1
echo env.MyVar2
echo env.MyVar3
sh"echo $MyVar1"
}
}
}

Jenkins - How to read the environment variables in groovy post build step

I am trying to read the environment variables in Groovy Postbuild step. I am able to read the values of parameters passed to builds but unable to read the values of parameters which are set in one of my Execute Windows batch command.
In one example of my Execute Windows batch command I do this:
SET custom_param=myValue
if I use ${custom_param} in other jenkins steps/jobs, it gets my value. So I am sure it has the value. I just can't get it in groovy script
I have tried followings to do so, none of them have worked:
manager.envVars['custom_param']
build.buildVariableResolver.resolve('custom_param')
build.getEnvironment(listener).get('custom_param')
Any help here would be great
(Assuming you're not running your script in groovy sandbox)
Try the bellow:
build = Thread.currentThread().executable
String jobName = build.project.getName()
job = Hudson.instance.getJob(jobName)
my_env_var = job.getLastBuild().getEnvironment()["YOUR_ENV_VAR"]
Groovy Post build step run as separate process. It has access to environment as normal JVM process.
You could use EnvInject plugin as a a build step. Subsequent steps in build will able to read this via normal environment access (System.env in your groovy script)
When you set some custom variables in your "Windows command batch" step, these variables are available only during this Jenkins step.
Once Jenkins move on the next step, your variables are lost...
If you want to set some variables permanently, you can try to use the SETX command:
What is the difference between setx and set in environment variables in Windows?

Passing variable from shell script to jenkins

I trigger a shell script from Jenkins, This scripts get date and export it as a environment(Linux) variable $DATE. I need to use this $DATE inside same Jenkins job. I made job as parameter build. Created a string parameter as DATE value as DATE=$DATE. But it is not working.
Please suggest !!
You mention that you are exporting a DATE environment variable in an shell script, which is presumably being started via an "Execute shell" step.
The problem is, once the shell step has completed, that environment is gone — the variables will not be carried over to subsequent build steps.
So when you later try to use the $DATE value — whether in another build step, or as a parameter to another job — that particular environment variable will no longer exist.
What you can do instead is use the EnvInject plugin to export environment variables during a build. Variables set up using this plugin will be available to all subsequent build steps.
For example, you could write the DATE to a properties field in one build step:
echo DATE=$(date +%Y-%m-%d) > env.properties
Then you can add an "Inject environment variables for your job" build step, and enter env.properties in the "Environment Properties File Path" field.
That way, the DATE variable (and anything else in that properties file) will be exported and will be visible to the rest of the build steps.
You could use an assignment statement and sh's returnStdout to get the value in Jenkins without having to write to a properties file.
foo = sh(
returnStdout: true,
script: 'date'
)
Then later on in the Jenkinsfile you can use $foo like any other variable.
EDIT: This is for a pipeline job, not a freestyle job.
I had the same issue.
The solution that worked for me was:
env.ABC=bat(returnStdout: true,
script: ''' #echo off echo abc ''').trim()
The .trim() and #echo off is important if you want to reuse the variable in another batch script.

How to set environment variables in Jenkins?

I would like to be able to do something like:
AOEU=$(echo aoeu)
and have Jenkins set AOEU=aoeu.
The Environment Variables section in Jenkins doesn't do that. Instead, it sets AOEU='$(echo aoeu)'.
How can I get Jenkins to evaluate a shell command and assign the output to an environment variable?
Eventually, I want to be able to assign the executor of a job to an environment variable that can be passed into or used by other scripts.
This can be done via EnvInject plugin in the following way:
Create an "Execute shell" build step that runs:
echo AOEU=$(echo aoeu) > propsfile
Create an Inject environment variables build step and set "Properties File Path" to propsfile.
Note: This plugin is (mostly) not compatible with the Pipeline plugin.
The simplest way
You can use EnvInject plugin to injects environment variables at build startup. For example:
How you know it's working
In my case, I needed to add the JMETER_HOME environment variable to be available via my Ant build scripts across all projects on my Jenkins server (Linux), in a way that would not interfere with my local build environment (Windows and Mac) in the build.xml script. Setting the environment variable via Manage Jenkins - Configure System - Global properties was the easiest and least intrusive way to accomplish this. No plug-ins are necessary.
The environment variable is then available in Ant via:
<property environment="env" />
<property name="jmeter.home" value="${env.JMETER_HOME}" />
This can be verified to works by adding:
<echo message="JMeter Home: ${jmeter.home}"/>
Which produces:
JMeter Home: ~/.jmeter
In my case, I had configure environment variables using the following option and it worked-
Manage Jenkins -> Configure System -> Global Properties -> Environment Variables -> Add
You can try something like this
stages {
stage('Build') {
environment {
AOEU= sh (returnStdout: true, script: 'echo aoeu').trim()
}
steps {
sh 'env'
sh 'echo $AOEU'
}
}
}
You can use Environment Injector Plugin to set environment variables in Jenkins at job and node levels. These are the steps to set them at job level:
From the Jenkins web interface, go to Manage Jenkins > Manage Plugins and install the plugin.
Go to your job Configure screen
Find Add build step in Build section and select Inject environment variables
Set the desired environment variable as VARIABLE_NAME=VALUE pattern. In my case, I changed value of USERPROFILE variable
If you need to define a new environment variable depending on some conditions (e.g. job parameters), then you can refer to this answer.
EnvInject Plugin aka (Environment Injector Plugin) gives you several options to set environment variables from Jenkins configuration.
By selecting Inject environment variables to the build process you will get:
Properties File Path
Properties Content
Script File Path
Script Content
and finally Evaluated Groovy script.
Evaluated Groovy script gives you possibility to set environment variable based on result of executed command:
with execute method:
return [HOSTNAME_SHELL: 'hostname'.execute().text,
DATE_SHELL: 'date'.execute().text,
ECHO_SHELL: 'echo hello world!'.execute().text
]
or with explicit Groovy code:
return [HOSTNAME_GROOVY: java.net.InetAddress.getLocalHost().getHostName(),
DATE_GROOVY: new Date()
]
(More details about each method could be found in build-in help (?))
Unfortunately you can't do the same from Script Content as it states:
Execute a script file aimed at setting an environment such as creating
folders, copying files, and so on. Give the script file content. You
can use the above properties variables. However, adding or overriding
environment variables in the script doesn't have any impacts in the
build job.
There is Build Env Propagator Plugin which lets you add new build environment variables, e.g.
Any successive Propagate build environment variables step will override previously defined environment variable values.
Normally you can configure Environment variables in Global properties in Configure System.
However for dynamic variables with shell substitution, you may want to create a script file in Jenkins HOME dir and execute it during the build. The SSH access is required. For example.
Log-in as Jenkins: sudo su - jenkins or sudo su - jenkins -s /bin/bash
Create a shell script, e.g.:
echo 'export VM_NAME="$JOB_NAME"' > ~/load_env.sh
echo "export AOEU=$(echo aoeu)" >> ~/load_env.sh
chmod 750 ~/load_env.sh
In Jenkins Build (Execute shell), invoke the script and its variables before anything else, e.g.
source ~/load_env.sh
This is the snippet to store environment variable and access it.
node {
withEnv(["ENABLE_TESTS=true", "DISABLE_SQL=false"]) {
stage('Select Jenkinsfile') {
echo "Enable test?: ${env.DEVOPS_SKIP_TESTS}
customStep script: this
}
}
}
Note: The value of environment variable is coming as a String. If you want to use it as a boolean then you have to parse it using Boolean.parse(env.DISABLE_SQL).
extending the answer of #JSixface:
To define environment variables globally for access from within all the stages of a declarative pipeline, you can add the environment section within the pipeline block.
pipeline {
agent {
node {
label 'myAgent'
}
}
environment {
AOEU = "${sh(returnStdout: true, script: 'echo aoeu').trim()}"
}
stages {
...
}
}
Try Environment Script Plugin (GitHub) which is very similar to EnvInject. It allows you to run a script before the build (after SCM checkout) that generates environment variables for it. E.g.
and in your script, you can print e.g. FOO=bar to the standard output to set that variable.
Example to append to an existing PATH-style variable:
echo PATH+unique_identifier=/usr/local/bin
So you're free to do whatever you need in the script - either cat a file, or run a script in some other language from your project's source tree, etc.
For some reason sudo su - jenkins does not log me to jenkins user, I ended up using different approach.
I was successful setting the global env variables using using jenkins config.xml at /var/lib/jenkins/config.xml (installed in Linux/ RHEL) - without using external plugins.
I simply had to stop jenkins add then add globalNodeProperties, and then restart.
Example, I'm defining variables APPLICATION_ENVIRONMENT and SPRING_PROFILES_ACTIVE to continious_integration below,
<?xml version='1.0' encoding='UTF-8'?>
<hudson>
<globalNodeProperties>
<hudson.slaves.EnvironmentVariablesNodeProperty>
<envVars serialization="custom">
<unserializable-parents/>
<tree-map>
<default>
<comparator class="hudson.util.CaseInsensitiveComparator"/>
</default>
<int>2</int>
<string>APPLICATION_ENVIRONMENT</string>
<string>continious_integration</string>
<string>SPRING_PROFILES_ACTIVE</string>
<string>continious_integration</string>
</tree-map>
</envVars>
</hudson.slaves.EnvironmentVariablesNodeProperty>
</globalNodeProperties>
</hudson>
You can use either of the following ways listed below:
Use Env Inject Plugin for creating environment variables. Follow this for usage and more details https://github.com/jenkinsci/envinject-plugin
Navigate below and can add
Manage Jenkins -> Configure System -> Global Properties -> Environment Variables -> Add
Scripted Pipeline syntax that we use is this:
env.AEOU = sh label:'set env var',
returnStdout: true,
script : '''#!/bin/bash
echo "aeou"
'''
sh label:'checkit',
script : '''#!/bin/bash
echo "${AEOU}"
'''
Note the use of triple-single-quote notation for the script parameter to the sh step. This ensures that the ${AEOU} does not get interpolated by Groovy and does get interpolated by the bash shell.
We use groovy job file:
description('')
steps {
environmentVariables {
envs(PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true)
}
}

How to get the jobname from jenkins

Is there a way to get the jobname for the current build in jenkins and pass it as a parameter to an ant build script?
Jenkins sets some environment variables such as JOB_NAME (see here) for more details on the variables set.
You can then access these in ant via ${env.JOB_NAME}.
Edit: There's also a little howto for environment variables on the same page here.
A similar issue, I was looking for job name for shell script.
In the 'Execute shell' > 'Command' textbox,
both the below worked for me:
echo $JOB_NAME
echo "$JOB_NAME"
You may set special variable for that based on global variable. Simple:
THEJOB="${JOB_NAME.substring(JOB_NAME.lastIndexOf('/') + 1, JOB_NAME.length())}"
Now $THEJOB is your job name
If you can run any Job, you can easily go to the Build section of that job and go to environment variables and see all the information there.
Nowadays there is an environment variable JOB_BASE_NAME which contains the last component of JOB_NAME.
For example: if JOB_NAME contains Cool_Jobs/Very_Cool_Jobs/The_Coolest then JOB_BASE_NAME will just contain The_Coolest

Resources