Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 49

Automating CI/CD Builds with n8n and Git Push Webhooks

Recommended resources

Introduction

In our last session, you mastered the use of the HTTP Request node to make authenticated API queries, specifically retrieving data from the GitHub API using a GET request. This was a crucial first step in learning how to interact with external CI/CD systems from n8n.

Today, we will build directly on that foundation. We will move from passively querying information to actively initiating processes. Your learning objective is to trigger a CI/CD build from within n8n, using the data provided by a Git push event. We will combine your knowledge of webhooks (from Module 9) with your new skills in using the HTTP Request node. By the end of this lesson, you will have a workflow that automatically starts a build in a CI/CD tool whenever new code is pushed to your repository, a core function in modern DevOps automation.


The CI/CD Triggering Pattern

The fundamental pattern for this automation is straightforward and powerful. It consists of two main parts:

  1. The Trigger: An n8n workflow starts when it receives a notification from your Git provider (like GitHub) that a push event has occurred. We typically use the dedicated GitHub Trigger node for this, which conveniently handles the webhook setup and payload parsing for you.
  2. The Action: The workflow takes information from the webhook payload—such as the repository name, branch, or specific commit SHA—and uses it to make an API call to your CI/CD tool, instructing it to start a new build.

This lesson will focus on the "Action" part, exploring how to trigger builds in two of the most common environments you're already familiar with: Jenkins and GitHub Actions.


Use Case 1: Triggering a Jenkins Build

Jenkins is a staple in the CI/CD world, and n8n provides a dedicated node that makes integration seamless. While you could use the generic HTTP Request node to call the Jenkins API, the dedicated Jenkins node simplifies the process by providing a clear user interface for common actions.

Let's explore how to use it to trigger a build. The article "Jenkins n8n Integration" provides an excellent guide.

Jenkins n8n Integration: 11 Actions to Automate CI/CD

This guide from Hackceleration covers the exact steps for connecting to Jenkins and triggering jobs.

First, read the section on connecting Jenkins to n8n. This involves generating an API token in Jenkins and creating a corresponding credential in n8n, a process that should be familiar. Next, review the descriptions for the Trigger Job action and, more importantly, the Trigger with Parameters action. The latter is essential for dynamic CI/CD pipelines, as it allows you to pass data from your GitHub webhook directly into your Jenkins job. Finally, confirm your understanding by reading the FAQ entry on triggering builds from GitHub webhooks.

Building the Jenkins Trigger Workflow

Now, let's assemble the workflow in n8n.

  1. Add a Trigger: Start with a GitHub Trigger node. Configure it with your GitHub credentials and specify the repository and the push event, just as you did in Module 9.
  2. Add the Action Node: Add a Jenkins node.
    • Credentials: Select the Jenkins credentials you just created.
    • Resource: Set this to Job.
    • Operation: Choose Trigger with Parameters. This is the key to a dynamic workflow.
    • Job Name or ID: Enter the name of the Jenkins job you want to trigger.
  3. Configure Parameters: This is where you connect the Git event to the Jenkins build. Under the Parameters section, you can add key-value pairs that will be passed to your Jenkins job. You can use expressions to pull these values directly from the GitHub Trigger node's output.

For example, if your Jenkins job accepts a parameter named GIT_COMMIT_SHA, you would:

  • Add a parameter in the n8n Jenkins node.
  • Set its Name to GIT_COMMIT_SHA.
  • For its Value, use the expression picker to select the commit hash from the webhook payload, which is often found at an expression like {{$json.after}}.

By executing this workflow, a push to your GitHub repository will now automatically start a parameterized Jenkins build with the exact commit SHA that triggered the event.


Use Case 2: Triggering a GitHub Actions Workflow

What if your CI/CD pipeline is already in GitHub Actions? You can trigger a workflow there as well. Unlike Jenkins, there isn't a dedicated "Trigger Workflow" action in the n8n GitHub node. Instead, we fall back to the powerful HTTP Request node you mastered in the previous lesson.

GitHub Actions has a special trigger event called repository_dispatch. This allows an external service, like n8n, to trigger a workflow by sending a POST request to a specific GitHub API endpoint.

The n8n blog provides a concise guide on this exact topic.

How to set up a no-code CI/CD pipeline with GitHub and TravisCI – n8n Blog

This article explains how to use the HTTP Request node to trigger a GitHub Action.

Focus on the section Trigger GitHub Actions. It outlines all the necessary parameters for the HTTP Request node: the authentication method, the POST request method, the specific API URL, and the body parameters required to specify the event type.

Building the GitHub Actions Trigger Workflow

The workflow structure is very similar to the Jenkins example.

  1. Configure Your GitHub Action: First, ensure the target workflow file in your GitHub repository is configured to be triggered by repository_dispatch. It looks like this in your YAML file:

    on:
      repository_dispatch:
        types: [build-triggered-by-n8n]
    
  2. Set Up the n8n Workflow:

    • Trigger: Start with a GitHub Trigger node listening for the push event.
    • Action: Add an HTTP Request node.
      • Authentication: Set this to Generic Credential Type, select Bearer Auth, and choose the GitHub PAT credential you created in the last lesson.
      • Method: POST.
      • URL: Use the GitHub API endpoint: https://api.github.com/repos/{owner}/{repo}/dispatches. Replace {owner} and {repo} with the correct values, which you can even pull dynamically from the trigger node's output.
      • Body: Switch to JSON/RAW and send a JSON body. You must specify the event_type that you defined in your workflow's on: trigger. You can also pass additional data in a client_payload object.
        {
          "event_type": "build-triggered-by-n8n",
          "client_payload": {
            "commit": "{{$json.after}}",
            "branch": "{{$json.ref}}"
          }
        }
        
        Notice how we are embedding expressions directly in the JSON body to pass the commit SHA and branch reference from the webhook into the GitHub Actions workflow.

When this n8n workflow runs, it sends a repository_dispatch event to your repository, which in turn triggers the corresponding GitHub Actions workflow and passes along the commit and branch information in the client_payload.


Conclusion

You have now successfully bridged the gap between a Git event and a CI/CD build, a fundamental task in DevOps automation. You've seen how to accomplish this for two different CI/CD platforms, giving you a flexible toolkit for your projects.

Key Takeaways:

  • CI/CD builds can be triggered from n8n by making a POST API call to the CI/CD tool.
  • For tools with dedicated n8n nodes like Jenkins, use the node's built-in actions (e.g., Trigger with Parameters) for a streamlined setup.
  • For tools like GitHub Actions, or any other tool with a REST API, use the HTTP Request node to make a POST request to the appropriate endpoint (e.g., the repository_dispatch endpoint for GitHub).
  • The key to creating dynamic and useful pipelines is to pass data from the trigger event (like a webhook payload) as parameters or a request body in the action node.

In our next lesson, we will address the next logical question: after triggering a build, how do we know if it succeeded? You will learn how to poll a CI/CD API to monitor the status of a running job and build workflows that can react to the outcome.