Continuous Integration for your Laravel Application with GitLab CI
Continuous Integration is the practice of regularly vetting code via automated testing, code analysis and/or whatever else your team uses to gauge validity of your code. Over time, implementing continuous integration into your software development lifecycle should increase your software’s quality by catching errors more frequently.
Read More: https://www.thoughtworks.com/continuous-integration
GitLab CI - Free and Easy Continuous Integration
If you have a Laravel application you want to add continuous integration to it is insanely easy with GitLab CI. GitLab CI is available under the free tier of GitLab.com and allows up to 2000 minutes of continuous integration pipelines per month.
In this article I will go over the steps to get continuous integration up and running to automatically run tests on a new Laravel application.
Repository Setup
If you already have a Laravel application up and running you can skip to the Enabling GitLab Continuous Integration portion of this article.
Installing Laravel
There are several different methods of installing Laravel, but I’m going to use composer to install a new project.
$ composer create-project --prefer-dist laravel/laravel laravel-ci-example
Once the repo has been created you can verify that the repo already has some sample tests setup.
$ cd laravel-ci-example
$ vendor/bin/phpunit
PHPUnit 7.3.5 by Sebastian Bergmann and contributors.
.. 2 / 2 (100%)
Time: 264 ms, Memory: 14.00MB
OK (2 tests, 2 assertions)
Adding a GitLab Project
Follow the steps here to add a new project to GitLab.
Pushing to GitLab
After creating a new project you can follow these steps to push to your newly created GitLab project.
$ cd laravel-ci-example
$ git init
$ git remote add origin [email protected]:{user}/laravel-ci-example.git
$ git add .
$ git commit -m "Initial commit"
$ git push -u origin master
Enabling GitLab Continuous Integration
Enabling CI for a GitLab project is as simple as 3 clicks.
- Setup CI/CD
- Choose Laravel Template
- Commit
After committing this config you will notice your first pipeline run will fail, we just need a couple more changes to conform to the template's desired setup:
$ cd laravel-ci-example
$ git pull
$ cat .env > .env.testing
Make the following edits to .env.testing:
DB_HOST=mysql
DB_DATABASE=project_name
DB_USERNAME=root
Then finally:
$ git add .env.testing
$ git commit -m "CI Setup"
$ git push
Now upon every commit to the code base these tests will run. You can see the state of this repository here: https://gitlab.com/aknosis/laravel-ci-example, and the results of the pipelines here: https://gitlab.com/aknosis/laravel-ci-example/pipelines.
Depending on your project's requirements you may need to modify .gitlab-ci.yml further.
For additional help you can see the CI Quickstart.
In a future article I will go over optimizing the Docker container for GitLab continuous integration to speed up test execution.
Thanks for reading.
If you are looking to make a change in your career consider checking out our job listings, subscribing to our newsletter, and following us on Twitter @__phpJobs__.