Identify which downstream job failed and send notification in Jenkins - jenkins

I have created a wrapper job in Jenkins which will get triggered every hour if there are any new commits in my GIT repository. This wrapper job in turn calls 6 other downstream jobs. So the structure of my wrapper job (W) is like this:
W -> A -> B -> C -> D -> E -> F
I am using Jenkins Parameterized Trigger Plugin to stitch one job to the other so that my upstream jobs fail if the downstream job fails. Upon completion of the last downstream job (F), Wrapper job (W) is copying the artifacts from all the downstream jobs in its current workspace.
Now when one of my downstream job (lets say E) fails, I get failure notifications from the failed downstream job (E) as well as from all the other upstream jobs (D, C, B, A and W). So I get 6 mails in total and it creates some noise.
If I activate the email notification on only the Wrapper job (W), then I get a single failure notification mentioning that Job A has failed. Then I will check Job A's logs only to find out that it was Job B that failed and continue the log checks until I reach Job E.
How can I customize the notification to send a single mail identifying the specific downstream job (in this case E) that caused the failure?
OR
Is there a better way to trigger the downstream jobs, wait for all the downstream jobs to get completed and copy the artifacts from all the downstream jobs to the trigger job?

Wrote a Groovy script in Groovy Postbuild to iterate through all the subprojects of wrapper job and mark the wrapper job as failure if any of the subprojects have failed.
Also changed the exit criteria in "Trigger/call builds on other projects" to never mark job as failure/unstable. Instead the call of setting the job as failure is handled in the groovy script itself based on the status of the downstream subprojects.

Related

Jenkins pipeline how to detect downstream build status when using parallel()?

I have a pipeline that invokes a couple hundred runs of a downstream job in a parallel block. The downstream job contains a check that may abort the job.
How can I check the build status of the downstream jobs ran in the parallel block? Ideally, the parallel step returns the status of each downstream build in a map or something, but as far as I can tell it returns only a single build status. In my case, if downstream jobs have been SUCCESS, FAILURE, and ABORTED, the upstream build sets it's status as ABORTED.
currentBuild.status and currentBuild.currentResult both seem to have the wrong status set, and if I catch the exception thrown from the parallel step, it's just a hudson control flow exception that doesn't let me know the status of the downstream build.
What is the best way to get the correct downstream build status from jobs invoked from the parallel step?

How to get the status of child Jenkins Job from parent Jenkins job

I am currently running 2 Jenkins jobs where one Jenkins Job (Job A) calling another Jenkins Job (Job B) by using "when builds promote". Once it is approved manually, then Job B job will get triggered. After this step, I need the scenario like I would like to get status from Job B in Job A.
If my Job B fails then Job A should fail or vice versa. Any help!
You are missing the fundamentals of how Jenkins works. If an upstream job (job 1) is successful and the downstream job (job 2) fails, Jenkins is doing its job correctly by showing the actual statuses of the jobs (i.e., job 1 is green and job 2 is red).
If the upstream job fails then the downstream should never kick off in the first place, so you won't have to worry about that. This is how Jenkins was designed to work.

How to run jenkins job only once but with two conditions

Thanks for looking into my concern.
I have 3 jenkins jobs. JOb A, B & C.
Job A starts at 10PM at night.
JOB B is a down stream of Job A and runs only if job A is success.
Job C is a downstream job of job B
Now I want job C to be triggered after successful completion of job B or at at a scheduled time. Problem is if I schedule job C as down stream as well as with a schedule. It runs twice.
But, it should run only once.
Please help me to achieve this.
Did you try "Conditional BuildStep" plug-in? You can execute a downstream job (or a script) based on "Build cause"
You can add more than 1 "single" conditions for each build cause.
Now you'll need to decide when to run a job, as a timer or as a downstream
You can use jenkins pipeline plugin. You can create a pipeline job with stages. A pipeline will proceed only to next stage if previous stage is successful. Refer documentation for more details on pipeline.
Pipeline comes with a lot of flexibilities in which you can define the flow. You can either use a declarative pipeline or a scripted pipeline. Good number of examples can be found in here

how to trigger jenkins downstream job only both upstreams jobs successfully executed

I have created Job A which looks for upstream jobs (Job B and Job C) success result and triggers a shell script to verify a condition.
Once Job B and Job C executed successfully Job A executes downstream jobs (Job D and Job E).
I have used reverse (to configure upstream jobs) and downstream-ext (to configure downstream jobs) plugins in Job A using JJB.
Issue I am facing here is: After Job B is executed successfully without waiting for Job C result. Job A should wait for both Job B and Job C and then execute based on the result.
Could you please help me how to configure this scenario.
You can try using the Join Plugin, here is the documentation:
https://wiki.jenkins-ci.org/display/JENKINS/Join+Plugin
This'd be easier if you convert your A job to a build flow https://wiki.jenkins.io/display/JENKINS/Build+Flow+Plugin?focusedCommentId=60917290 or even better it's successor Pipeline 2.0 https://jenkins.io/doc/book/pipeline/
(Groovy) Code in A would then be something like:
if (build('scenario-B-Job') && build('scenario-C-Job')) {
build('scenario-E-Job')
build('scenario-D-Job')
}
You can also parallelize (B,C and then D,E) to shorten overall execution times if you have enough slaves around.

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