CI / CD via GitHub Actions - Just 2 Min - Copy Paste Done - 🤫
Why Use GitHub Actions for CI/CD?
GitHub Actions allows you to automate workflows directly in your repository. Benefits include:
- Fast Setup: Pre-built actions for testing, building, and deploying.
- Scalability: Works for small projects and large apps.
- Cost-Effective: Free for public repos and generous limits for private repos.
Let’s dive into two straightforward examples to get your apps deployed in no time.
Example 1: Deploying a Ruby on Rails App
This example sets up a CI/CD pipeline for a Rails app, running tests and deploying to a server via SSH.
Prerequisites
- A Rails app hosted on GitHub.
- A server with Ruby, Rails, and Nginx installed.
- SSH access to the server.
GitHub Actions Workflow
Create a file named .github/workflows/deploy-rails.yml
in your repository and paste the following:
name: Deploy Rails App
on:
push:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.2'
- name: Install dependencies
run: |
gem install bundler
bundle install
- name: Run tests
run: bundle exec rspec
deploy:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Deploy to Server
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
cd /path/to/your/rails/app
git pull origin main
bundle install
RAILS_ENV=production bundle exec rake db:migrate
touch tmp/restart.txt
Steps to Set Up
- Add Secrets: In your GitHub repo, go to Settings > Secrets and variables > Actions and add:
SERVER_HOST
: Your server’s IP or domain.SERVER_USER
: SSH username (e.g.,deploy
).SSH_PRIVATE_KEY
: Your SSH private key.
- Nginx Configuration: Ensure Nginx is proxying requests to your Rails app (e.g., via Puma or Passenger).
- Push to Main: Commit and push to the
main
branch to trigger the workflow.
This workflow runs tests with RSpec, and if they pass, it deploys the app by pulling the latest code, installing dependencies, running migrations, and restarting the server.
Example 2: Deploying a Node.js App with Nginx
This example deploys a Node.js app to a server with Nginx as a reverse proxy.
Prerequisites
- A Node.js app hosted on GitHub.
- A server with Node.js, PM2 (for process management), and Nginx installed.
- SSH access to the server.
GitHub Actions Workflow
Create a file named .github/workflows/deploy-node.yml
in your repository and paste the following:
name: Deploy Node.js App
on:
push:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
deploy:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Deploy to Server
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
cd /path/to/your/node/app
git pull origin main
npm install
pm2 restart app
Steps to Set Up
- Add Secrets: In your GitHub repo, add the same secrets as above (
SERVER_HOST
,SERVER_USER
,SSH_PRIVATE_KEY
). - Nginx Configuration: Set up Nginx to proxy requests to your Node.js app. Example config:
server {
listen 80;
server_name your-domain.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
- PM2 Setup: Install PM2 on the server (
npm install -g pm2
) and start your app (pm2 start app.js --name app
). - Push to Main: Commit and push to trigger the workflow.
This workflow runs tests, and if they pass, it deploys by pulling the latest code, installing dependencies, and restarting the app with PM2.
Tips for Success
- Test Locally First: Ensure your tests pass locally to avoid pipeline failures.
- Monitor Logs: Check GitHub Actions logs for errors during deployment.
- Secure Secrets: Never hardcode sensitive data in your workflow files.
- Optimize Nginx: Use caching and compression for better performance.
Need Help?
- Feel Free to connect buddy: I am just a call/message away 😊🤩.