Skip to content

Instantly share code, notes, and snippets.

@guychouk
Last active January 21, 2024 05:12
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save guychouk/db0061b989141f55381a66b046adc2c3 to your computer and use it in GitHub Desktop.
Save guychouk/db0061b989141f55381a66b046adc2c3 to your computer and use it in GitHub Desktop.
Simple code deployment via a Github Action that uses git and rsync.

Simple Deployment 🚀

  • Install nginx and rsync on the server (using Debian as an example):
apt install -y nginx rsync 
  • Add the github user:
adduser --disabled-password --gecos "" --home /home/github github
  • Add github user to the www-data group created by nginx:
usermod -g www-data github
  • Generate a public and private SSH keys, add the public key to the /home/github/.ssh/authorized_keys file.
  • Add a DEPLOY_KEY Secret in Github which contains the github user's private key.
  • Set proper permissions:
chown -R github:www-data /home/github/.ssh
chown -R www-data:www-data /var/www/html
chmod -R 770 /var/www/html
  • Put the deploy.yml workflow in the .github/workflows dir at the root of the project. This Github Action is triggered on every merge to the main branch.

  • Change the <HOST> placeholder in the remote_host key to the address of your server (make sure your firewall rules allow ssh access)

  • The build job in the deploy.yml uses rsync to upload the contents of the src/ dir to the destination server at the specified remote_path (/var/www/html).

name: CD
# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches: [ main ]
pull_request:
branches: [ main ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
# Deploy using rsync to server
- name: rsync deployments
uses: burnett01/rsync-deployments@4.1
with:
# -v: verbose
# -z: compress files during transfer
# -r: copy files recursively
# --delete: delete extraneous files from dest dirs
switches: -vzr --delete
path: src/
remote_path: /var/www/html/
remote_host: <HOST>
remote_user: github
remote_key: ${{ secrets.DEPLOY_KEY }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment