Implementing continuous integration and deployment (CI/CD) for Laravel projects involves automating the process of testing, building, and deploying your application whenever changes are made to the codebase. Here is a step-by-step guide on how to set up CI/CD for a Laravel project using popular tools like GitHub Actions and Laravel Envoyer. Additionally, I'll provide links to documentation and resources for further reading.
Ensure your Laravel project is hosted on a version control system like Git. GitHub is a popular choice, but you can use other platforms like GitLab or Bitbucket.
.github/workflows
directoryInside your Laravel project, create a directory named .github/workflows
. GitHub Actions workflows are defined in YAML files within this directory.
Create a YAML file (e.g., laravel-ci-cd.yml
) in the .github/workflows
directory. This file will define the CI/CD workflow.
yamlname: Laravel CI/CD
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
- name: Install dependencies
run: composer install
- name: Run tests
run: vendor/bin/phpunit
deploy:
runs-on: ubuntu-latest
needs: build
steps:
- name: Deploy to Laravel Envoyer
uses: envoyer/deploy@v2
with:
token: ${{ secrets.ENVOYER_TOKEN }}
project: your-envoyer-project-name
Sign up for Laravel Envoyer: Laravel Envoyer
Set up a new project in Laravel Envoyer and configure the deployment environment.
Retrieve the Envoyer deployment token.
In your GitHub repository, go to "Settings" -> "Secrets" and add a secret named ENVOYER_TOKEN
with the value being the deployment token from Laravel Envoyer.
Commit the changes to your GitHub repository and push them to the main branch.
This example assumes you are using Laravel Envoyer for deployment, but you can adapt the workflow for other deployment tools or platforms based on your requirements.
=== Happy Coding :)