Automating n8n Setup and Deployment with GitHub Actions and EC2

Automating n8n Setup and Deployment with GitHub Actions and EC2

Introduction

n8n is a powerful workflow automation tool that enables users to connect various services and automate tasks through a user-friendly interface. In this guide, we'll explore how to set up n8n using GitHub Actions for continuous integration and continuous deployment (CI/CD), and then deploy it to an EC2 server.

PROJECT URL

Prerequisites

  1. GitHub repository for your n8n project.

  2. Access to an AWS EC2 instance.

  3. Docker and Docker Compose are installed on your EC2 instance.

  4. Basic knowledge of GitHub Actions, Docker, and n8n.

Step 1: Setting Up GitHub Actions

a. Workflow File

Create a .github/workflows/main.yml file in your GitHub repository to define the GitHub Actions workflow.

name: CI/CD for n8n

on:
  push:
    branches:
      - main

jobs:
  build:
    name: Build Docker Image and Push into Docker-Hub
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Login to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          context: .
          file: ./Dockerfile
          push: true
          tags: ${{ secrets.DOCKER_USERNAME }}/n8n:${{ github.sha }}


      - name: Deploy to n8n Server
        run: |
          docker-compose -f docker-compose.yml up -d

      - name: Deploy to EC2
  run: |
    ssh -i ${{ secrets.SSH_PRIVATE_KEY_PATH }} ubuntu@13.232.212.71 'bash -s' << 'ENDSSH'
      docker pull ranawattech/n8n:${{ github.sha }}
      docker stop ranawattech/n8n || true
      docker rm ranawattech/n8n || true
      docker run -d -p 80:80 --name ranawattech/n8n ranawattech/n8n:${{ github.sha }}
    ENDSSH
  env:
    SSH_PRIVATE_KEY_PATH: ${{ secrets.SSH_PRIVATE_KEY_PATH }}

b. Docker Image

Ensure your n8n instance is set up to use Docker and update the Dockerfile accordingly.

FROM n8nio/n8n:latest

COPY . .

CMD ["n8n"]

c. Docker Compose File (docker-compose.yml)

version: '3'

services:
  n8n:
    build: .
    ports:
      - "5678:5678"
    volumes:
      - ./data:/home/node/.n8n
    environment:
      - N8N_BASIC_AUTH_ACTIVE=true
      - N8N_BASIC_AUTH_USER=GovindSingh
      - N8N_BASIC_AUTH_PASSWORD=Singh@9447

d. GitHub Secrets

Add DOCKER_USERNAME ,DOCKER_PASSWORD and SSH_PRIVATE_KEY_PATH as secrets in your GitHub repository settings.

Step 2: Deploying to EC2

a. SSH Key

Generate an SSH key pair for secure communication between GitHub Actions and your EC2 instance.

ssh-keygen -t rsa -b 2048 -f github_actions_ec2 -N ""

Add the public key (github_actions_ec2.pub) to your EC2 instance's ~/.ssh/authorized_keys.

b. Update GitHub Secrets

Add the private key (github_actions_ec2) as a secret named SSH_PRIVATE_KEY in your GitHub repository settings.

c. Update Workflow File

Add a new job to deploy n8n to your EC2 instance.

- name: Deploy to n8n Server
  run: |
    docker-compose -f docker-compose.yml up -d

- name: Deploy to EC2
  run: |
    ssh -i ${{ secrets.SSH_PRIVATE_KEY_PATH }} ubuntu@13.232.212.71 'bash -s' << 'ENDSSH'
      docker pull ranawattech/n8n:${{ github.sha }}
      docker stop ranawattech/n8n || true
      docker rm ranawattech/n8n || true
      docker run -d -p 80:80 --name ranawattech/n8n ranawattech/n8n:${{ github.sha }}
    ENDSSH
  env:
    SSH_PRIVATE_KEY_PATH: ${{ secrets.SSH_PRIVATE_KEY_PATH }}

Conclusion

With this setup, each push to the main branch triggers a GitHub Actions workflow that builds and pushes the n8n Docker image. After a successful build, it deploys the updated n8n instance to your EC2 server.

This CI/CD pipeline ensures a seamless development and deployment process for your n8n workflows.