Home

Automate GitHub to Linux Server Deployments with Bash Scripts

19 views

To automate deployment from GitHub to a Linux server using a Bash script, you can schedule the script to run regularly on your server, or trigger it manually. This approach is simpler and doesn't rely on external services like GitHub Actions, but it does require access to the server where the script will be executed. Here's a step-by-step guide:

Prerequisites

  • A GitHub repository.
  • A Linux server with SSH access to clone the repository.
  • Basic knowledge of SSH, Linux, Git, and scripting.

Steps

1. Set Up SSH Access

  1. Generate SSH Keys (if you haven't already) on your local machine:

    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    

    This will generate a public and a private key stored in ~/.ssh/id_rsa (private) and ~/.ssh/id_rsa.pub (public).

  2. Add the Public Key to GitHub:

    • Go to your GitHub repository.
    • Click on Settings > Deploy keys > Add deploy key.
    • Name it and paste the contents of your id_rsa.pub file. Select "Allow write access" if your script will push changes.
  3. Ensure Git is Installed on the server:

    sudo apt-get install git # For Debian/Ubuntu systems
    

2. Create a Bash Deployment Script

Create a new Bash script on your server (e.g., deploy.sh). Grant it execution permissions with chmod +x deploy.sh.

Here's a sample deploy.sh script:

#!/bin/bash

# Variables
REPO_URL="git@github.com:yourusername/yourrepo.git"
PROJECT_PATH="/path/to/project/on/server"
BRANCH="main"

# Navigate to the project directory (or clone if it doesn't exist)
if [ ! -d "$PROJECT_PATH" ]; then
  git clone -b $BRANCH $REPO_URL $PROJECT_PATH
fi

cd $PROJECT_PATH

# Pull the latest changes
echo "Pulling latest changes from $BRANCH branch..."
git fetch --all
git reset --hard origin/$BRANCH

# Optional: Restart service or perform additional deployment tasks
# Example for a Node.js project
# echo "Restarting server..."
# pm2 restart my-node-app

echo "Deployment complete!"

3. Run the Script

You can run this script manually by executing ./deploy.sh. For automatic deployments, you need to schedule this script using a cron job.

4. Schedule with Crontab

To trigger the deployment script regularly:

  1. Open the crontab editor:

    crontab -e
    
  2. Add a line to schedule the script. For example, to run the script every hour:

    0 * * * * /path/to/deploy.sh >> /path/to/deploy.log 2>&1
    

Considerations

  • SSH Keys: Ensure that your server's SSH keys have permission to access the GitHub repository.
  • Logging: Redirect output to a log file, as shown in the crontab entry, to help debug any issues.
  • Security: Be cautious with permissions for the script and SSH keys, ensuring they are only accessible where necessary.
  • Error Handling: Enhance the script with additional logging and error checking as needed.

This solution is suitable for cases where you prefer local server-side automation over cloud-based CI/CD workflows. It can also be combined with tools like inotify to detect changes in the repository filesystem if you prefer trigger-based automation over cron scheduling.