Test and deploy a Laravel application with Gitlab CI/CD and Envoy
Luigi Laezza
Every new installation of Laravel (currently 5.4) comes with two type of tests, ‘Feature’ and ‘Unit’, placed in the tests directory. Here’s a unit test from test/Unit/ExampleTest.php:
<?php namespace Tests\Unit; ... class ExampleTest extends TestCase { public function testBasicTest() { $this->assertTrue(true); } }
This test is as simple as asserting that the given value is true.
vendor/bin/phpunit OK (1 test, 1 assertions)
This test will be used later for continuously testing our app with GitLab CI/CD.
PUSH TO GitLab
Since we have our app up and running locally, it’s time to push the codebase to our remote repository. Let’s create a new project in GitLab named laravel-sample. After that, follow the command line instructions displayed on the project’s homepage to initiate the repository on our machine and push the first commit.
cd laravel-sample git init git remote add origin git@gitlab.example.com:<USERNAME>/laravel-sample.git git add . git commit -m 'Initial Commit' git push -u origin master
CONFIGURE THE PRODUCTION SERVER
Before we begin setting up Envoy and GitLab CI/CD, let’s quickly make sure the production server is ready for deployment. We have installed LEMP stack which stands for Linux, Nginx, MySQL and PHP on our Ubuntu 16.04.
CREATE A NEW USER
Let’s now create a new user that will be used to deploy our website and give it the needed permissions using Linux ACL:
# Create user deployer sudo adduser deployer # Give the read-write-execute permissions to deployer user for directory /var/www sudo setfacl -R -m u:deployer:rwx /var/www
If you don’t have ACL installed on your Ubuntu server, use this command to install it:
sudo apt install acl ADD SSH KEY
Let’s suppose we want to deploy our app to the production server from a private repository on GitLab. First, we need to generate a new SSH key pair with no passphrase for the deployer user.
# As the deployer user on server # # Copy the content of public key to authorized_keys cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys # Copy the private key text block cat ~/.ssh/id_rsa
Now, let’s add it to your GitLab project as a secret variable. Secret variables are user-defined variables and are stored out of .gitlab-ci.yml, for security purposes. They can be added per project by navigating to the project’s Settings > CI/CD.
>>>>secret variables page
# As the deployer user on the server # # Copy the public key cat ~/.ssh/id_rsa.pub
To the field Title, add any name you want, and paste the public key into the Key field.
Now, let’s clone our repository on the server just to make sure the deployer user has access to the repository.
# As the deployer user on server # git clone git@gitlab.example.com:<USERNAME>/laravel-sample.git
Note: Answer yes if asked Are you sure you want to continue connecting (yes/no)?. It adds GitLab.com to the known hosts.
CONFIGURING NGINX
Now, let’s make sure our web server configuration points to the current/public rather than public.
sudo nano /etc/nginx/sites-available/default
The configuration should be like this.
server { root /var/www/app/current/public; server_name example.com; # Rest of the configuration }
Note: You may replace the app’s name in /var/www/app/current/public with the folder name of your application.
@servers(['web' => 'remote_username@remote_host']) @task('list', [on => 'web']) ls -l @endtask
As you may expect, we have an array within @servers directive at the top of the file, which contains a key named web with a value of the server’s address (e.g. deployer@192.168.1.1). Then within our @task directive we define the bash commands that should be run on the server when the task is executed.
@SETUP DIRECTIVE The first step of our deployment process is to define a set of variables within @setup directive. You may change the appto your application’s name: ... @setup $repository = 'git@gitlab.example.com:<USERNAME>/laravel-sample.git'; $releases_dir = '/var/www/app/releases'; $app_dir = '/var/www/app'; $release = date('YmdHis'); $new_release_dir = $releases_dir .'/'. $release; @endsetup ... $repository is the address of our repository $releases_dir directory is where we deploy the app $app_dir is the actual location of the app that is live on the server $release contains a date, so every time that we deploy a new release of our app, we get a new folder with the current date as name $new_release_dir is the full path of the new release which is used just to make the tasks cleaner @STORY DIRECTIVE The @story directive allows us define a list of tasks that can be run as a single task. Here we have three tasks called clone_repository, run_composer, update_symlinks. These variables are usable to making our task’s codes more cleaner: ... @story('deploy') clone_repository run_composer update_symlinks @endstory ...
Let’s create these three tasks one by one.
CLONE THE REPOSITORY
The first task will create the releases directory (if it doesn’t exist), and then clone the master branch of the repository (by default) into the new release directory, given by the $new_release_dir variable. The releases directory will hold all our deployments:
... @task('clone_repository') echo 'Cloning repository' [ -d {{ $releases_dir }} ] || mkdir {{ $releases_dir }} git clone --depth 1 {{ $repository }} {{ $new_release_dir }} @endtask ...
While our project grows, its Git history will be very very long over time. Since we are creating a directory per release, it might not be necessary to have the history of the project downloaded for each release. The --depth 1 option is a great solution which saves systems time and disk space as well.
... @task('run_composer') echo "Starting deployment ({{ $release }})" cd {{ $new_release_dir }} composer install --prefer-dist --no-scripts -q -o @endtask ...
ACTIVATE NEW RELEASE
... @task('update_symlinks') echo "Linking storage directory" rm -rf {{ $new_release_dir }}/storage ln -nfs {{ $app_dir }}/storage {{ $new_release_dir }}/storage echo 'Linking .env file' ln -nfs {{ $app_dir }}/.env {{ $new_release_dir }}/.env echo 'Linking current release' ln -nfs {{ $new_release_dir }} {{ $app_dir }}/current @endtask
As you see, we use -nfs as an option for ln command, which says that the storage, .env and current no longer points to the preview’s release and will point them to the new release by force (f from -nfs means force), which is the case when we are doing multiple deployments.
@servers(['web' => 'deployer@192.168.1.1']) @setup $repository = 'git@gitlab.example.com:<USERNAME>/laravel-sample.git'; $releases_dir = '/var/www/app/releases'; $app_dir = '/var/www/app'; $release = date('YmdHis'); $new_release_dir = $releases_dir .'/'. $release; @endsetup @story('deploy') clone_repository run_composer update_symlinks @endstory @task('clone_repository') echo 'Cloning repository' [ -d {{ $releases_dir }} ] || mkdir {{ $releases_dir }} git clone --depth 1 {{ $repository }} {{ $new_release_dir }} @endtask @task('run_composer') echo "Starting deployment ({{ $release }})" cd {{ $new_release_dir }} composer install --prefer-dist --no-scripts -q -o @endtask @task('update_symlinks') echo "Linking storage directory" rm -rf {{ $new_release_dir }}/storage ln -nfs {{ $app_dir }}/storage {{ $new_release_dir }}/storage echo 'Linking .env file' ln -nfs {{ $app_dir }}/.env {{ $new_release_dir }}/.env echo 'Linking current release' ln -nfs {{ $new_release_dir }} {{ $app_dir }}/current @endtask
One more thing we should do before any deployment is to manually copy our application storage folder to the /var/www/app directory on the server for the first time. You might want to create another Envoy task to do that for you. We also create the .env file in the same path to setup our production environment variables for Laravel. These are persistent data and will be shared to every new release.
git add Envoy.blade.php git commit -m 'Add Envoy' git push origin master
CONTINUOUS INTEGRATION WITH GITLAB
We have our app ready on GitLab, and we also can deploy it manually. But let’s take a step forward to do it automatically with Continuous Delivery method. We need to check every commit with a set of automated tests to become aware of issues at the earliest, and then, we can deploy to the target environment if we are happy with the result of the tests.
# Set the base image for subsequent instructions FROM php:7.1 # Update packages RUN apt-get update # Install PHP and composer dependencies RUN apt-get install -qq git curl libmcrypt-dev libjpeg-dev libpng-dev libfreetype6-dev libbz2-dev # Clear out the local repository of retrieved package files RUN apt-get clean # Install needed extensions # Here you can install any other extension that you need during the test and deployment process RUN docker-php-ext-install mcrypt pdo_mysql zip # Install Composer RUN curl --silent --show-error https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer # Install Laravel Envoy RUN composer global require "laravel/envoy=~1.0"
We added the official PHP 7.1 Docker image, which consist of a minimum installation of Debian Jessie with PHP pre-installed, and works perfectly for our use case.
docker build -t registry.gitlab.com/<USERNAME>/laravel-sample . docker push registry.gitlab.com/<USERNAME>/laravel-sample
>>> container registry page with image
git add Dockerfile git commit -m 'Add Dockerfile' git push origin master
SETTING UP GITLAB CI/CD
image: registry.gitlab.com/<USERNAME>/laravel-sample:latest services: - mysql:5.7 variables: MYSQL_DATABASE: homestead MYSQL_ROOT_PASSWORD: secret DB_HOST: mysql DB_USERNAME: root stages: - test - deploy unit_test: stage: test script: - cp .env.example .env - composer install - php artisan key:generate - php artisan migrate - vendor/bin/phpunit deploy_production: stage: deploy script: - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )' - eval $(ssh-agent -s) - ssh-add <(echo "$SSH_PRIVATE_KEY") - mkdir -p ~/.ssh - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config' - ~/.composer/vendor/bin/envoy run deploy environment: name: production url: http://192.168.1.1 when: manual only: - master
That’s a lot to take in, isn’t it? Let’s run through it step by step.
image: registry.gitlab.com/<USERNAME>/laravel-sample:latest services: - mysql:5.7 ...
Note: If you wish to test your app with different PHP versions and database management systems, you can define different image and services keywords for each test job.
VARIABLES
GitLab CI/CD allows us to use environment variables in our jobs. We defined MySQL as our database management system, which comes with a superuser root created by default.
So we should adjust the configuration of MySQL instance by defining MYSQL_DATABASE variable as our database name and MYSQL_ROOT_PASSWORD variable as the password of root. Find out more about MySQL variables at the official MySQL Docker Image.
Also set the variables DB_HOST to mysql and DB_USERNAME to root, which are Laravel specific variables. We define DB_HOST as mysql instead of 127.0.0.1, as we use MySQL Docker image as a service which is linked to the main Docker image.
... variables: MYSQL_DATABASE: homestead MYSQL_ROOT_PASSWORD: secret DB_HOST: mysql DB_USERNAME: root ...
UNIT TEST AS THE FIRST JOB
... unit_test: script: # Install app dependencies - composer install # Setup .env - cp .env.example .env # Generate an environment key - php artisan key:generate # Run migrations - php artisan migrate # Run tests - vendor/bin/phpunit ...
DEPLOY TO PRODUCTION
The job deploy_production will deploy the app to the production server. To deploy our app with Envoy, we had to set up the $SSH_PRIVATE_KEY variable as an SSH private key. If the SSH keys have added successfully, we can run Envoy.
... deploy_production: script: # Add the private SSH key to the build environment - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )' - eval $(ssh-agent -s) - ssh-add <(echo "$SSH_PRIVATE_KEY") - mkdir -p ~/.ssh - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config' # Run Envoy - ~/.composer/vendor/bin/envoy run deploy environment: name: production url: http://192.168.1.1 when: manual only: - master
You may also want to add another job for staging environment, to final test your application before deploying to production.
TURN ON GITLAB CI/CD
We have prepared everything we need to test and deploy our app with GitLab CI/CD. To do that, commit and push .gitlab-ci.yml to the master branch. It will trigger a pipeline, which you can watch live under your project’s Pipelines.
Here we see our Test and Deploy stages. The Test stage has the unit_test build running. click on it to see the Runner’s output.
After our code passed through the pipeline successfully, we can deploy to our production server by clicking the play button on the right side.
Once the deploy pipeline passed successfully, navigate to Pipelines > Environments.
If something doesn’t work as expected, you can roll back to the latest working version of your app.
By clicking on the external link icon specified on the right side, GitLab opens the production website. Our deployment successfully was done and we can see the application is live.
In the case that you’re interested to know how is the application directory structure on the production server after deployment, here are three directories named current, releases and storage. As you know, the current directory is a symbolic link that points to the latest release. The .env file consists of our Laravel environment variables.
If you navigate to the current directory, you should see the application’s content. As you see, the .env is pointing to the /var/www/app/.env file and also storage is pointing to the /var/www/app/storage/ directory.
Related Articles
Transforming Media Agencies with AI: Embrace the Future Today
By Gustavo Pinto
The Power of a Working MVP: Turning Ideas into Investments
By Luigi Laezza
Turning Your Startup Idea Into Reality: How the Right Tech Team Can Make All the Difference
By Luigi Laezza
How Startups Can Leverage Custom Software Solutions to Accelerate Growth
By Luigi Laezza