Pipeline - Parameterised Trigger Plugin - jenkins

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

Related

How to check if "Restrict where this project can be run" is checked or not in 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.

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.

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'

How can I create parameterized Jenkins job?

I want to use same job in different machine. But I don't want to change the configuration of the job each time. Can I pass the machine name label as parameter and run the job in different machine ? (Not simultaneously).
I want to pass parameters while running a job to the script which I have written in th configuration (batch script). Can we do that ?
Can I get a return value from a job and use it in next job?
Example criteria:
User needs to use different environments for executions.
1. Select jenkins project
2. Job -> Configure -> Select this project is parameterized check box.
3. Select Add parameter drop down - > String parameter
4. Define a string parameter (In my example I use environment variable as : BaseURL)
5.Now under build section initialize the mvn command user needs to execute.Here in this case I want to execute my jenkins build on a environment where I specify. So I should be able to execute my build in different environment each time with out changing the code.
My mvn command: I pass my environment url as a parameter (Using $ sign). This is based on user requirement.
clean test -Dcustomproperty="$BaseURL"
6. Apply and save your Jenkins project.
7. Now your project is parameterized. Then your project should have Build with parameters option.
8.Click Build with parameters link and enter your parameter (In this example my environment variable) and click build. Your jenkins job should run with the parameter.
NOTE: This build won't succeed unless the passing parameter is not utilized in the automation script. So script has to be modified to retrieve the passing variable.
String BaseURL= System.getProperty("customproperty");
Yes, you can pass a node label parameter with NodeLabel Parameter Plugin.
Yes, you can define parameters, as described, in Parameterized Builds and then use it in your script as an environment variable:
The parameter is available as environment parameters. So e.g. a shell ($FOO, %FOO%) or Ant ( ${env.FOO} ) can access these values.
This is not exactly a return value, but you can pass any parameter (with its value) to the downstream job with Parameterized Trigger Plugin.
Use parameterized Build plugin and NodeLabel Parameter Plugin

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.

Resources