How to import env variables in child jenkinsfile from parent jenkinsfile? - jenkins

I have a monorepo setup where I am trying to dispatch service Jenkinsfile from root JenkinsFile
The structure looks like this:
Monorepo
- Jenkinsfile (at root)
- services
- Jenkinsfile (services Jenkinsfile)
- Service A
- src/
- Service B
- src/
Service Jenkinsfile is triggered from root Jenkinsfile.
These env variables are working fine in root jenkinsfile but they are giving null values in service jenkinsfile.
stage('Echo env') {
echo "Branch_name=${env.BRANCH_NAME}, PR_Branch_change=${env.CHANGE_BRANCH}, PR_Key=${env.CHANGE_ID}, PR_Base=${env.CHANGE_TARGET}, Key=$CHANGE_ID, branch=$CHANGE_BRANCH, base=$CHANGE_TARGET"
} // Stage is same in both the jenkinsfile
In service jenkinsfile either gives error: or Print Null values
Is there any way to import or reference the env variables from another (root) Jenkinsfile

Finally, I was able to get env variables values printed from parent jenkinsfile to child jenkinsfile
Here is the post I went through and somehow it helped me getting end result
Pass environment variables from parent job to child job Jenkins pipeline

Related

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.

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 to pass env vars to a MultibranchPipelineJob created by Jenkins Job DSL?

I am creating a MultibranchPipelineJob with Jenkins Job DSL. I want to pass some environment variables to the job, but I can't figure out how to do that from the documentation.
MultibranchPipeline Job no longer support parameters
You can use Folder Properties Plugin to set your Env variables, which can be accessed by all the jobs within that folder. https://plugins.jenkins.io/folder-properties/
However, the MultiBranch pipeline job has a lot of performance issues so we moved away from multibranch pipeline jobs. We wrote a DSL job that would act as a multibranch pipeline job - which will scan through the git branches and create Simple pipeline jobs as needed.
You pass them as parameters like this:
parameters {
stringParam("MyVariable1", "my-value1")
stringParam("MyVariable2", "${my-dynamic-value2}")
}
Then consume them in the job using parameters or environment (both work equally) as this:
echo "my vars are ${parameters.MyVariable1} or ${env.MyVariable2}"

groovy script loaded from jenkinsfile not found

currently I have an "all inclusive" jenkinsfile which contains various functions.
In order to re-use those functions in other jenkinsfiles I want to put them into separate groovy scripts and load them from the jenkinsfile(s).
scmHandler.groovy:
#!groovy
def handleCheckout() {
if (env.gitlabMergeRequestId) {
echo 'Merge request detected. Merging...'
}
...
}
return this;
in jenkinsfile I do:
...
def scmHandler = load ("test/scmHandler.groovy")
scmHandler.handleCheckout()
I tried to follow the instructions from here but jenkins is constantly complaining that there is no such file scmHandler.groovy an I get:
java.io.FileNotFoundException: d:\jenkins\workspace\myJenkinsJob\test\scmHandler.groovy
Both jenkinsfile and scmHandler.groovy reside in a test/ subdir of the workspace in the git repo of the project to boild and are checked out correctly on master:
/var/lib/jenkins/jobs/myJenkinsJob/workspace#script/test/scmHandler.groovy
However I cannot find them on the slave node where the jenkinsfile executes the build steps inside a node {}. There I only see old versions of the jenkinsfile since the (separated) checkout step is not executed yet.
How do I correctly access the handleCheckout.groovy? What am I miss here?
Actually I find this a neat way to "include" external groovy files without using a separate library.
Use checkout scm before loading scmHandler.groovy
checkout scm
def scmHandler = load ("test/scmHandler.groovy")
scmHandler.handleCheckout()

Get variables set in build.gradle in Jenkins pipeline build

I have a Jenkins pipeline job that builds my project using gradle. As part of the build i would like to use a global variable I have set in the build.gradle file
project.ext.set("MyVar", "My Value")
How can i access this variable in the pipeline build, so
myVar = varSetInGradleBuild
Hope that makes sense
Thanks
Well, usually dependency is reverse, Jenkins sets variables for build to control behavior with -P key.
For your use case you can define variables in gradle.properties file and read it in Jenkins pipeline.

Resources