Skip to content

Instantly share code, notes, and snippets.

@mariusvw
Forked from anlutro/install.bash
Created July 5, 2018 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariusvw/cd4678bb1089cea4b5e5c20892bf3446 to your computer and use it in GitHub Desktop.
Save mariusvw/cd4678bb1089cea4b5e5c20892bf3446 to your computer and use it in GitHub Desktop.
Simple Laravel 4 deployment using git hooks and shell scripts

On the server where your project should be deployed:

  1. Create a bare repository somewhere all contributors have read/write access. git init --bare /path/to/project.git

  2. git clone into the path where you want the project files to be cloned - for example, git clone /path/to/project.git /var/www/myproject.

Repeat step 2 if you want to have multiple setups - for example, one for production, one for staging.

  1. Open /path/to/project.git/hooks/post-update in your favourite text editor and paste in the bash script above. Make sure to chmod +x the file.

On your local machine:

  1. Initialize a repository for your project, add all the files you want commited, remember to chmod +x bin/install.bash and make sure the file permissions is reflected in the repository. Put the install.bash script in a bin directory in the project root and add it to the git repository - make sure its permissions are set to executable in the git repository as well.

  2. git remote add server myuser@my.server.host:/path/to/project.git && git push server master

You should at this point see a bunch of messages from the remote indicating the progress of the deploy script. If nothing is happening, you probably forgot to chmod +x the post-update file. If you get any errors, debug and fix them.

#!/usr/bin/env bash
set -e
# function to easily determine if a set of programs and files are available.
function deploy_programs_available {
local programs=(npm bower grunt)
local files=(package.json bower.json Gruntfile.js)
for p in ${programs[@]}; do
if ! hash "$p" 2>/dev/null; then return 1; fi
done
for f in ${files[@]}; do
if [[ ! -f "$f" ]]; then return 1; fi
done
return 0
}
function main {
local dev; if [[ $1 = "dev" ]]; then dev=true; fi
# make sure we're in the right directory
if [ ! -f composer.json ]; then
echo "File composer.json not found, aborting"
exit
elif [ ! -f bin/artisan ]; then
echo "File bin/artisan not found, aborting"
exit
fi
# download or update composer
if [ ! -f bin/composer.phar ]; then
curl -sS https://getcomposer.org/installer \
| php -- --filename="bin/composer.phar"
else
php bin/composer.phar self-update
fi
# bring the application down for maintenance. avoid the artisan command
touch app/storage/meta/down
echo "Application is now down."
# remove compiled/cache files if they exist
rm -f bootstrap/compiled.php
rm -f app/storage/meta/services.json
# install packages, this also regenerates the autoloader
if [[ $dev ]]; then
php bin/composer.phar install --no-scripts
else
php bin/composer.phar install --no-scripts --no-dev --no-interaction --prefer-dist
fi
# display the environment
php bin/artisan env
# run any outstanding migrations
php bin/artisan migrate --force
if [[ ! $dev ]]; then
# optimize autoloader and compile common classes
php bin/artisan optimize --force
fi
# if npm, grunt and bower are all installed, run them
if deploy_programs_available; then
npm install
bower install
grunt
else
echo "WARNING: npm, grunt or bower not installed -"
echo " assets must be uploaded manually!"
fi
# and bring the application back up
rm -f app/storage/meta/down
echo "Application is now live."
}
main $@
#!/usr/bin/env bash
set -e
function do_git_update {
local trg_path="$1" # path to pull into
local branch="$2" # name of the branch to pull from
local remote="origin" # name of the remote to pull from
cd $trg_path
unset GIT_DIR
git checkout $branch
git remote update
# check if there have been any new commits to the branch
if [[ $(git rev-list --max-count=1 $branch) != $(git rev-list --max-count=1 $remote/$branch) ]]
then
echo "**** Pulling $branch into $trg_path"
git merge --ff-only $remote/$branch
if [ -f ./bin/install.bash ]; then
./bin/install.bash || exit
fi
else
echo "**** No changes to $branch in $trg_path"
fi
}
git update-server-info
# pull the master branch into one directory
do_git_update '/path/to/myproject-prod' master;
# ... and the develop branch into another
do_git_update '/path/to/myproject-dev' develop;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment