GitLab CI setup for PHP/Laravel

Continuous Integration with Laravel 5.6+
29th June 2018
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 make
Handles deployment, can be extended with test stages and additional deploment environments


# vi: set noexpandtab

## variables
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('', 'composer-setup.php');"
    php7.2 composer-setup.php

vendor: composer.phar composer.json composer.lock
    php7.2 composer.phar install
    ./artisan key:generate

node_modules: package.json package-lock.json
    npm install

public/css/app.css: $(SASS)
    npm run dev

public/css/app.js: $(JS)
    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 Makefile to make some things a bit easier for local dev, though they're not used on deployment.

    ## full cleanup - return to "just cloned" state
        make clean
        rm -rf vendor .env public/css/app.css public/js/app.js

    ## serve with artisan
        ./artisan serve --host=
With these, you can run make clean to clean up your working copy, or make serve 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 DEPLOYMENT_USER's ~/.ssh/authorized_keys
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
image: stevepatter/php-builder

    - vendor

# 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 )'
  - make

# deploy the dev branch
    - dev
    name: dev
    - 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"