Trigger another job if the existing build fails with timeout - jenkins

In Jenkins, I have used the Build-time plugin to timeout and abort the build if the build takes more than a certain specified time.
Is there a way to trigger another job immediately when this particular job fails with timeout where I can make my work process more efficient.

You can accomplish this by installing the Parameterized Trigger Plugin on Jenkins.
(https://wiki.jenkins.io/display/JENKINS/Parameterized+Trigger+Plugin)
Once installed, as a Post-build Action, add the Trigger parametrized build on other projects, from here you can select the Failure option that you need to trigger the next job.

Related

Jenkins: distinguish CI build from scheduled build

All my Jenkins jobs are triggered both by a Github webhook, but also via a scheduled build one per week. The build process is heavily cached to make the webhook CI builds finish quickly.
I would like to add a line to my build script which wipes the cache during the weekly scheduled build, and make it build from scratch. Is there a variable in the build script to identify if a build was triggered by a webhook or schedule?
Maybe the envInject plugin will give you what you need?
This plugin also exposes the cause of the current build as an
environment variable. A build can be triggered by multiple causes at
the same time e.g. an SCM Change could have occurred at the same time
as a user triggers the build manually.
The build cause is exposed as a comma separated list:
BUILD_CAUSE=USERIDCAUSE, SCMTRIGGER, UPSTREAMTRIGGER, MANUALTRIGGER
In addition, each cause is exposed as a single envvariable too:
BUILD_CAUSE_USERIDCAUSE=true
BUILD_CAUSE_SCMTRIGGER=true
BUILD_CAUSE_UPSTREAMTRIGGER=true
BUILD_CAUSE_MANUALTRIGGER=true

How to trigger a Jenkins Job from outside and receive the status of build

I need to trigger a Job from another Scheduler and want to receive the status of the triggered Job after it finishes.
Sure it would be possible to create a status file or such, but it would be convenient to trigger the Job from a script or per httpRequest and wait for some kind of exit or return code.
Is that possible?
For this, you have to use Build Authorization Token Root Jenkins plugin
It will allow you to run Jenkins build remotely.
Check this image
Please check my article : https://medium.com/appgambit/trigger-jenkins-job-from-slack-5b07b6131e25
But yes you can skip slack integration and just use API for triggering particular job.
Or if you want to run your job after another job is complete or success you have to use "Build after other projects are built"
Build after other projects are built

how to trigger Jenkins job from ant script running on same Jenkins server

I'm running an ant job that runs several thing on master node and need to trigger several jobs on slave servers based on the options i select from the main job parameters
is there a way to call another job from within ant script without using jenkins-cli.jar as external command
You can trigger Jenkins jobs by doing an HTTP request:
Go to your Job Configuration
Build triggers > Check 'Trigger Builds Remotely' and think of an access token, e.g. SOME_SECURE_TOKEN.
In your ant script: execute a POST request to JENKINS_URL/job/JOB_NAME/build?token=SOME_SECURE_TOKEN
Note that if you have authentication in place, you need to set up a user who is authorized to start the other jobs. In that case read this more detailed explanation: https://www.nczonline.net/blog/2015/10/triggering-jenkins-builds-by-url/
Another solution would be to use the Parameterized Trigger Plugin to trigger builds from a Jenkins step. You mention that the jobs that need to be triggered may depend on the job parameters. In that case you can combine the Conditional Buildstep plugin with the Parameterized Trigger plugin.

Run jenkins job without triggering downstream jobs

I have a jenkins job with a couple of downstream jobs which are triggered upon the job finishing correctly.
There are times when I want to run the initial job without triggering the downstream jobs. Is this possible?
It sounds like the conditional build step plugin might be of help. You can configure it to trigger other jobs based on various conditions, like so:
Here, the conditional build step has been configured to run downstream-job if foo.txt exists in the current workspace.

How to trigger a job, If the currently running job failed?

In my task I need to trigger a same job if its current build failed.
I don't want the trigger if the build got succeeded.
Is there any plugins or any other method available to do this task?
You can use Downstream Ext Plugin for this:
my_project will be triggered only if this build fails.
Note: if you want to trigger the same job, you should realize that this is a chance to have an infinitive loop. If the build always fails, it will be triggered over and over again...
The best solution is to use Naginator Plugin.
If the build fails, it will be rescheduled to run again after the time you specified. You can choose how many times to retry running the job. For each consecutive unsuccessful build, you can choose to extend the waiting period.
Jenkins Naginator Plugin
Jenkins Naginator Plugin can be used to automatically reschedule a build after a failure.
This becomes very useful in scenarios where the build fail due to unavoidable reasons like a database connectivity is lost, the file system is not accessible etc.
Configurations
Rescheduling configuration is available as a post-build-action. There are a number of configurations for you to pick correctly based on your expected (unavoidable) build failure reason.
Read further on the configurations here with a screenshot.

Resources