How To Build A CI/CD Pipeline For Laravel On Gitlab

Serkan Erip
2 min readApr 10, 2021

--

Photo by Ben on Unsplash

Hello everyone, in this article i want to talk about how to build simply a CI/CD pipeline for a Laravel project in Gitlab.

Let’s Start 🚀

First we need to create a file with name .gitlab-ci.yml in our project. And then copy the gist below and paste in your file.

What Does This Yaml File Say

First we set our default image to use in all jobs. It’s a nice image to use for Laravel projects.

Then we say we need a MySQL service to run our feature tests. And in variables we define database name and password.

And time to set what to cache, this cache will prevent going on internet and download dependencies each time so we set vendor and npm folders to cache. You can read more detail in here.

So we setup our environment now time to set our stages we need three step you can add more steps its up to you.

Build Stage

In this stage, it is going to install composer and sets a Github token to access private composer dependencies in Github (If you don’t need just remove that line otherwise you need to add your token in variables). And then install dependencies, prepare project with a custom artisan command.

Test Stage

In this stage, we have two jobs, one of them is runs tests and another one is checking code standarts.

Deploy Stage

In deploy stage, we have two jobs, but these jobs run only on given branch.

This job simply triggers a deployment with curl. You should add this variables in Project->Settings->CI/CD->Variables.

The End 🎉

And we done, it’s easy to start build a CI/CD pipeline for a Laravel project.I hope this article helps to you.

--

--