- Introduction
- Initialize our Laravel app on GitLab
- Configure the production server
- Setting up Envoy
- Continuous Integration with GitLab
- Conclusion
Test and deploy Laravel applications with GitLab CI/CD and Envoy
Introduction
GitLab features our applications with Continuous Integration, and it is possible to easily deploy the new code changes to the production server whenever we want.
In this tutorial, we’ll show you how to initialize a .
We assume you have a basic experience with Laravel, Linux servers,
and you know how to use GitLab.
Laravel is a high quality web framework written in PHP.
It has a great community with a .
Aside from the usual routing, controllers, requests, responses, views, and (blade) templates, out of the box Laravel provides plenty of additional services such as cache, events, localization, authentication, and many others.
We will use .
We assume Unit Test
Every new installation of Laravel (currently 8.0) comes with two type of tests, ‘Feature’ and ‘Unit’, placed in the tests directory.
Here’s a unit test from This test is as simple as asserting that the given value is true.
Laravel uses This test will be used later for continuously testing our app with GitLab CI/CD.
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 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.
Let’s now create a new user that will be used to deploy our website and give it
the needed permissions using :
If you don’t have ACL installed on your Ubuntu server, use this command to install it:
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.
After that, we need to copy the private key, which will be used to connect to our server as the deployer user with SSH, to be able to automate our deployment process:
Now, let’s add it to your GitLab project as a CI/CD variable.
Project CI/CD variables are user-defined variables and are stored out of To the field KEY, add the name We also need to add the public key to Project > Settings > Repository as a Deploy Key, which gives us the ability to access our repository from the server through SSH protocol.
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 Answer yes if asked Now, let’s make sure our web server configuration points to the Open the default NGINX server block configuration file by typing:
The configuration should be like this.
You may replace the app’s name in So we have our Laravel app ready for production.
The next thing is to use Envoy to perform the deploy.
To use Envoy, we should first install it on our local machine .
The pros of Envoy is that it doesn’t require Blade engine, it just uses Blade syntax to define tasks.
To start, we create an As you may expect, we have an array within On the local machine use the It should execute the Envoy is not a dependency of Laravel, therefore you can use it for any PHP application.
Every time we deploy to the production server, Envoy downloads the latest release of our app from GitLab repository and replace it with preview’s release.
Envoy does this without any ,
so we don’t have to worry during the deployment while someone might be reviewing the site.
Our deployment plan is to clone the latest release from GitLab repository, install the Composer dependencies and finally, activate the new release.
The first step of our deployment process is to define a set of variables within app to your application’s name:
The clone_repository, Let’s create these three tasks one by one.
The first task will create the While our project grows, its Git history will be 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 As you may know, this task just navigates to the new release directory and runs Composer to install the application dependencies:
Next thing to do after preparing the requirements of our new release, is to remove the storage directory from it and to create two symbolic links to point the application’s As you see, we use The script is ready, but make sure to change the At the end, our One more thing we should do before any deployment is to manually copy our application Now, we would need to deploy our app by running Now it’s time to commit main, without using feature-branches since collaboration is beyond the scope of this tutorial.
In a real world project, teams may use Issue Tracker and merge requests to move their code across branches:
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 GitLab CI/CD allows us to use .
To be able to build, test, and deploy our app with GitLab CI/CD, we need to prepare our work environment.
To do that, we’ll use a Docker image which has the minimum requirements that a Laravel app needs to run.
There are other ways to do that as well, but they may lead our builds run slowly, which is not what we want when there are faster options to use.
Initialize our Laravel app on GitLab
test/Unit/ExampleTest.php
:
<?php
namespace Tests\Unit;
...
class ExampleTest extends TestCase
{
public function testBasicTest()
{
$this->assertTrue(true);
}
}
PHPUnit
for tests by default.
If we run vendor/bin/phpunit
we should see the green output:
vendor/bin/phpunit
OK (1 test, 1 assertions)
Push to GitLab
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 main
Configure the production server
Create a new user
# 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
sudo apt install acl
Add SSH key
# 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
.gitlab-ci.yml
, for security purposes.
They can be added per project by navigating to the project’s Settings > CI/CD.
SSH_PRIVATE_KEY
, and to the VALUE field, paste the private key you’ve copied earlier.
We’ll use this variable in the .gitlab-ci.yml
later, to easily connect to our remote server as the deployer user without entering its password.
# As the deployer user on the server
#
# Copy the public key
cat ~/.ssh/id_rsa.pub
deployer
user has access to the repository.
# As the deployer user on server
#
git clone git@gitlab.example.com:<USERNAME>/laravel-sample.git
Are you sure you want to continue connecting (yes/no)?
.
It adds GitLab.com to the known hosts.
Configuring NGINX
current/public
rather than public
.
sudo nano /etc/nginx/sites-available/default
server {
root /var/www/app/current/public;
server_name example.com;
# Rest of the configuration
}
/var/www/app/current/public
with the folder name of your application.
Setting up Envoy
How Envoy works
Envoy.blade.php
in the root of our app with a simple task to test Envoy.
@servers(['web' => 'remote_username@remote_host'])
@task('list', ['on' => 'web'])
ls -l
@endtask
@servers
directive at the top of the file, which contains a key named web
with a value of the server’s address (for example, 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.
run
command to run Envoy tasks.
envoy run list
list
task we defined earlier, which connects to the server and lists directory contents.
Zero downtime deployment
...
@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
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
...
Clone the repository
releases
directory (if it doesn’t exist), and then clone the main
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 }}
cd {{ $new_release_dir }}
git reset --hard {{ $commit }}
@endtask
...
--depth 1
option is a great solution which saves systems time and disk space as well.
Installing dependencies with Composer
...
@task('run_composer')
echo "Starting deployment ({{ $release }})"
cd {{ $new_release_dir }}
composer install --prefer-dist --no-scripts -q -o
@endtask
...
Activate new release
storage
directory and .env
file to the new release.
Then, we need to create another symbolic link to the new release with the name of current
placed in the app directory.
The current
symbolic link always points to the latest release of our app:
...
@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
-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.
Full script
deployer@192.168.1.1
to your server and also change /var/www/app
with the directory you want to deploy your app.
Envoy.blade.php
file will look like this:
@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 }}
cd {{ $new_release_dir }}
git reset --hard {{ $commit }}
@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
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 set up our production environment variables for Laravel.
These are persistent data and will be shared to every new release.
envoy run deploy
, but it won’t be necessary since GitLab can handle that for us with CI’s environments, which will be described later in this tutorial.
git add Envoy.blade.php
git commit -m 'Add Envoy'
git push origin main
Continuous Integration with GitLab
Create a Container Image