Passing workspace url of Job A to Job B in Jenkins - 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.

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

How to trigger a Jenkins Pipeline job to use the same workspace

I have 2 pipeline jobs. JOB A & JOB B
I want to trigger JOB B to run on the same node and workspace of JOB A.
When I do so, Jenkins adds "#2" to the workspace of JOB B. for example:
JOB A workspace = /DATA/Jenkins_User/workspace/JOB-A
JOB B receives the correct workspace as parameter,
but when I perform ws(customWorkspace) it is being built in
/DATA/Jenkins_User/workspace/JOB-A#2
Any idea how can I remove the "#2" characters?

parametrized build plugin jenkins is not working

I have a problem with parametrized build plugin in jenkins.
I have configured 2 jobs.
job A and job B
Job A will send the parameters to Job B
But the problem is the values are never being sent from job A to job B
Job A configuration
Job A config
Job B configuration
Job B config
[ I am using jenkins version 2.7.2 ]
Please help me with the above problem
I believe you have to declare COPYJOB as a build parameter in Job B. Only then will it be picked up as an environment variable when passed from Job A, and you will then be able to reference it like $COPYJOB in your goals

How can I pass “build-step parameters” to a downstream project (freestyle project with parameterized triggers)?

I have a Jenkins setup with two freestyle-projects linked via Jenkins upstream/downstream post-build triggers (Jenkins Parameterized Trigger plugin). The upstream project builds something that is uploaded somewhere. And the downstream project picks that new thing up and tests it.
At the moment the downstream project finds the build by date by looking for “the latest”. I want to change that.
I would like to change upstream to generate a random string as filename in the build step and pass that to downstream as parameter. How can I do that?
(I know how to generate a random string – the point is: how to pass build-step information down to the “downstream job”?)
In your upstream pipeline script, add this:
build job: 'your-downstream-job', parameters: [[$class: 'StringParameterValue', name: 'YOUR_STRING_PARAM', value: "${yourRandomGeneratedStringVariable}" ]]
And your your-downstream-job should be e.g. a normal job which is parameterized (check the tick at This project is parameterized), with the StringParameter YOUR_STRING_PARAM.
Then just read the value of this environment variable and you can go on.

How to pass output from one pipeline to another in jenkins

I'm new to Jenkins and I've been tasked with a simple task of passing the output from one pipeline to the other.
Lets say that the first pipeline has a script that says echo HelloWorld, how would i pass this output to another pipeline so it displays the same thing.
I've looked at parameterized triggers and couple of other answers but I was hoping if someone could layout the step by step procedure to me.
If you want to implement it purely with Jenkins pipeline code - what I do is have an orchestrator pipeline job that builds all the pipeline jobs in my process, waits for them to complete then gets the build number:
Orchestrator job
def result = build job: 'jobA'
def buildNumber = result.getNumber()
echo "jobA build number : ${buildNumber}"
In each job like say 'jobA' I arrange to write the output to a known file (a properties file for example) which is then archived:
jobA
writeFile encoding: 'utf-8', file: 'results.properties', text: 'a=123\r\nb=foo'
archiveArtifacts 'results.properties'
Then after the build of each job like jobA, use the build number and use the Copy Artifacts plugin to get the file back into your orchestrator job and process it however you want:
Orchestrator job
step([$class : 'CopyArtifact',
filter : 'results.properties',
flatten : true,
projectName: 'jobA',
selector : [$class : 'SpecificBuildSelector',
buildNumber: buildNumber.toString()]])
You will find these plugins useful to look at:
Copy Artifact Plugin
Pipeline Utility Steps Plugin
If you are chaining jobs instead of using an orchestrator - say jobA builds jobB builds jobC etc - then you can use a similar method. CopyArtifacts can copy from the upstream job or you can pass parameters with the build number and name of the upstream job. I chose to use an orchestrator job after changing from chained jobs because I need some jobs to be built in parallel.

Resources