CI/CD Pipeline for Laravel Project (With Github Actions)

New Blog Every Week

We are always brewing something new and exciting. Subscribe now to stay updated on the remote tech world.
Table of Content
CI/CD pipeline for Laravel project

In this article, we will guide you through the process of setting up a CI/CD pipeline for Laravel project. You will learn how to automate the integration and deployment of your code, which will help make sure that your updates are tested and deployed smoothly.

We will discuss everything from setting up your environment to automating tests and deployments, to make it simple to improve your development workflow.

What is CI?

CI stands for Continuous Integration, it’s a method in software development where updates to the code are automatically integrated into the main codebase. This process helps to automate the tasks like building, linting(checking for code quality issues), and testing the source code to ensure it is functioning correctly.

By using CI, development teams can quickly and efficiently identify errors and security issues, as they are detected early in the development process. This approach makes it easier and less costly to fix bugs because they are addressed as soon as they are introduced.

What is CD?

CD means continuous deployment, which is an automated process that helps release software updates smoothly in different environments, such as development, staging, and production.

With continuous deployment, the entire process of deploying a new version of software is automated. This includes tasks such as

  • Pulling the new codebase version from the source control
  • Linting (checking for code quality issues)
  • Testing the software to ensure it works correctly
  • Building the application
  • Installing dependencies needed for the software to run
  • Running database migrations to update the database schema

Continuous deployment can also automate the infrastructure provisioning (setting up the hardware and software needed to run the application) and environment setup and configuration (ensuring all parts of the system are correctly configured).

What is CI/CD Pipeline?

Showing how CI/CD pipeline works and few tools suggestion

To set up Continuous Integration (CI) and Continuous Deployment (CD) for your project, creating pipelines is very important. A pipeline has two main parts: a trigger event and a series of actions that happen after the trigger.

The trigger can be any event, like making a pull request, merging branches, creating a new branch, or pushing a commit to the repository. You can also create custom triggers if needed.

The actions that occur after these events can include running tests, checking code quality with a linter, building the project, deploying it, or sending notifications about success or failure.

CI/CD pipelines help automate these tasks efficiently. Tools like Jenkins, GitHub Actions, and GitLab CI/CD make it easier to set up these pipelines with their specific triggers and actions.

GitHub Actions and GitLab CI/CD, which both host Git repositories, have built-in support for triggers and can be configured using YML files. These files explain which events will trigger the pipeline and what actions it will perform.

CI/CD tools also let you choose the environment where the pipeline will run. For example, if you select an Ubuntu server, the pipeline will operate in that environment, so that you can use Ubuntu commands like ‘docker build -t test-tag .’ to create docker images and ‘docker push‘ to upload those images to a docker registry.

When you use the right triggers and environments, pipelines can automate almost any manual task.

Now, let’s look at how to set up a simple continuous deployment pipeline for a Laravel project using GitHub Actions.

Configuring CI/CD Pipeline for Laravel Project (with github action)

Let’s configure a CI/CD pipeline for your Laravel project. The first step in this process involves generating a SSH key.

1. Generating SSH Key:

First, you need to allow GitHub to access your server. You can do this without using an SSH key, but using one is safer and easier.

To start, access the server yourself using SSH:

Then create a SSH key with following Command:

It will ask you a few questions here, but it’s best to just stick with the default options each time.

This command generates both public and private SSH keys. For now, we only need the public key. To view it, type the following command in the terminal:

Copy the output of the command and then run this command:

2. Adding SSH Keys on a Github

You need to add the keys you created earlier in different places on GitHub. Here’s how to add public keys to your profile:

Go to Settings :

Screenshot of a GitHub dashboard with navigation to the Settings page.

Then go to ” SSH and GDP keys ” in the Settings:

Screenshot of a GitHub dashboard with navigation to the "SSH and GPG keys" page.

Then press on “New SSH Key” :

Screenshot of a GitHub dashboard with navigation to add new SSH key

Copy your public key and paste it in here. The title field is optional, but if you add one try to make it easy to understand.

Screenshot of a GitHub dashboard with navigation to add title and your public key

Now, go to the repository where you want to set up your CI/CD Pipeline.

Next, go to the settings of the repository and look for the “Secrets” tab.

Screenshot of a GitHub dashboard with navigation to "Secrets" tab

Add a few secrets to the repository:

  • SSH_HOST, which is the IP address of our server.
  • SSH_USERNAME, which is the username we use in the SSH command.
  • SSH_KEY, which is our private key (make sure not to share it anywhere else!);

You can find your private SSH key by running this command:

Copy the output of this command.

Configure CD Pipeline:

Now, it’s time to set up the CD pipeline. First, create two folders in your project. Start by making a folder called .github in the main project directory, then create another folder inside it named .workflows. Next, you need to create a file in the .workflows folder. You can name this file whatever you like, but make sure the name is clear.

Let’s create a yml file called deploy.yml.

Screenshot of a GitHub dashboard with navigation to create .yml file

Now, write the following configuration in this file:

If you’re using Laravel Mix instead of Vite, you need to change “npm run build” to “npm run prod.”

Now, let’s break down the process step by step:

First, we’ll identify which event will trigger the GitHub Actions workflow:

Then we declared the operating system , where the docker container will be set up:

And this is the workflow:

Now you will need to use some tools like :

  • setup-php to install PHP inside the container
  • checkout to clone your project inside a container
  • setup-node, which installs Node inside the container
  • mysql-action which installs and configures the MySQL database inside the container.

Next, we will list the commands that we need to run to:

  1. Copy the .env.example file
  2. Install Composer packages
  3. Clear the Laravel cache, update the configuration, create a project key, and compile assets
  4. Allow Laravel to create, read, and delete files in the bootstrap/cache directory
  5. Create an SQLite file that we will use for testing
  6. Update the Laravel .env file so it can connect to the current MySQL database with the right credentials
  7. Finally, you can run the test suite.

This example shows the commands you need to use in a Laravel application to make the updates visible. You can adjust this section to meet the specific needs of your application.

If you still find this process overwhelming, consider hiring a Laravel developer to handle it for you.

FAQ

Can I use GitLab instead of GitHub Actions for Setting Up a CI/CD Pipeline?

Yes, you can use GitLab instead of GitHub Actions. GitLab has its own built-in CI/CD tools that work similarly to GitHub Actions

What Are Observers in Laravel?

Observers in Laravel are classes that help you listen for certain events when working with models, like when a record is created or updated.

How Docker is Used in CI CD pipeline?

Docker is used in a CI/CD pipeline to make sure that the environments for building, testing, and deploying applications are the same. It packages your app and everything it needs into containers, so it works the same way no matter where you run it.

What is a Deployer in CI/CD?

Deployer is a tool used in CI/CD pipelines to automate the process of deploying applications to servers.

Can I Use Kubernetes Cluster with GitHub Actions for CI/CD?

Yes, you can use a Kubernetes cluster with GitHub Actions for CI/CD.

New Blog Every Week

We are always brewing something new and exciting. Subscribe now to stay updated on the remote tech world.
Scroll to Top