Get variables set in build.gradle in Jenkins pipeline build - jenkins

I have a Jenkins pipeline job that builds my project using gradle. As part of the build i would like to use a global variable I have set in the build.gradle file
project.ext.set("MyVar", "My Value")
How can i access this variable in the pipeline build, so
myVar = varSetInGradleBuild
Hope that makes sense
Thanks

Well, usually dependency is reverse, Jenkins sets variables for build to control behavior with -P key.
For your use case you can define variables in gradle.properties file and read it in Jenkins pipeline.

Related

Environment variable in Jenkins Pipeline

Is there any environment variable available for getting the Jenkins Pipeline Title?
I know we can use $JOB_NAME to get title for a freestyle job,
but is there anything that can be used for getting Pipeline name?
You can access the same environment variables from groovy using the same names (e.g. JOB_NAME or env.JOB_NAME).
From the documentation:
Environment variables are accessible from Groovy code as env.VARNAME or simply as VARNAME. You can write to such properties as well (only using the env. prefix):
env.MYTOOL_VERSION = '1.33'
node {
sh '/usr/local/mytool-$MYTOOL_VERSION/bin/start'
}
These definitions will also be available via the REST API during the build or after its completion, and from upstream Pipeline builds using the build step.
For the rest of the documentation, click the "Pipeline Syntax" link from any Pipeline job
To avoid problems of side effects after changing env, especially using multiple nodes, it is better to set a temporary context.
One safe way to alter the environment is:
withEnv(['MYTOOL_HOME=/usr/local/mytool']) {
sh '$MYTOOL_HOME/bin/start'
}
This approach does not poison the env after the command execution.

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?

Jenkins: How to use user defined shell variables when copying artifacts from another project

In my Jenkins configuration I have a conditional step which exports a custom shell variable called "SUFFIX". I want to use this variable in name the of the project from which I am copying artifacts but it says:
Unable to find project for artifact copy: myProject${SUFFIX}_release
How can I use such a variable or achieve such a behaviour where the project name depends on a job parameter. The job parameter is a boolean value and should stay a boolean value. There should not be a string parameter SUFFIX.
Is this question related: Being clever when copying artifacts with Jenkins and multi-configurations
Do I need the EnvInject plugin to make variables accessible by the Copying artifacts plugin?
Jenkins does not retain environment variable changes between builds or build-steps. This is part of the design, to keep the build environment clean.
You cannot export an environment variable in Execute Shell build step, and then use it in Copy Artifacts build step. To get around this, you do need EnvInject plugin.
Instead of exporting your shell environment variable to the Environment, you need to write it to - properties file, in format param=value
Then, using EnvInject build step, load that properties file
After that, your newly loaded environment variable will be available to all subsequent build/post-build steps, including Copy Artifacts build step.

Updating jenkins job variable

Parameterized variable are not getting updated in jenkins
I m using the conditional build-step plugin to update the jenkins job parameter by executing shell script its showing me the new value of variable as well but its not get reflected.
You can try the EnvInject Plugin. One of the features is a build step that allows you to "inject" parameters into the build job from a settings file.
Create a property for the email list in the env.properties file:
echo "variable=`value`"> env.properties
It will create the properties file in the job workspace directory.
env.properties
In shell script:
"$variable"
If I understand correctly, you are trying to change the value of a pre-defined parameter
from within a script that is run by the job.
This will not work, because of "scope" (or "call-stack"),
as a process (your script) cannot change the environment of a parent process (your Jenkins job).

Resources