I've worked this together from several other examples, and separated
build from deployment so that if anything goes pear-shaped, you can
build the project locally with 1 command and end-up with the same
configuration as gitlab CI would create.
- This builds the code but doesn't handle any deployment steps. Intended so you can get a copy of the code running locally
by simply running
- Handles deployment, can be extended with test stages and additional deploment environments
# vi: set noexpandtab
ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
# SASS & JS to autodetect if css/js compile needs a rebuild
SASS := $(shell find resources/assets/sass -type f -name '*.scss')
JS := $(shell find resources/assets/js -type f -name '*.js')
## build - this runs every time
all: .env vendor node_modules public/css/app.css public/js/app.js clean
## build dependencies
cp .env.example .env
php7.2 -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
vendor: composer.phar composer.json composer.lock
php7.2 composer.phar install
node_modules: package.json package-lock.json
npm run dev
npm run dev
## end build dependencies
## cleanup after build, removes files that don't need deploying
rm -rf node_modules composer.phar composer-setup.php
You can add these lines to
to make some things a bit easier for local dev, though
they're not used on deployment.
## full cleanup - return to "just cloned" state
rm -rf vendor .env public/css/app.css public/js/app.js
## serve with artisan
./artisan serve --host=0.0.0.0
With these, you can run
to clean up your working copy, or
to quickly serve a local copy with artisan.
This file uses several environment variables that can be set on the in repository in gitlab, under settings
-> CI/CD. This lets you change the destination without editing the code, and keeps the deployment key
safe, in case you share the repository later.
- Private key for deployment, this is in the
- Username on deplyment server
- Server to deploy to
- Site path on deployment server
# name of the (public) docker image we're using to build
# this particular one has php5.6, 7.0, 7.1 & 7.2 available
# prepare the image
- apt-get update
- apt-get -y install rsync make unzip build-essential libpng-dev
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
# deploy the dev branch
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
- rsync -e "ssh -oStrictHostKeyChecking=no" -a --delete . "$DEPLOYMENT_USER"@"$DEPLOYMENT_HOST":"$DEPLOYMENT_PATH"