Setting up GitLab's Continuous Integration with Laravel, PostgreSQL and PHPUnit
I recently set up a continuous integration process based on GitLab for a project based on Laravel and PostgreSQL. It took a while to get it right, so I am sharing here my final config.
Basic structure
The main GitLab CI configuration is held by a file at the root of your project that should be called .gitlab-ci.yml
. From this file it is possible to call other shell scripts, but I have tried to hold up everything in just one place.
We will however manage a second file called .env.gitlab-ci
that will hold all ENV variable configuration optios specific to GitLab CI.
Last but not least you will need a PHPUnit xml configuration file.
Step by step look into .gitlab-ci.yml
We’ll go line by line in the config file, but you can see the final result at the end of the article.
Here we select one of the available docker images with php already configured. You can find a complete list here: from https://hub.docker.com/r/_/php/
From GitLab’s official documentation src:
The
services
keyword defines just another docker image that is run during your job and is linked to the docker image that theimage
keyword defines. This allows you to access the service image during build time.
It is very important to remark that if you add postgres
as service to your application, the service container for PostgresSQL will be accessible under the hostname postgres
. So, in order to access your database service you have to connect to the host named postgres
instead of a socket or localhost
. This will be important later when we configure our .env.gitlab-ci
file.
Here you may use all the values you want as long as they match with those of .env.gitlab-ci
and your phpunit.xml
.
Again from the official documentation src
before_script
is used to define the command that should be run before all jobs, including deploy jobs, but after the restoration of artifacts. This can be an array or a multi-line string.
Lets explore all these commands one by one.
Simply installing some dependencies
git
is needed to later installcomposer
libicu-dev
will be necessary for the internationalization php extension (intl
)libpq-dev
are the header files for postgreslibzip-dev
andzlib1g-dev
are necessary for zip manipulation and needed to activate the corresponding php extension. Without them ourcomposer install
command will throw a lot of warnings and try to download each package twice slowing the whol process down for 2 or more minutes
docker-php-ext-install
is a handy script for installing php extensions provided by the official php docker image. I restricted myself to the bare minimun of extension necessary for my project to run. You may need more.
Install composer
and use it to install all of our project dependencies.
Set up the correct ENV variables. We’ll look into this file in a moment.
Set application key, clear a couple of caches just in case and run the migrations.
And finally run our PHPUnit test suite using our phpunit-gitlabci.xml
config file.
Small break
That was the most complicated file, the rest is a piece of cake, but you deserve a rest :P
A brief look into .env.gitlab-ci
The file itself is quite self explainatory, and you can see the final version at the end of the article. But I wanted to highlight the database configuration.
Notice how the values for DB_HOST
, DB_DATABASE
and DB_USERNAME
are the same as those of inside of the variables
key on the .gitlab-ci.yml
configuration file.
A brief note on phpunit-gitlabci.xml
This file may be optional depending on your local configuration. If your normal phpunit.xml
file doesn’t define any DB_DATABASE
env variable you would not need this. But if it happens to define one, and has a different value from that that you chose for GitLab’s CI you have to remember to set it correctly:
Summary
GitLab’s continuous integration system can be a blessing once is properly configured, but it can get a bit confusing if you trail off of the usual path and all you hear is docker image configuration linking and you’ve never worked with it before. Hopefully you’ll find this small guide useful