Introduction
In our last lesson, you learned how to build robust notification workflows that report the status of your CI/CD jobs, closing the communication loop with your team. While knowing the outcome is crucial, the true power of automation lies in taking the next step: acting on that information without human intervention.
This lesson serves as the bridge from passive notification to active response. We will focus on how to configure a trigger for deployment failure detection. This is the critical starting point for more advanced automation, such as automated rollbacks or incident creation. We will explore the two primary architectural patterns for this:
- Webhooks (Push): An event-driven approach where your CI/CD tool notifies n8n immediately upon failure.
- API Polling (Pull): A scheduled approach where n8n periodically checks the status of a job to detect failures.
Your familiarity with CI/CD tools like Jenkins will be valuable here, as you'll recognize these patterns as fundamental ways systems interact. By the end of this lesson, you will be able to choose and implement the appropriate failure detection strategy for your pipelines.

Method 1: Failure Detection with Webhooks
The most efficient and real-time method for failure detection is using webhooks. In this "push" model, the CI/CD system itself is responsible for sending a notification to a specific n8n endpoint the moment a job fails. Most modern CI/CD tools, including Jenkins, GitHub Actions, and GitLab CI, support this through built-in features or plugins.
The following article describes an advanced "CI/CD Pipeline Health Monitor" that is triggered by this exact mechanism. It's a great illustration of the professional-grade systems you can build.
Top 10 n8n Workflows: Automate Dev Tasks Without Extra ...
This article from The Bit Forge details a sophisticated workflow for monitoring CI/CD pipelines, starting with a webhook trigger for pipeline completion events. It highlights how to move beyond simple failure alerts to intelligent, context-aware monitoring.
Please read the introduction and the section titled How This Workflow Actually Works. Pay particular attention to the first step, "Receive Pipeline Event," which describes the webhook trigger as the starting point for the entire process.
As the article explains, the workflow starts when your CI system sends a webhook. Let's look at how to set this up in n8n.
Setting Up the Webhook Trigger
To receive data from an external system like Jenkins, you use the Webhook node in n8n. This node generates a unique URL that you then configure in your CI/CD tool to call upon build failure. The following video provides a clear, step-by-step guide on configuring the generic Webhook node, which is exactly what you need for this use case.
n8n Webhook (Step-By-Step Guide)
This video from Kia Ghasem demonstrates how to set up and configure the generic Webhook trigger node in n8n.
Watch the section from setting up the webhook. Focus on these key configuration points: HTTP Method: For receiving failure data from a CI tool, you will almost always use POST. Path: This defines the unique endpoint for your URL. Use something descriptive, like jenkins-failure-prod-api. Authentication: For any production-level workflow, you must secure your webhook. The video explains how to use Header Auth, which is a standard and secure method.
Once you've configured the Webhook node, n8n provides two URLs: one for testing and one for production. You would copy the production URL and configure it in your Jenkins job's post-build actions, typically using a plugin like the "HTTP Request Plugin" or a dedicated webhook feature. You would configure Jenkins to send a POST request to this URL, often with a JSON body containing details about the failed build.

When you are setting this up, you can use the "Listen for Test Event" button in the Webhook node. Then, you can manually trigger a failed build in Jenkins. n8n will capture the incoming data, allowing you to see the exact structure of the payload and build the rest of your workflow using real data.
Method 2: Failure Detection via API Polling
What if your CI/CD tool doesn't support webhooks for build failures, or you need to monitor a resource that doesn't emit events? In these cases, you can use API polling. In this "pull" model, an n8n workflow runs on a schedule, actively queries the CI/CD tool's API for the status of a job, and then determines if a new failure has occurred.
This approach requires more setup but is incredibly versatile. The n8n blog has an excellent article explaining the fundamental concepts of polling.
Creating triggers for n8n workflows using polling
This n8n blog post explains the concept of polling and contrasts it with webhooks. It then provides a practical guide on how to build a polling workflow in n8n.
First, read the introduction to solidify your understanding of the "pull" model. Next, review the section How to poll in n8n, which introduces the core components: a schedule trigger, a node to fetch data, and a Function node (which we'll adapt to a Code node for Python) for logic. Finally, look at the code example under the section Function node. The JavaScript logic here is key: it compares incoming data with data from the previous run to find what's new. We will replicate this pattern in Python.
Building a Polling Workflow
A polling workflow consists of three main parts:
- Schedule Trigger (
ScheduleorCronnode): This node initiates the workflow at a regular interval (e.g., every 5 minutes). - Fetch Data (
HTTP Requestnode): This node calls the API of your CI/CD tool to get the status of the latest build. For Jenkins, you might make aGETrequest to an endpoint likehttps://your-jenkins.com/job/your-job/lastBuild/api/json. You would configure authentication using the methods we've covered previously. If you need a refresher on theHTTP Requestnode, the comprehensive video guide from Ryan & Matt Data Science (LINK) is an excellent resource. - Detect New Failures (
Codenode): This is where the intelligence lies. A simple check for a "FAILURE" status isn't enough, as you would trigger the workflow repeatedly for the same failure. You need to detect new failures.
Given your proficiency in Python, you can implement this logic in a Code node. The goal is to compare the current build status with the last build you checked. You can store the ID of the last-checked build using n8n's static data feature.
Here is a Python-based conceptual implementation for the Code node:
# The workflow's static data is available via this dictionary.
# It persists between workflow executions when the workflow is active.
static_data = get_workflow_static_data("node")
# Assume the HTTP Request node is named 'GetJenkinsStatus'
# and its output is the first input item to this Code node.
current_build = items[0].json
last_checked_build_number = static_data.get("last_checked_build_number", 0)
new_failures = []
# Check if this is a new build and if it has failed.
if current_build["number"] > last_checked_build_number:
# Update the static data with the latest build number we've processed.
static_data["last_checked_build_number"] = current_build["number"]
if current_build.get("result") == "FAILURE":
# This is a new failure, so we pass it on for processing.
new_failures.append(items[0])
# Return only the new failures. If there are none, the
# workflow branch will stop here.
return new_failures
This code ensures that you only trigger the rest of your workflow (e.g., sending a Slack alert, creating a Jira ticket) once per unique failure.
Comparing the Approaches
Both webhooks and polling are valid methods for failure detection, and the best choice depends on your specific context.
| Feature | Webhook (Push) | API Polling (Pull) |
|---|---|---|
| Latency | Real-time or near real-time. | Delayed by the polling interval (e.g., up to 5 minutes). |
| Efficiency | Highly efficient. No unnecessary requests. | Less efficient. Makes API calls even when no change. |
| Setup | Simpler in n8n; requires configuration in source system. | More complex in n8n; no changes to source system needed. |
| Use Case | Ideal for systems that emit events (e.g., Jenkins). | Necessary for systems without webhooks or for checking state. |
| Risks | Missed events if n8n is down. | Can hit API rate limits if the interval is too short. |
Conclusion
You have now learned the two fundamental patterns for triggering workflows based on deployment failures. This capability elevates your automation from simple notification to intelligent, proactive response.
Key Takeaways:
- Webhook (Push) triggers are real-time and efficient, making them the preferred method when supported by your CI/CD tool. You configure them using the
Webhooknode. - API Polling (Pull) triggers are versatile and work with any system that has an API, but they introduce latency and are less efficient. You build them using a
Scheduletrigger, anHTTP Requestnode, and aCodenode for stateful logic. - Stateful Polling is essential to avoid repeated triggers for the same failure. The
getWorkflowStaticData()method in aCodenode allows you to store information between executions. - The choice between these methods is a key architectural decision based on the capabilities of your tools and the requirements of your pipeline.
In our next lesson, we will put this failure detection trigger to work. You will learn how to build a rollback workflow that executes upon a detected deployment failure, creating a powerful, self-healing component for your CI/CD pipeline.