Cancel All Jobs Triggered by Another Job in Jenkins
If you are using Jenkins as your continuous integration and continuous delivery tool, you may encounter situations where you need to cancel all jobs triggered by another job. This can be useful when you want to stop the execution of multiple jobs that were triggered by a single job. In this article, we will discuss how to cancel all jobs triggered by another job in Jenkins.
Understanding Jenkins Jobs
In Jenkins, a job is a unit of work that can be scheduled and executed. Jobs can be triggered manually or automatically based on certain events or conditions. When a job is triggered, it can also trigger other jobs as part of a build pipeline or workflow.
When a job triggers other jobs, it creates a chain of job executions. If you want to cancel all jobs triggered by a particular job, you need to find a way to identify and stop these chained executions.
Cancelling Jobs in Jenkins
Jenkins provides a way to cancel individual job executions from the Jenkins web interface. However, cancelling multiple jobs manually can be time-consuming and error-prone. To cancel all jobs triggered by another job, we need to use Jenkins API and a script.
Here is an example script that cancels all jobs triggered by a specific job:
import jenkins
import requests
def cancel_triggered_jobs(job_name):
server = jenkins.Jenkins('http://jenkins-server-url')
job_info = server.get_job_info(job_name)
downstream_jobs = job_info['downstreamProjects']
for job in downstream_jobs:
job_url = job['url'] + 'lastBuild/stop'
requests.post(job_url)
In this script, we use the Jenkins API to retrieve information about the specified job. We then extract the list of downstream jobs triggered by the specified job. Finally, we iterate over the downstream jobs and send a POST request to the Jenkins API to stop the execution of each job.
You can save this script as a Python file and run it using a Python interpreter or execute it as a Jenkins job. Make sure to replace 'http://jenkins-server-url' with the URL of your Jenkins server and provide the name of the job you want to cancel its triggered jobs.
By running this script, you will be able to cancel all jobs triggered by another job in Jenkins.
Cancelling all jobs triggered by another job in Jenkins can be a useful feature when you need to stop the execution of multiple jobs. By using the Jenkins API and a script, you can automate the process and save time. In this article, we discussed how to cancel all jobs triggered by another job in Jenkins using a Python script. Hopefully, this information will help you in managing your Jenkins jobs efficiently.
| Reference | Link |
|---|---|
| Jenkins API Documentation | https://www.jenkins.io/doc/book/using/remote-access-api/ |
| Python Requests Library Documentation | https://docs.python-requests.org/en/latest/ |