I'm looking for a plugin/approach that lets me set and read a persistent global variable for use between jobs.
The scenario is that I have CI job that runs tests on various branches of the codebase and I want to associate a build number that corresponds to the last stable build of the release branch. i.e.
Build No Branch Result GolbalSharedThingVal
5 release Success 1.5
6 dev Fail 1.5
7 dev Success 1.7
8 release Unstable 1.7
9 release Success 1.9
10 release Fail 1.9
Then in my deployment job I want to annotate the build with the version using a groovy post build action:
manager.addShortText(" ${manager.build.env.get('GolbalSharedThingVal')}")
Does anyone have any advice about what GolbalSharedThingVal could be?
Many Thanks,
Vackar
EnvInject plugin is the plugin for anything related to environment variables.
Don't know about setting a persistent global variable (that goes against the design principles of Jenkins), but you could have the job export a value to a properties file, and other jobs read the value from the properties file at initialization and expose it as environment variable to other build steps.
Of course the property file would have to be centrally located on Jenkins master somewhere
If you make sure that the different jobs/stages run on the same agent, you can use a file as a persistent storage. In a declarative pipeline this could look like
stage('myStage') {
steps {
echo 'in myStage'
sh '''
# get value from file into environment variable
export PERSISTENTVAR=`cat $HOME/persistent_var_file`
# do some stuff here to e.g. change $PERSISTENTVAR
# save env variable into file for later use
echo $PERSISTENTVAR > $HOME/persistent_var_file
'''
}
}
Related
I have a batch Job (not the pipeline Job) in Jenkins where I am using a plugin called "Naginator" which will check if the current build is failed/unstable - If the current build is failed/unstable it will run immediately the next build to confirm the error is genuine. If the error is not genuine then the run is successful and it will periodically set the next build to run. Along with this, I use some CMD commands to move my data into another folder with the Build number as my folder name.
Now here is the issue, if the previous build was unstable and Naginator runs the next build and that build is stable then what I need to do is delete the previous unstable build data from the folder manually. Is it possible to fetch the previous build number in Jenkins so that I can delete the file in an automated way - lets say in CMD Commands .BAT file.
Jenkins has it's own global variables. You can check whem in your pipeline job -> Pipeline Syntax -> Global Variables Reference.
Additionally check http://jenkins_url:port/env-vars.html/
For your purpose BUILD_NUMBER exist.
Just create new env var like this:
PREV_BUILD_NUMBER = $($BUILD_NUMBER -1)
Excuse me if this piece of code will not work, I'm not good about scripting) Just for example.
UPDATE:
also you can find in mentioned reference a list of variables:
previousBuild
previousBuildInProgress
previousBuiltBuild
previousCompletedBuild
previousFailedBuild
previousNotFailedBuild
previousSuccessfulBuild
Create a Seed Job using DSL that would create 5 Child Jobs. As part of build step I've to use ${WORKSPACE} environment variable, which should read the value from the slave machine where the child Job is running. But however when the child jobs are created, the workspace value is replaced with SeedJobs workspace location from master server.
How would I restrict seed job not to inject its ${WORKSPACE} value.
Could some one help me on this please.
Thanks
Mano
To refer to the BUILD_NUMBER of the generated job in your job-dsl code, you should code it as follows :
"\${BUILD_NUMBER}"
The important part is the \ before the $ sign which prevents the evaluation of the BUILD_NUMBER variable as it pertains to the Goovy runtime that is (likely) your Jenkins job-dsl seed job.
The config.xml of the generated job will contain ${BUILD_NUMBER} instead of a digit that represents you build number of your seed-job. And then your generated job will evaluate that when it runs.
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.
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.
I have a lot of jobs that contain very similiar configuration values.
Then I had the idea to use the EnvInject Plugin to read a generated Properties file, which contains most of my configuration.
However, I don't know up to what extent I can use environment variables in a Jenkins job configuration.
For instance, in a Maven job, I can specify the Root POM to be ${JOB_NAME}/pom.xml. Jenkins will tell me it can't find the file, but the job actually works.
Configuring other parts (like the number of builds to keep) fails miserably (the variable is simply removed).
So does anyone have experience in using environment variables to cut down the copy/paste configuration in Jenkins?
If your objective is to cut down on cut and paste then the job dsl plugin might suit your needs better.
You can build a template job (using statement) then use that to build your main jobs.
Modified from the tutorial
job {
name 'DSL-Tutorial-1-Test'
using 'seed-job'
scm {
git('git://github.com/jgritman/aws-sdk-test.git')
}
triggers {
scm('*/15 * * * *')
}
steps {
maven('-e clean test')
}
}
In addition if you need to change all the jobs you can change your template and rebuild the main jobs