How to pass environment variable to Jenkins Remote API when submitting job - jenkins

I have a declarative pipeline job (this is not multi-branch pipeline job using Jenkinsfile) without parameters but some stages are conditional based on value in environment variable:
stage('deploy-release') {
when {
environment name: 'GIT_BRANCH', value: 'master'
}
steps {
sh "mvn deploy:deploy-file -B -DpomFile=pom.xml -Dfile=target/example.jar -DrepositoryId=maven-releases -Durl=${NEXUS_URL}/repository/maven-releases/"
}
}
I want to trigger the job from external system but I need to pass correct value of given environment variable. Is there some way how to do that via Jenkins Remote API?

For passing value of given environment variable, you need to define parameters with the exact same name as that of environment variable for your job by selecting "This build is parameterized".
You can refer Parameterized Build

Related

How to access pipeline name in the freestyle job

I have a pipeline which triggers a jenkins freestyle job.
How to access the pipeline name from the freestyle job.
I tried using ${BUILD_CAUSE} but it shows the value as UPSTREAMTRIGGER.
How to get the pipeline name?
JOB_NAME may contain the name of the job that you're after, unless it was triggered by an upstream job.
See a list of generic Jenkins environment variables that are available in most builds. The best way to find out what environment variables are available is to run sh 'printenv' in the steps.
If you need the name of the upstream job in a downstream job, I'd suggest setting a parameter on the downstream job and then referencing it when you trigger the job.
e.g. Upstream Pipeline job:
...
step {
build job: 'freestyle-downstream-job', parameters: [
string(name: 'upstream_job_name', value: "${JOB_NAME}")
]
}
...

Pass environment variables from parent job to child job Jenkins pipeline

I have two pipeline jobs.
Parent job: This job contains Gerrit trigger and builds on every patch-set created. After building of this job I can see the Gerrit environment variables into the build environment variable section.
Child job: which runs only if the gerrit_branch is not equal to master.
I want to pass all the gerrit environment variables to this job like the parent job.
Is there any way to pass all the env variable to the child job.
I have taken two pipeline jobs: Pipeline1 (parent job) and pipelineB (child job)
Pipeline1: I am doing SCM checkout from github where Jenkinsfile is present and in Jenkinsfile, I have called pipelineB (child job) where I am passing the parent job environment variable (GIT_BRANCH).
Pipeline1 configuration
Jenkinsfile of Pipeline1
pipeline {
agent any
stages
{
stage ('Build JobB')
{
steps {
sh 'env'
build job: 'pipelineB', parameters: [string(name: 'GITHUB_BRANCH', value: "${env.GIT_BRANCH}")]
}
}
}
}
GIT_BRANCH is environment variable here. (https://plugins.jenkins.io/git/#environment-variables)
pipelineB: I have used the option This build is parameterized to capture the value of environment value being passed by parent job i.e. Pipeline1. Then, used the variable in pipeline script.
pipelineB Configuration
Please try it out.

Jenkins - How to pass environmental variable from freestyle job to pipeline job

I have a freestyle job in Jenkins and would normally pass parameters to another freestyle job using the Predefined parameters option.
example:
PROJECT=Myproject
PATH=/depot/workspace/
Previously I could access the above values through the KEY in the downstream job through the environment by using ${PROJECT} OR ${PATH}.
My problem now is that I have a pipeline job that needs to access the above values but when using ${PROJECT} OR ${PATH} it does not work.
So, in general how I want it to work is have the freestyle job run first and pass the parameters to the downstream pipeline job.
You might need to use "${params.PROJECT}" in your pipeline to access the parameters.

Jenkins Share common environment variables in a groovy method

I am building a declarative JenkinsFile, I have some common variables that I want to be shared across some Jenkins projects and jobs.
So I created a jenkins shared library, but for some reason i can't get my Jenkins file to to read the common environment variables from common groovy.
pipeline {
environment {
commonEnv()
Email_Notification_Enabled="true"
Slack_Notification_Enabled="false"
}
}
and in my groovy i had:
def call() {
a = "abc"
b = "abc"
}
It throws error that commonEnv() is not allowed in environments.
What is the possible way to achieve such behaviour.
You could write a Groovy method that sets the common environment variables. Please refer this Stack Overflow question to know how to do this. Include that method in Jenkins pipeline shared library.
Now call this Groovy method in declarative pipeline of each of your jobs. Remember that in a declarative pipeline, you may use Groovy only inside the script step. So, your pipeline would look something like:
pipeline {
stages {
stage("First stage") {
steps {
script {
// call to Groovy method that sets environment variables
}
// other steps
}
}
// other stages
}
}
Hope, it helps.
Since you need to have environment variables that are shared across all Jenkins projects and jobs, you should set them up on Jenkins instance level rather than on a Jenkins project or job level.
So, instead of doing it in a Jenkinsfile (which will do it at Jenkins job level), I will do it in Manage Jenkins > Configure System > Global properties > Environment Variables:
The environment variables could then be read in the pipeline script from Jenkins Global Variable env:
echo "This is my Jenkins global environment variable ${env.MY_ENV_VAR_NAME}"

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.

Resources