Pass environment variables from parent job to child job Jenkins pipeline - jenkins

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.

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}")
]
}
...

Jenkins build multibranch pipeline from another multibranch pipeline

I have Jenkins setup with 2 multibranch pipeline which depend on each other let say multibranchPipelineA and multibranchPipelineB. I would like a job from multibranchPipelineA to build specific branch in multibranchPipelineA and wait the build to finish
I have tried use below from multibranchPipeleA Jenkinfile
stage('Build MiniApp Libs') {
steps {
build(
job: "../multibranchPipeleB/master",
propagate: true,
wait: true
)
}
}
But always receive No item named ../multibranchPipeleB/master found.
If I use single pipeline, let's say pipelineB, then the below work
../pipelineB
How can I build specific branch multibranchPipeline from another multibranchPipeline jobs? and wait the build to finish?
To build another multibranchPipeline you do not need .. before its name. So in your case just use:
job: "multibranchPipeleB/master"

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

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

Passing workspace url of Job A to Job B in Jenkins

I have two pipeline jobs Job A and Job B. I need to pass the workspace url of Job A (say /var/lib/jenkins/workspace/JobA) to be used by Job B. The main idea is I am trying to copy the contents of target folder which is generated due to maven build but I don't want to use Copy Artifacts Plugin or Archive Artifacts Plugin to achieve the same.
I have tried using the option "This job is parameterized" where Job A is the upstream of Job B but i am unable to so using that option.
Can anyone help to achieve the same ?
The WORKSPACE variable is an env variable from Jenkins and is pointing like / .
For eg.
If the job name is Job_A --> the workspace value will be <jenkins_path>/Job_A
For eg.
If the job name is Job_B --> the workspace value will be <jenkins_path>/Job_B
So you can't use the WORKSPACE var and expects the Job_B to point to Job_A workspace value.
The below can be used to get certain properties from the upstream job.
Jenkins - How to get and use upstream info in downstream
Even if you want to hard code it in the Job_B it will be fine(not recommended)
Also for this to work your node should be same for both the jobs
I have found a way to do the same and it is working fine.
I have made the Job B a parameterized job using "This project is parameterized" and used string parameter.
Then, in the pipeline script of Job A, i invoked the Job B by passing WORKSPACE env variable. Here is the declarative pipeline script for Job A:
pipeline {
agent any
stages
{
stage ('Build JobB')
{
steps {
build job: 'jobB', parameters: [string(name: 'UPSTREAM_WORKSPACE', value: "${env.WORKSPACE}")]
}
}
} }
Now, in Job B pipeline you can try to echo the variable UPSTREAM_WORKSPACE. This is how we can pass the workspace url and use it to copy the artifacts.

How can I pass parameters from a job to the other job when both the jobs are building inside a pipeline?

I have 3 jobs building in pipeline job as below
node('master') {
stage ('first')
parallel firstBranch: {
build 'job1'
build 'job2'
}
stage ('second')
build 'job3'
}
My job2 can take parameters and it should be passed from job1. How can I do that in pipeline?
I cannot use Parameterized Trigger Plugin as I don't want to trigger one job from another.
I want a way to pass the parameters from one job1 to job2 inside pipeline project.

Resources