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?
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:
ssh root@127.0.0.1
Then create a SSH key with following Command:
ssh-keygen
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:
cat ~/.ssh/id_rsa.pub
Copy the output of the command and then run this command:
sudo vim ~/.ssh/authorized_keys
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 :
Then go to ” SSH and GDP keys ” in the Settings:
Then press on “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.
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.
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:
cat ~/.ssh/id_rsa
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.
Now, write the following configuration in this file:
name: Deploy
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
- uses: actions/checkout@v2
- uses: actions/setup-node@v3
with:
node-version: 16
- uses: mirromutth/mysql-action@v1.1
with:
mysql database: laravel-test-db
mysql user: laravel_test_user
mysql password: example
- name: Copy .env
run: cp .env.example .env
- name: Install composer Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Install node dependencies
run: npm ci
- name: Setup Project
run: |
php artisan config:clear
php artisan cache:clear
php artisan key:generate
npm run build
- name: Directory Permissions
run: chmod 755 -R storage bootstrap/cache
- name: Run Unit tests
env:
APP_ENV: testing
DB_CONNECTION: mysql
DB_USERNAME: laravel_test_user
DB_PASSWORD: super_secret
DB_DATABASE: laravel_test_db
run: php artisan test
- name: Deploy to Server
if: ${{ success() }}
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
script_stop: true
script: |
cd apps
cd laravel-example
git pull
npm ci
npm run prod
composer i
php artisan migrate --force
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:
on:
push:
branches: [main]
Then we declared the operating system , where the docker container will be set up:
runs-on: ubuntu-latest
And this is the workflow:
steps:
- uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
- uses: actions/checkout@v2
- uses: actions/setup-node@v3
with:
node-version: 16
- uses: mirromutth/mysql-action@v1.1
with:
mysql database: laravel-test-db
mysql user: laravel_test_user
mysql password: example
- name: Copy .env
run: cp .env.example .env
- name: Install composer Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Install node dependencies
run: npm ci
- name: Setup Project
run: |
php artisan config:clear
php artisan cache:clear
php artisan key:generate
npm run build
- name: Directory Permissions
run: chmod 755 -R storage bootstrap/cache
- name: Run Unit tests
env:
APP_ENV: testing
DB_CONNECTION: mysql
DB_USERNAME: laravel_test_user
DB_PASSWORD: super_secret
DB_DATABASE: laravel_test_db
run: php artisan test
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:
- Copy the .env.example file
- Install Composer packages
- Clear the Laravel cache, update the configuration, create a project key, and compile assets
- Allow Laravel to create, read, and delete files in the bootstrap/cache directory
- Create an SQLite file that we will use for testing
- Update the Laravel .env file so it can connect to the current MySQL database with the right credentials
- Finally, you can run the test suite.
- name: Deploy to Server
if: ${{ success() }}
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
script_stop: true
script: |
cd apps
cd laravel-example
git pull
npm ci
npm run prod
composer i
php artisan migrate --force
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.