ubuntu

Ubuntu PHP如何实现持续集成与持续部署

小樊
57
2025-10-08 00:14:49
栏目: 编程语言

Implementing Continuous Integration and Continuous Deployment (CI/CD) for PHP on Ubuntu

Continuous Integration (CI) and Continuous Deployment (CD) are essential for streamlining PHP development workflows, reducing manual errors, and accelerating delivery. Below is a structured guide to setting up CI/CD for PHP on Ubuntu, covering tools, configurations, and best practices.

1. Prerequisites

Before configuring CI/CD, ensure your Ubuntu environment has:

2. Choose a CI/CD Tool

Select a tool that aligns with your team’s needs:

3. Configure CI/CD Pipelines

Option A: GitHub Actions (Recommended for GitHub Projects)

Create a .github/workflows/php-ci.yml file in your project root. This workflow automates testing and deployment on every push to the master branch:

name: PHP CI/CD  
on:  
  push:  
    branches: [ master ]  
jobs:  
  build-test-deploy:  
    runs-on: ubuntu-latest  
    steps:  
      # Checkout code  
      - uses: actions/checkout@v3  
      # Set up PHP (specify version and extensions)  
      - uses: shivammathur/setup-php@v2  
        with:  
          php-version: '8.0'  
          extensions: mbstring, gd, curl  
      # Install dependencies  
      - run: composer install --no-interaction --prefer-dist --optimize-autoloader  
      # Run tests (PHPUnit required)  
      - run: vendor/bin/phpunit  
      # Deploy to server (via SSH)  
      - name: Deploy to Server  
        run: scp -r . user@your-server-ip:/var/www/myproject  
        env:  
          SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}  

Option B: Jenkins (For Customizable Pipelines)

Install Jenkins on Ubuntu (sudo apt install jenkins) and create a Jenkinsfile in your project:

pipeline {  
  agent any  
  stages {  
    stage('Build') {  
      steps {  
        checkout scm  
        sh 'composer install'  
        sh 'php artisan key:generate' // If using Laravel  
      }  
    }  
    stage('Test') {  
      steps {  
        sh 'vendor/bin/phpunit'  
      }  
    }  
    stage('Deploy') {  
      when {  
        branch 'master'  
      }  
      steps {  
        sh 'ssh deploy@your-server-ip "cd /var/www/myproject && git pull && composer install && systemctl restart nginx"'  
      }  
    }  
  }  
}  

Option C: GitLab CI (For GitLab Repositories)

Create a .gitlab-ci.yml file:

stages:  
  - build  
  - test  
  - deploy  

build_job:  
  stage: build  
  script:  
    - composer install  
  artifacts:  
    paths:  
      - vendor/  

test_job:  
  stage: test  
  script:  
    - vendor/bin/phpunit  

deploy_job:  
  stage: deploy  
  only:  
    - master  
  script:  
    - ssh deploy@your-server-ip "cd /var/www/myproject && git pull && composer install && systemctl restart nginx"  

4. Key Practices for Effective CI/CD

5. Post-Deployment Steps

After successful deployment, automate post-deployment tasks:

By following these steps, you can establish a robust CI/CD pipeline for PHP on Ubuntu, enabling faster, more reliable deployments while maintaining code quality.

0
看了该问题的人还看了