The idea:
- push your repo change into Github
- production site pulls your change from Github on commit
The key idea is that Git implements hooks that are scripts that fire on certain Git events. Every Git installation supports them, and Github the Site implements something similar, calling them "Github Actions" that is stored in the .github/workflows directory in the repo. It extends beyond single repo actions and into continuous integration/deployment I think.
(V1) Direct Deploy without Github Workflows.
First let's look at the case of Direct Deploy without Github Workflows. Studying this tutorial reveals:
- repo/hooks/ contains scripts
- list of hooks: https://git-scm.com/docs/githooks
- The hook we want is post-receive
Step 1: Set up Bare Repo on Production
cd /repo/dir
mkdir project
cd project
git init --bare
Add a script that is in repo-folder/hooks/post-receive that pulls the update:
#!/bin/sh
NOW=$(date +'%d-%m-%Y_%T')
# check out master branch
GIT_WORK_TREE=/path/to/destination/folder/htdocs git checkout -f master
# custom steps for deployment
# For example, let's execute composer to refresh our dependencies :
cd /path/to/destination/folder/htdocs
composer update
# backup current version, in case we need to do a rollback
cp -R /path/to/destination/folder/htdocs/. /backups/project-name/$NOW/
Then chmod +x post-receive
Step 2: Add Production Repo to Local Dev
cd ~/my-working-copies
git remote add production ssh://username@myserver.com:22/repo/dir/project
git remove -v # check that it's available
Step 3: Add stuff to your Local Dev and Push Changes!
When you do git push production master this will push to the production remote into branch master. This makes the changes available. After the push is received, it will fire the post-receive hook script, which does any other configuration necessary (in this case, composer update is a PHP cli for updating dependencies)
(V2) Deploy from Local through Github to Production
This is called "Continuous Deployment" (CD). This article describes how to use a Github Action to rsync to a production server, which is basically what we want.