Downstream Jenkins project gets wrong upstream run parameter - jenkins

I'm having a problem with a Jenkins build pipeline. All jobs after the first one are parameterized with the "Run Parameter" of the first job. By default, this should reference the most recent stable build of the first job. Each subsequent job uses the "Run Parameter" of the first job to access saved artifacts from the first job. Each subsequent job triggers the next job of the pipeline as a parameterized build and passes the aforementioned "Run Parameter". The first job of the pipeline triggers the second job as a simple (i.e., not parameterized) build.
Here's a screenshot of the relevant configuration of a downstream job:
My problem is that the job number in the "Run Parameter" isn't the job number of the first job of the pipeline. Instead, it's the job number of the first job of the previous pipeline. Thus, if the first job is on build #11, then all subsequent job of that pipeline will access the archive of build #10 of the first job.
How can I get the subsequent jobs of the pipeline to access the archive directory of the first job of the pipeline?

I discovered the answer. Apparently, the reason the downstream job was using the artifacts from the upstream job of the previous pipeline was because I had set the "Run Parameter" filter in the configuration of the downstream job to "Stable Builds Only". Setting this filter to "All Builds" results in correct behavior.
It's as if Jenkins doesn't consider an upstream job to be stable when it's starting another build in its post-build section.

Quote: "By default, this should reference the most recent stable build of the first job."
Do you mean the last successful build of the Top job. Since in that case there might be a case where the last successful build of the top job was #7 and current build is #11. So you want the downstream jobs to look for #7 and not #10.
If that is the case then I will suggest putting a groovy build step. Install the groovy plugin for that. But before that test the script.
Open: YourJenkinsServerURL/script
Run this script.
def tmp = hudson.model.Hudson.instance
def myJobName="YourTopJobName";
tmp.getItems(hudson.model.Job).each {job ->
if(job.displayName == myJobName)
{
println(job.getLastSuccessfulBuild())
}
}
In groovy you can access and set an environment variable (injected via envInject plugin maybe) to this last successful build number and then pass on this variable to downstream job.
If that is not the case then I will suggest use Nant Script.
Use "int::parse()" to convert the string format build number to integer. Decrement the value and then pass on the value to the downstream job.

Related

How can I analyze console output data from a triggered jenkins job?

I have 2 jobs 'job1' and 'job2'. I will be triggering 'job2' from 'job1'. I need to get the console output of the 'job1' build which triggered 'job2' and want to process it in 'job2'.
The difficult part is to find out the build number of the upstream job (job1) that triggered the downstream job (job2). Once you have it, you can access the console log, e.g. via ${BUILD_URL}consoleOutput, as pointed out by ivoruJavaBoy.
How can a downstream build determine by what job and build number it has been triggered? This is surprisingly difficult in Jenkins:
Solution A ("explicit" approach): use the Parameterized Trigger Plugin to "manually" pass a build number parameter from job1 to job2. Apart from the administrational overhead, there is one bigger drawback with that approach: job1 and job2 will no longer run asynchronously; there will be exactly one run of job2 for each run of job1. If job2 is slower than job1, then you need to plan your resources to avoid builds of job2 piling up in the queue.
So, some solution "B" will be better, where you extract the implicit upstream information from Jenkins' internal data. You can use Groovy for that; a simpler approch may be to use the Job Exporter Plugin in job2. That plugin will create a "properties" (text) file in the workspace of a job2 build. That file contains two entries that contain exactly the information that you're looking for:
build.upstream.number Number of upstream job that triggered this job. Only filled if the build was triggered by an upstream project.
build.upstream.project Upstream project that triggered this job.
Read that file, then use the information to read the console log via the URL above.
You can use the Post Build Task plugin, then you can get the console output with a wget command:
wget -O console-output.log ${BUILD_URL}consoleOutput

In Jenkins how to trigger another build job based on the build parameter passed to current build job?

I have build jobs like a-build, a-deploy. b-build, b-deploy.
*-deploy are downstream job for *-build jobs. So they look like,
a-build
|
+-a-deploy
b-build
|
+-b-deploy
Now I have another job X-build. It accepts a-build, b-build etc as a parameter. So I if I run X-build with a-build as parameter it should complete with a post build action that triggers a-deploy. How can that be done?
You can accomplish this quite easily if you receive the job name in a parameter.
You can then use "Call/Trigger Builds on Other Projects" step, and use the Parameter you receive in the job name:
If the step is not available to you via Post Build menu, you can get to it via "Execute set of scripts" post-build step.

How to synchronize subproject build number

Jenkins JobA triggers JobB as a subproject.
Is there any way that I can force JobB to have the same build number as JobA?
I am currently passing an environment variable into JobB so that JobB can use the correct number during its build. But it's still confusing having JobA and JobB have different build numbers.
I am also using the "Next Build Number Plugin", but JobA and JobB's build numbers keep drifting apart over time as JobA fails before it calls JobB, forcing me to come back and manually fix it.
You should consider Build Name Setter Plugin, as it allows setting a BUILD_NUMBER through a variable. Configure it to use something like $parentBuildNumber
Now, in JobB configure a text/string parameter, parentBuildNumber.
When you trigger JobB from JobA, you should be using Parameterized Trigger Plugin.
There configure a predefined parameter set as parentBuildNumber=$BUILD_NUMBER
In the parent job get the next build number, substract by 1 if needed and pass it to child job:
cat $JENKINS_HOME/jobs/$JOB_NAME/nextBuildNumber
And output it to
$JENKINS_HOME/jobs/$CHILD_JOB_NAME/nextBuildNumber
You may or may not have to install the NextBuildNumber plugin:
Also, there is a CLI command set-next-build-number. Try executing jenkins CLI help.

running a shell script once build passes

I have a job that is running unit tests on a project build and then ssh into a staging server and pulling down from the master branch. Right now I'm using the post-build-script but this is running regardless of pass/fail. I'm trying to use the parameterized build plugin to trigger a new job when the build is passed. So far I've created the new job and set to trigger in the configuration of the original.
The new job is building ok on its own but the original job isn't triggering it. From 'Add post-build action' I've selected 'Trigger parameterized build on other projects' with build triggers:
Projects to build: new_job, Trigger when build is: Stable or unstable but not failed.
Any ideas appreciated!
C
If you don't actually need to pass a parameter to the second build, make sure that "Trigger build without parameters" is checked in the parameterized build trigger options.
The "Post build task" allows you to query the console log of the build step, and is executed only when criteria is met.
Jenkins writes BUILD SUCCESSFUL in the console log for every build step that had passed.
In your "Post build task" step, under Log text just put BUILD SUCCESSFUL, and under Script put your linux script/commands.
This way your script/commands will only be executed if the Build Step was successful

Jenkins Parallel Trigger and Wait

I have 4 jobs which needs to be executed in the following sequence
JOB A
|------> JOB B
|------> JOB C
|------> JOB D
In the above
A should trigger B & C parallely and C inturn triggers D.
A should hold the job as running till all 3 of them completed.
I tried the following plugins and couldn't achieve what I am looking for
Join Plugin
Multijob Plugin
Multi-Configuration Project
Paramterized Trigger Plugin
Is there any plugin which I haven't tried would help me in resolving this. Or is this can be achieved in a different way. Please advise.
Use DSL Script with Build Flow plugin.
try this Example for your execution:
build("job A")
parallel
(
{build("job B")}
{build("job C")}
)
build("job D")
Try the Locks and Latches plugin.
This may not be optimal way, but it should work. Use the Parameterized Trigger Plugin. To Job A, add a build step (NOT a Post Build Action) to start both Jobs B and C in the same build step AND block until they finish. In Job C, add a build step (NOT a Post Build Action) that starts Job D AND blocks until it is finished. That should keep Job A running for the full duration.
This isn't really optimal though: Job A is held open waiting for B and C to finish. Then C is held open until D is finished.
Is there some reason that Job A needs to remain running for the duration? Another possibility is to have Job A terminate after B and C are started, but have a Promotion on Job A that will execute your final actions after jobs B, C and D are successful.
I am trying to build a same system. I am building a certification pipeline where I need to run packager/build/deploy jobs and and corresponding test jobs. When all of them are successful, I want to aggregate the test results and trigger the release job that can do an automated maven release.
I selected Build pipeline plugin for visualization of the system. Initially tried with Parameterized trigger Plugin with blocking builds. I could not setup archiving the artifacts/fingerprinting and downstream build relationship this way since archiving the artifacts works only in postbuild. Then I put the Parameterized trigger in Post build activity. This way I was able to setup downstream builds, fingerprinting, aggregate test results but the build failures were not bubbling to upstream job chain and upstream jobs were non blocking
I was finally able to achieve this using these plugins-
Build Pipeline
MultiJob Plugin
FingerPrint Plugin
Copy Artifacts Plugin
Join Plugin
I'm using Jenkins 1.514
System looks like this
Trigger Job --> build (and deploy) Job (1..n) ---> Test Job (1..n)
Trigger Job -
Create as MultiJob and create a fingerprint file in shell exec
echo date +%s > fingerprint.txt
Trick is that file needs to be archived during the build, to do that execute this script-
ARCHIVEDIR=$JENKINS_HOME/jobs/$JOB_NAME/builds/$BUILD_ID/archive
mkdir $ARCHIVEDIR
cp fingerprint.txt $ARCHIVEDIR
Create MultiJob Phase consisting of build/deploy job.
Build/deploy job is itself a multijob
follow the same steps for creating build/deploy job as above relative
to fingerprinting.
Copy the fingerprint.txt artifact from upstream job
Setup MultiJob phase in deploy job that triggers the test job
create a new fingerprint file and force archive it similar to above step
Collect Junit results in the final test job.
In the trigger Job, use Join Plugin to execute the Release Job by choosing 'Run Post Build Actions at join' and execute the release project only on stable build of Trigger Job.
This way all the steps are showing up in Build Pipeline view and Trigger job is blocking for all downstream builds to finish and sets its status as the worst downstream build to give a decision point for release job.
Multijob Plugin
If you'd like to stop the mess with downstream / upstream jobs chains definitions. Or when you want to add a full hierarchy of Jenkins jobs that will be executed in sequence or in parallel. Add context to your buildflow implementing parameter inheritance from the MultiJob to all its Phases and Jobs. Phases are sequential while jobs inside each Phase are parallel.
https://wiki.jenkins-ci.org/display/JENKINS/Multijob+Plugin

Resources