Whatsapp

CI/CD Pipeline with GitHub Actions and VPS Deployment

CI/CD Pipeline with GitHub Actions and VPS Deployment

Introduction

In today's fast-paced development environment, automating software deployment is crucial. A CI/CD pipeline helps developers streamline the process of integrating and deploying code with minimal human intervention. GitHub Actions offers a powerful way to automate workflows, including building, testing, and deploying applications to a Virtual Private Server (VPS).

This guide will walk you through setting up a CI/CD pipeline using GitHub Actions to deploy applications seamlessly to a VPS. By the end, you'll have an efficient system for automating deployments and ensuring software stability.


What is a CI/CD Pipeline?

A Continuous Integration/Continuous Deployment (CI/CD) pipeline is a series of automated steps that ensure code changes are tested, integrated, and deployed efficiently. The main stages include:

  • Continuous Integration (CI): Merging and testing code changes automatically.

  • Continuous Deployment (CD): Deploying tested code to production or staging environments without manual intervention.


Why Use GitHub Actions for CI/CD?

GitHub Actions is a built-in automation tool in GitHub that allows developers to create workflows. It provides:

  • Seamless integration with GitHub repositories.

  • Scalability and custom workflows.

  • Pre-configured actions for testing, building, and deploying applications.

  • Integration with third-party tools like AWS, Docker, and Kubernetes.


Setting Up GitHub Actions for CI/CD

Step 1: Creating a GitHub Actions Workflow

To create a GitHub Actions workflow, navigate to your repository and create a .github/workflows directory. Inside this directory, create a new YAML file, such as deploy.yml.

name: Deploy to VPS

on:
  push:
    branches:
      - main

env:
  SERVER_IP: ${{ secrets.SERVER_IP }}
  USERNAME: ${{ secrets.USERNAME }}
  PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }}

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up SSH
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.PRIVATE_KEY }}" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan -H ${{ secrets.SERVER_IP }} >> ~/.ssh/known_hosts

      - name: Deploy to VPS
        run: |
          ssh ${{ env.USERNAME }}@${{ env.SERVER_IP }} "cd /var/www/app && git pull && npm install && pm2 restart all"

Step 2: Configuring GitHub Secrets

To securely store your VPS credentials, navigate to your GitHub repository:

  1. Go to Settings > Secrets and variables > Actions.

  2. Add the following secrets:

    • SERVER_IP: Your VPS IP address.

    • USERNAME: Your SSH username.

    • PRIVATE_KEY: Your private SSH key.

Step 3: Automating Deployment

Whenever you push code to the main branch, GitHub Actions will automatically execute the workflow and deploy your application to the VPS.


VPS Deployment Best Practices

1. Secure Your VPS

  • Use SSH keys instead of passwords.

  • Set up a firewall (e.g., UFW for Ubuntu).

  • Regularly update software and security patches.

2. Use a Process Manager

Tools like PM2 or Supervisor ensure that your application runs continuously:

pm install pm2 -g
pm start
pm2 start app.js --name my-app
pm2 save
pm2 startup

3. Monitor Application Logs

To troubleshoot deployment issues, monitor logs using:

pm2 logs
journalctl -u nginx --follow

Benefits of Using CI/CD with GitHub Actions and VPS

  • Automated Deployments: Reduces manual work.

  • Faster Development Cycles: Enables rapid code updates.

  • Improved Code Quality: Ensures tested code is deployed.

  • Scalability: Can be adapted for larger applications.


Conclusion

Implementing a CI/CD pipeline using GitHub Actions and deploying applications to a VPS ensures seamless automation, better software quality, and faster deployments. By following this guide, you can streamline your development workflow and focus on building great applications.

For more insights on DevOps best practices, check out GitHub Actions Documentation and DigitalOcean VPS Guide.

By leveraging GitHub Actions and following best practices, you can create a robust deployment pipeline tailored to your needs!

Get in Touch with us