n8n

 What is N8N?

N8N stands for nodemation  (node-automation), 8 letters in between, just like k8s which abbreviates to Kubernetes.

Now as the name suggests in N8N everything is node to node automation, each node can represent a task, if you connect two nodes like node1 > node2, output of node1 can be reffered as input of node1

n8n is used in wide range of operations, you can also think of it as scheduling and posting content for content creator, for example, user can set a single video to be posted on Youtube, reddit, linkedln, instagram, facebook.  Analyzing and sending latest news topics to your mail or any unlimited possibilities of automation.

You have templates designed for  each use case, most of them are community driven, let us now see a use case of n8n in DevOps workflow, while it can be used in many scenarios, let us assume one common scenario of DevOps engineer

scenario:

A DevOps Engineer receives alerts from different teams notifying that their pipeline has failed.

DevOps Engineer asks for pipeline link to analyse but instead developer shares the repository link, devops engineer now navigates to pipelines and see there are many failed pipelines, some were testing pipelines which serve no purpose, and devops has to check every pipeline log to see what exact pipeline error is making developer worry, one easy way is to check pipeline where reported developer made commit,

Now considering this scenario, let's make it easier for DevOps engineer, as whenever pipeline is failed, it alerts all the DevOps enginners in particular slack/ Teams channels, also error is analyzed by AI and then AI suggesting the fix, also the link to pipeline and also commit message, this would make DevOps engineers work efficiently,


Let us achieve this task with N8N tool while learning about it, let us setup a demo repo, tools and code as pre requisites  to achieve this.

You need Docker Desktop, Github account, Slack accountVisual studio code

let us do the setup first and then, we will achieve the automation,

clone the following repo

command:

git clone https://github.com/sagarkakkalasworld/n8n-demo.git



lets see what repo consists of,


it has workflow file with one stage and four steps, checkout step checks out repository into github runner, node version install nodejs 20, and thirs step is npm istall, - we will not go deep into github actions, but if you want to deep dive into CI/CD tools, you can checkout my CI/CD course in Udemy - cicd-tools-real-world-devops-for-beginners-sagar-kakkala

here, when npm install is run, it default checks for package.json file to make build and if you want more insights on this, you can check Day-1: Deployment of React application in Nginx

since package.json file is missing, our pipeline fails here and also in next step, we added intentional failure step as we need failed pipelines for this Demo.

and here our application is as simple as "Hello world", its just output, since here we are not majorly focusing on application deployment



and now, lets see docker-compose file


this is a docker file where our n8n image is placed and we will be running n8n inside our docker container

and to know more about Docker - Day-2, Deployment of Docker 

and now before we install n8n in docker, make sure your docker desktop is running, simply open installed docker desktop in your device


Now, since it is a docker-compose file, run the following command, to run docker container in background

command: docker compose up -d


you can now see the containers running in docker desktop, you can navigate through docker desktop link of simply type localhost:5678 in your browser


and once you are in URL, provide a valid email ID as we need for verification and license key which is valid for 14 days


fill details as you prefer



click on send me a free license key


click on Activate license key


and now, you would be navigated to your n8n workflow server, click on New Workflow or Build your first workflow navigates to the same



Now let us start our n8n automation, you can click on plus at centre to search for available nodes or even plus at extreme left.

for our scenario, let us use webhook first, webhook in simple means when an action is done, trigger a reaction or send a message that particular event has occured, so here we will use webhook to check whenever pipeline is run.


once you click on webhook, you have these jsons methods and update inputs to POST and github-actions to make things easier for URL, and POST is when you want new ID to be updated and PUT is used when we want data to be appended.



and also you could find production URL at the top,we need production URL for this demo. and as you observe here URL has localhost, so to use localhost IPs, our github repo must as well run in our local system instead of github servers, since it isn't possible, as an alternate, we can make this localhost IP available to public using cloudfare

if you are using windows:

command:

winget install Cloudflare.cloudflared

if you are using MacOs:

command:

brew install cloudflared


and now, let us use cloudfare to explore our n8n, since n8n is running at our localhost:5678

use following command:

cloudflared tunnel --url http://localhost:5678


and from the screenshot, you can see a URL generated by cloudfare, let us use this URL.


as once navigated to URL, you will be able to see n8n here, now let us continue our workflow here and now in place of localhost:5678 , use domain name provided by cloudfare



so our url now changes from http://localhost:5678/webhook/github-actions to http://var-publicly-tires-features.trycloudflare.com/webhook/github-actions.


also notice that we are in editor mode, click on publish for this node to work





now push the cloned repo into your github repository, once cloned, go to settings


then webhooks, click on add wehbook and add webhook url of n8n node, except change localhost:5678 to cloudfare domain, something as shown http://var-publicly-tires-features.trycloudflare.com/webhook/github-actions


also select individual event and workflow runs, so whenever pipeline runs, this webhook gets triggered



so, our webhook is now configured, so according to webhook, whever workflows runs inside our github, it triggers n8n webhook, let us test, you can manually test this by adding a test file as workflow is designed in a way whenver there is a change in repo, pipeline gets triggered.


and to check if pipeline is running, go to Actions tab and as you could see pipeline failed as we intended



now lets check our n8n tab, and here go for excutions tab to check if the webhook has triggered or not. and you will observe node has triggered recently



and you can click on node to see, what are logs, output can be viewed in schema, json, table format and output of this node, can be served as input to next node

let us create if node, as once pipeline is run we want to check two conditions her, if pipeline run is completed and other condition is if pipeline had any error logs, if it does not have any error logs, we can ignore to reduce false alarms




and to know what exact conditons to add, add if block, publish and test the pipeline again, which makes our job easier to know what values to add. , and also make sure to click publish else it would not reflect any change


command:

{{ $json.body.action }} 

is equal to 

command:

completed

command:

{{ $json.body.workflow_run.conclusion }}

 is equal to 

command:

failure

or also as an alternate, to make it simpler, you can use Curl command 

command;

curl -X POST \

  http://localhost:5678/webhook-test/github-actions \

  -H "Content-Type: application/json" \

  -d '{"test":"hello","status":"failure"}'


this helps to add inputs for out next node.


let us make change in repo, to make our workflow run, since it is demo project, i am running in production and in real-time scenarios test-url is used,


and once commited, this triggers our pipeline, and now lets look at exceutables at n8n workflow

and now you will be able to see if block exceution


here on input, you can see in schema of what conditions shows it is completed and what condition shows pipeline is failed, you can simply drag and drop as shown in the video attached to this blog




now since our condition is working, once pipeline is failed, let us use http request to get the logs from the pipeline

click on add nodes for true in if block, you can leave false -no node attached to stop, reprat the process of adding test file to trigger pipeline, add test node to check outputs from previous node.


here for http request to get from our github account, we need, authentication

add this in URL : 

command:

{{ $('Webhook').item.json.body.workflow_run.url }}/logs 


and for authentication select predefined 



and select Github API



Now open a new tab and github token from your profile and steps were clearly shown during our argoCD session -argocd / or watch video attached to this blog to avoid confusion




and now place the token inside our http request node, and once http node is exceuted you will observe something like this



since, it is a zip file, let us extract and place logs in a cleanier way for us to pass on to AI agent

search for compression, and add decompressor node


and change output prefix to logs


each extratcted file would be named as log0, log1, log2 when you excecute


now search for code block, as we want to integrate all the logs into one place and then feed to AI



i am choosing code in Javascript, now click on Javascript and paste the following code in code block

command:

const item = $input.first();


let combinedLogs = '';


for (const [key, file] of Object.entries(item.binary ?? {})) {

  const buffer = await this.helpers.getBinaryDataBuffer(0, key);

  const text = buffer.toString('utf8');


  combinedLogs += `\n\n===== ${key} =====\n\n`;

  combinedLogs += text;

}


return [

  {

    json: {

      logs: combinedLogs,

    },

  },

];



now we got all the logs integrated


now search for AI agent node and in the node add the following prompt

command:

Analyze this failed GitHub Actions workflow.


Determine:

1. The root cause of the failure

2. The exact error that caused it

3. The failed job or step

4. Why the failure happened

5. The recommended fix


Ignore unrelated warnings and focus on the actual failure.


GitHub Actions logs:


{{ $json.logs }}



now add chat model, you can use openAI or claude as you prefer




generate API key here- and add in credentials- openAI API key


now , let us make changes in repo to trigger pipeline

now once you check AI Agent node, it shows root cause and summary - 


and now , we want to send this to our slack channel but before we send to slack channel, make sure you create slack bot and integrate to slack channel, for more refer video or you can refer the following blog- configure slack bots

create a slack channel


click on channel > Add agent or apps


select App that you created 



now get into n8n, search for slack node, and select send a message

add App bot token in credential


find slack channel ID back from Slack about section


now in n8n, paste the slack channel ID and also message

🚨 *GitHub Actions Failed*  *Repository:* {{ $('Webhook').item.json.body.repository.full_name }} *Workflow:* {{ $('Webhook').item.json.body.workflow_run.name }} *Branch:* {{ $('Webhook').item.json.body.workflow_run.head_branch }}  🔍 *AI Root Cause Analysis*  {{ $json.output }}  🔗 *GitHub Actions Run* {{ $('Webhook').item.json.body.workflow_run.html_url }}  🤖 Analyzed automatically by n8n


now try changing code in repo , to check use case, let us wait till all the execution is completed


and now once it is exceuted, you can observed slack channel that pipeline error and suggested fix generated and sent to our slack channel



we have successfully achieved automation for our use-case, and the below image being our complete workflow




Comments