Skip to content

Instantly share code, notes, and snippets.

@shaneparsons
Last active July 10, 2020 14:46
Show Gist options
  • Save shaneparsons/6bc1f2d3a1541272cb5a6f837acd484b to your computer and use it in GitHub Desktop.
Save shaneparsons/6bc1f2d3a1541272cb5a6f837acd484b to your computer and use it in GitHub Desktop.
Git / push to production workflow

Git / push to production workflow

From the remote server

Create the repo

mkdir /git && cd /git
git init --bare --shared [app].git

Create the post-receive hook file

cd [app].git/hooks
touch post-receive

Make the hook executable

chmod +x post-receive

Configure the hook

nano post-receive

Basic example

#!/bin/sh

# directories
APP_WEB_DIR="/var/www/[app]"
APP_GIT_DIR="/git/[app].git"

# checkout the last commit inside the web app directory
git --work-tree=${APP_WEB_DIR} --git-dir=${APP_GIT_DIR} checkout -f

Laravel example

#!/bin/sh

# directories
APP_WEB_DIR="/var/www/[app]"
APP_GIT_DIR="/git/[app].git"

# checkout the last commit inside the web app directory
git --work-tree=${APP_WEB_DIR} --git-dir=${APP_GIT_DIR} checkout -f

# run composer (NOTE: requires composer to be installed beforehand)
cd ${APP_WEB_DIR}
composer install
   
# ensure storage is writable
chmod -R g+w storage

# run optimizations
# php artisan config:cache
# php artisan route:cache

# do other things here if needed
# php artisan migrate

From the local repo

Go to the project folder

cd /path/to/your/project

Add the production server URL

git remote add production ssh://[user]@[domain]/git/[app].git

Push code to production

git push production master
@shaneparsons
Copy link
Author

shaneparsons commented Jul 10, 2020

Big thanks to this post by netgloo for opening my eyes to this simple workflow. I've been referring to it for over 5 years now, and figured I'd re-write it elsewhere just in case it ever got taken down.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment