How to set up a CI/CD pipeline with GitHub Actions for Laravel?
Aug 03, 2025 am 02:43 AMCreate a .github/workflows/ci-cd.yml file to define workflows, trigger conditions to push to or merge to main branch, and configure MySQL services; 2. Check out code in test tasks, set up PHP environment, install dependencies, generate application keys, configure .env files, run migrations and execute phpunit tests; 3. Optional but recommended to add tools such as PHPStan for code quality check; 4. Use appleboy/ssh-action to deploy to the server through SSH, run only after the main branch is pushed and the test is passed, and manage sensitive information through GitHub Secrets; 5. All sensitive configurations use environment variables and GitHub Secrets injection to avoid hard coding; 6. It is recommended to use actions/cache to cache Composer dependencies to improve performance. This process enables automated testing and continuous deployment of Laravel applications, ensuring code quality and improving release efficiency.
Setting up a CI/CD pipeline for a Laravel application using GitHub Actions is straightforward and powerful. It helps automated testing, linting, and deployment workflows every time you push or merge code. Here's how to do it step by step.

? 1. Create the GitHub Actions Workflow File
Start by creating a .github/workflows/ci-cd.yml
file in your Laravel project. This defines your CI/CD pipeline.
name: Laravel CI/CD on: push: branches: [ main ] pull_request: branches: [ main ] jobs: test: runs-on: ubuntu-latest services: mysql: image: mysql:8.0 env: MYSQL_ROOT_PASSWORD: rootpassword MYSQL_DATABASE: homestead MYSQL_USER: homestead MYSQL_PASSWORD: secret Ports: - 3306:3306 options: --health-start-period=20s --health-interval=10s
This configures the workflow to run on every push or PR to main
, and sets up a MySQL service for testing.

? 2. Set Up the Environment and Run Tests
Inside the test
job, install dependencies and run Laravel's test suite.
Steps: - name: Checkout code uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 with: php-version: '8.2' extensions: mbstring, dom, pdo, sqlite, pdo_sqlite, mysql, zip coverage: none - name: Validate composer.json and composer.lock run: composer validate --strict - name: Install dependencies run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress - name: Generate application key run: php artisan key:generate - name: Create Laravel .env file run: cp .env.example .env - name: Run Laravel migrations run: php artisan migrate --database=mysql --force env: DB_CONNECTION: mysql DB_HOST: 127.0.0.1 DB_PORT: 3306 DB_DATABASE: homestead DB_USERNAME: homestead DB_PASSWORD: secret - name: Execute tests (phpunit) run: vendor/bin/phpunit
? This ensures your Laravel app boots up, connects to the database, migrates, and runs unit/feature tests.
? 3. Add Code Linting (Optional but Recommended)
You can include PHP_CodeSniffer or PHPStan for code quality checks.
- name: Install dev dependencies run: composer install -q - name: Run PHPStan run: vendor/bin/phpstan analyze app --level=7
Make sure phpstan/phpstan
is in require-dev
.
? 4. Set Up Deployment (CD) via SSH or Deployers
For continuous deployment (eg, to a VPS), you can use tools like appleboy/ssh-action
.
?? Store credentials in GitHub Secrets (
DEPLOY_HOST
,DEPLOY_USER
,SSH_PRIVATE_KEY
, etc.)
deploy: needs: test runs-on: ubuntu-latest if: github.ref == 'refs/heads/main' && github.event_name == 'push' Steps: - name: Deploy to production server uses: appleboy/ssh-action@v1.0.1 with: host: ${{ secrets.DEPLOY_HOST }} username: ${{ secrets.DEPLOY_USER }} key: ${{ secrets.SSH_PRIVATE_KEY }} script: | cd /var/www/your-laravel-app git pull origin main composer install --no-dev --optimize-autoloader php artisan migrate --force php artisan config:cache php artisan route:cache php artisan view:cache sudo systemctl reload nginx
This step only runs on pushes to main
after tests pass.
? 5. Environment Variables & Secrets
Never hardcode credentials. Use:
-
.env.example
for template - GitHub Secrets for sensitive values in workflows
In your workflow, inject secrets as environment variables where needed.
? Summary of Key Points
- ? Use
ubuntu-latest
runner with PHP and MySQL services - ? Run
composer install
,migrate
, andphpunit
- ? Keep deployment separate and conditional
- ? Use GitHub Secrets for SSH keys and DB credentials
- ? Cache dependencies (optional for speed):
- name: Cache Composer packages uses: actions/cache@v3 with: path: vendor key: composer-${{ hashFiles('composer.lock') }}
That's it. Your Laravel app now has automated testing on every change and automatic deployment to production when code lands on main
. It's not magic — just consistent, reliable automation.
The above is the detailed content of How to set up a CI/CD pipeline with GitHub Actions for Laravel?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

In modern software development, continuous integration (CI) has become an important practice to improve code quality and development efficiency. Among them, Jenkins is a mature and powerful open source CI tool, especially suitable for PHP applications. The following content will delve into how to use Jenkins to implement PHP continuous integration, and provide specific sample code and detailed steps. Jenkins installation and configuration First, Jenkins needs to be installed on the server. Just download and install the latest version from its official website. After the installation is complete, some basic configuration is required, including setting up an administrator account, plug-in installation, and job configuration. Create a new job On the Jenkins dashboard, click the "New Job" button. Select "Frees

Introduction Continuous integration (CI) and continuous deployment (CD) are key practices in modern software development that help teams deliver high-quality software faster and more reliably. Jenkins is a popular open source CI/CD tool that automates the build, test and deployment process. This article explains how to set up a CI/CD pipeline with Jenkins using PHP. Set up Jenkins Install Jenkins: Download and install Jenkins from the official Jenkins website. Create project: Create a new project from the Jenkins dashboard and name it to match your php project. Configure source control: Configure your PHP project's git repository as Jenkin

The Python language has become an indispensable part of modern software development, and continuous integration (CI) is part of the highly integrated and continuous delivery process, which can greatly improve the efficiency and quality of the development process. The purpose of CI is to minimize unnecessary errors by integrating code into a common code base and continuously running automated tests and static analysis tools. This article will discuss the principles of continuous integration in Python and its impact on the software development process. The Principle of Continuous Integration CI in Software Development

Answer: Use PHPCI/CD to achieve rapid iteration, including setting up CI/CD pipelines, automated testing and deployment processes. Set up a CI/CD pipeline: Select a CI/CD tool, configure the code repository, and define the build pipeline. Automated testing: Write unit and integration tests and use testing frameworks to simplify testing. Practical case: Using TravisCI: install TravisCI, define the pipeline, enable the pipeline, and view the results. Implement continuous delivery: select deployment tools, define deployment pipelines, and automate deployment. Benefits: Improve development efficiency, reduce errors, and shorten delivery time.

With the development of the Internet and the increasing number of application scenarios, more and more companies and developers are beginning to use the PHP language to develop websites and applications. In the development process, continuous integration and continuous deployment have become a trend, which can greatly improve development efficiency and product quality. GitLabCI/CD has received widespread attention and use as a tool to achieve continuous integration and continuous deployment. GitLab is an open source tool for managing and deploying software code, which can realize code version control, project management, code

Metrics for measuring CI/CD and automated deployments include: Build time: the time it takes for an application to be built and deployed Deployment frequency: the number of times an application is deployed in a specific time period Deployment failure rate: the number of failed deployments as a percentage of total deployment changes Failure rate: The ratio of deployments that resulted in failures or errors to the total number of deployments Code coverage: The percentage of code that was executed with automated tests Mean time to failure: The average time between a failure and resolution Lead time: The time it takes for code to be submitted to deployment to production average time required

In modern software development, CI/CD (ContinuousIntegration/ContinuousDeployment) has become an essential development process, which accelerates the development cycle and reduces error rates through continuous integration and automated deployment. For PHP developers, using CI/CD to achieve continuous integration and automated deployment is also a good choice. This article will briefly introduce how to use CI/CD in PHP to achieve continuous integration and automated deployment

For large PHP projects, CI/CD and automated deployment are crucial and can be achieved by following these steps: Set up a CI/CD pipeline using Jenkins, including continuous integration, delivery and deployment phases. Use PHPUnit for automated testing and Capistrano for deployment. Trigger a Jenkins pipeline and start a deployment on every code push or manual prompt. Use Capistrano to automatically deploy tested code to production. Benefits include: reducing errors, speeding development, improving team collaboration and ensuring quality.
