How to check if "Restrict where this project can be run" is checked or not in Jenkins? - jenkins

When a job is building in Jenkins, how can I check if "Restrict where this project can be run" is checked or not without installing any plugin.
I thought it would show in environment variables but i didn't see it.

Jenkins uses assignedNode property for restricting the project to label/node.
You can get this property using
/config.xml - Look for assignedNode property
Using groovy (run this from master console)
def job = Jenkins.instance.getItemByFullName('')
println job.getAssignedLabel().getExpression()
For more information, check - getAssignedLabel
Hope this helps

The groovy script doesn't give correct results for pipeline scripts, however, if the script uses the agent { 'xy' } - directive. The groovy will always show master - and for the initial pipeline script, that is of course always true. Something to keep in mind.

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.

Pipeline - Parameterised Trigger Plugin

I have a pipeline type job in my Jenkins. What I am trying to do is to run some pipelines with a parameter like release. For example, I want to run this pipeline for version 1.3.5. I prepared a pipeline code for that and tried to use Parameterized Trigger Plugin to make it parameterised. This plugin was working for normal type tasks but, I couldn't make it works with pipelines.
Here is my definition of the variable;
I can't access the variable I defined with the plugin. Here is the pipeline part which tries to access it;
....
build_tag = "${TAG}"
But, build_tag variable is always null. I tried env.${TAG} and ${env.TAG} but no result.
I defined String param named THIS_IS_TEST and was able to get the value with -
echo "${THIS_IS_TEST}"
Make sure that the plugin is updated

Paramertized Build - Jenkins Pipeline

Hoping someone can point me in the right direction.
I have just started to look at the Jenkins pipeline and am trying to work out how to trigger a parameterized build for a job that already exists using the Jenkinsfile
In my Jenkinsfile i have
node {
stage 'Build My Job'
build job: 'my-build'
}
I need to be able to pass a branch name from the Jenkinsfile config to the job that is running? If i am misunderstanding anything then please let me know
Thanks
Instead of starting with a Jenkinsfile, it's easier to start with a pipeline job in which you can directly edit the pipeline script. By clicking the 'Pipeline Syntax' link you can open the snippet generator, where you can generate the Groovy for a particular step:
This Snippet Generator will help you learn the Groovy code which can be used to define various steps. Pick a step you are interested in from the list, configure it, click Generate Groovy, and you will see a Groovy statement that would call the step with that configuration. You may copy and paste the whole statement into your script, or pick up just the options you care about. (Most parameters are optional and can be omitted in your script, leaving them at default values.)
In the configuration page select 'This project is parameterized' and sleect parameter type and enter parameter name
You can access this new parameter value in you jenkinsfile using 'env.parameterName'

Jenkins Promoted Build Parameter Value

I have a jenkins job that runs some tests and promotes the build if all tests pass.
I then have a second job that has a 'Promoted build parameter' which is used for deployments.
THe idea is that the deply job should let you pick one of the prompted builds for deploying. The issue I'm having is that I can pick a promoted build, but I have not idea how I access information about the build.
I've named the build parameter
SELECTED_BUILD
And according to the docs this should then be available via the environment.
The problem is that it doesn't seem to be bound to anything.
If I run a build step to exectute this sheel script:
echo $SELECTED_BUILD
echo ${SELECTED_BUILD}
The values are not interpolated / set.
Any idea how I can access the values of this param?
Many Thanks,
Vackar
Inject the information as variable user jenkins plugin (eject evn variable).
while triggering parameterized build on other job, pass above variable as parameter to next job.

Changing Jenkins build number

Is there a way to change the build number that is sent via email after a job completes? The problem is that are product builds are NOT being done by Jenkins, so we want to be able to get the build number(ie. from a text file) and update the build number in Jenkins to match it. I have tried to set the build number:
set BUILD_NUMBER=45
But the email is still showing the build number that Jenkins originally set.
If you have access to the script console (Manage Jenkins -> Script Console), then you can do this following:
Jenkins.instance.getItemByFullName("YourJobName").updateNextBuildNumber(45)
can be done with the plugin:
https://wiki.jenkins-ci.org/display/JENKINS/Next+Build+Number+Plugin
more info:
http://www.alexlea.me/2010/10/howto-set-hudson-next-build-number.html
if you don't like the plugin:
If you want to change build number via nextBuildNumber file you should
"Reload Configuration from Disk" from "Manage Jenkins" page.
Under the job workspace folder, like:
C:\Program Files (x86)\Jenkins\jobs\job_name
there is a file named nextBuildNumber.
Setting the build number in the file and reloading the configuration from disk (Manage Jenkins menu) will force the next build you start to have the value from the file as BUILD_NUMBER.
If you have branch name including Forward Slash (using git flow for example), you will need to replace the Forward Slash with its Unicode character %2F within the branch name.
Here is an example for the pipeline My-Pipeline-Name and the branch release/my-release-branch-name
Jenkins.instance.getItemByFullName("My-Pipeline-Name/release%2Fmy-release-branch-name").updateNextBuildNumber(BUILD_NUMBER)
I was able to find out about this by running the following command which will list the different jobs (branches) for your pipeline
Jenkins.instance.getItem("My-Pipeline-Name").getAllJobs()
Hope it helps.
Perhaps a combination of these plugins may come in handy:
Parametrized build plugin - define some variable which holds your build number
Version number plugin - use the variable to change the build number
Build name setter plugin - use the variable to change the build number
You can change build number by updating file ${JENKINS_HOME}/jobs/job_name/nextBuildNumber on Jenkins server.
You can also install plugin Next Build Number plugin to change build number using CLI or UI
For multibranch pipeline projects, do this in the script console:
def project = Jenkins.instance.getItemByFullName("YourMultibranchPipelineProjectName")
project.getAllJobs().each{ item ->
if(item.name == 'jobName'){ // master, develop, feature/......
item.updateNextBuildNumber(#Number);
item.saveNextBuildNumber();
println('new build: ' + item.getNextBuildNumber())
}
}
Follow the steps: Jenkins Console > Manage Jenkins > Script Console, then write the script as:
Jenkins.instance.getItemByFullName("Your_job_name").updateNextBuildNumber(45)
By using environmental variables:
$BUILD_NUMBER =4

Resources