Flagging a Jenkins Job - jenkins

We do a lot of Jenkins System Groovy Scripts to check our Jenkins configuration for things, such as someone allowing Anonymous access when they shouldn't. But there are times when we want to flag a job to be ignored in these self-audits.
My thought was to set an Environment Variable via the EnvInject plugin. But I can't see where you can use the Groovy System Scripts to get these values?
Anyone know how to do this? Alternatives to this method would also be helpfull.

You can use obtain the environmental variables from a System Groovy Script using the following:
def myVar = build.getEnvironment(listener).get('JOB_NAME')
println "JOB_NAME = " + myVar

Related

Variable indirection in a Jenkins pipeline?

In my Jenkins system configuration page, I have 3 variables defined, namely, sandbox_deployed, staging_deployed, and production_deployed. In my pipeline, I want to access one of these variables, based on a pipeline property, BUILD_ENV, defined in the job's configuration page. In other words, in my job's configuration page I have
BUILD_ENV=sandbox
How can I write pipeline code that does
println "$env.${env.BUILD_ENV}_deployed"
If I write it like in the above println, I get
org.jenkinsci.plugins.workflow.cps.EnvActionImpl#336841dd.sandbox_deployed
But I really want this
println "env.sandbox_deployed"
which prints out the correct value of the sandbox_deployed variable.
Try this code, at least it worked for me
println "${env."${env.BUILD_ENV}_deployed"}"

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

Reference groovy variables over multiple script parts in jenkins

In a Jenkins job, I have a groovy script, that is split in two parts. It does something before SCM and does some other thing at the end of the job as last build instruction.
Now, I need to access a variable in the second part, that I set in the first part.
How to do that?
I tried to mark a variable as a field with #Field Boolean myFlag = false, but still myFlag cannot be accessed in the second part of the script.
Interpreter says:
groovy.lang.MissingPropertyException: No such property: myFlag for class: Script1
Does anyone know how to accomplish accessing a variable from the first part of the script in the second?
Thanks!
you can pass these variables as the Jenkins Job's parameters. That way you have access to the variables throughout.
It was not clear from your question if you're running a single script in the job or if you're executing multiple scripts.

How to pass parameter to the downstream job but shouldn't trigger the downstream job?

I have two jobs which are pipelined, I want to send the BUILD_NUMBER info of upstream job to downstream.
The main point is we shouldn't trigger the downstream project. Triggering of downstream project should be manual.
Whenever I trigger the downstream job it need to get the latest BUILD_NUMBER of the upstream.
How can I do this?
Use environment inject plugin in the second job
As mentioned by #VnoC in the first job write the buildnumber in the properties file like below
echo "last_build_number = ${BUILD_NUMBER}"> ../Common.properties
Mention in the inject enviornement variable the same property file path for the secod job (Path is repective to the active workspace so keeping it in a common location)
Now is the second job you can access the variable like $last_build_number
You could set an environment variable, which will then be read by the next job with WithEnv(${last_build_number}) {...}.
That is not ideal since this is listed as an anti-pattern in Top 10 Best Practices for Jenkins Pipeline Plugin
While you can edit some settings in the env global variable, you should use the withEnv syntax instead.
Why? because the env variable is global, changing it directly is discouraged as it changes the environment globally, so the withEnv syntax is recommended.
Still, in your case, that could be an acceptable workaround.
environment {
last_build_number = ${BUILD_NUMBER}
}
I thought first to write it in a file, but the feature of reading parameters from a file is still pending (JENKINS-27413).

Retrieving Jenkins node variables from master

So my google-fu has failed me and the Jenkins O'Reilly book isn't helping either.
I have a Jenkins setup with a master and 20-odd nodes. I make heavy use of custom environment variables on the nodes, as many of them perform similar tasks, only slightly different platforms.
I'm now in a position that I have a job that runs on the master (by necessity), which needs to know certain node properties for any given node, including some of the environment variables that I've set up.
Is there any way to reference these? My alternative seems to be to have hundreds of environment variables in the master in the form node1_var1, node2_var1, node1_var2, node2_var2 etc., which just seems messy. The master clearly has knowledge of the variables, as that's where the configuration for them is done, but I just can't find a way to specify them in a job.
Any help (or ridicule and pointing out of obvious answers) much appreciated...
Here's a simple Groovy script that prints the list of environment variables for each slave:
for (slave in jenkins.model.Jenkins.instance.slaves) {
println(slave.name + ": ")
def props = slave.nodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)
for (prop in props) {
for (envvar in prop.envVars) {
println envvar.key + " -> " + envvar.value
}
}
}
Warning: I am not an experienced Groovy programmer, so I may not be using the appropriate idioms of Groovy.
You can run this from the Jenkins script console in order to experiment. You can also run a "System Groovy Script" as a build step. Both of the above require the Groovy plugin. If you don't use Groovy in your job, you could use this script to write a properties file that you load in the part of your build that does the real work.

Resources