温馨提示×

Laravel项目在Debian上如何进行持续集成

小樊
35
2025-12-30 18:40:05
栏目: 智能运维

在Debian上进行Laravel项目的持续集成(CI)可以通过多种方式实现,其中最常见的是使用GitLab CI/CD、GitHub Actions或Jenkins。以下是使用这些工具的基本步骤:

使用GitLab CI/CD

  1. 安装GitLab Runner: 在Debian上安装GitLab Runner:

    sudo apt update
    sudo apt install gitlab-runner
    
  2. 注册Runner: 注册一个新的Runner到你的GitLab实例:

    sudo gitlab-runner register
    

    按照提示输入GitLab实例的URL和注册Token。

  3. 配置.gitlab-ci.yml: 在你的Laravel项目根目录下创建一个.gitlab-ci.yml文件,并添加CI/CD配置。例如:

    stages:
      - test
      - deploy
    
    test:
      stage: test
      script:
        - composer install
        - php artisan test
    
    deploy:
      stage: deploy
      script:
        - ssh -o StrictHostKeyChecking=no $DEPLOY_USER@$DEPLOY_HOST "cd /path/to/your/project && git pull && composer install && php artisan migrate"
      only:
        - master
    
  4. 配置SSH密钥: 确保GitLab Runner有权限访问你的部署服务器。你可以在Runner的设置中添加SSH密钥。

使用GitHub Actions

  1. 创建GitHub Actions工作流: 在你的Laravel项目根目录下创建一个.github/workflows/main.yml文件,并添加CI/CD配置。例如:

    name: CI/CD
    
    on:
      push:
        branches:
          - master
    
    jobs:
      build:
        runs-on: ubuntu-latest
    
        steps:
          - uses: actions/checkout@v2
          - name: Install dependencies
            run: |
              composer install
          - name: Run tests
            run: php artisan test
          - name: Deploy to server
            env:
              DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
              DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
              DEPLOY_PATH: /path/to/your/project
            run: |
              ssh -o StrictHostKeyChecking=no $DEPLOY_USER@$DEPLOY_HOST "cd $DEPLOY_PATH && git pull && composer install && php artisan migrate"
    
  2. 配置SSH密钥: 在GitHub仓库的Settings -> Secrets中添加DEPLOY_USERDEPLOY_HOSTDEPLOY_PATH

使用Jenkins

  1. 安装Jenkins: 在Debian上安装Jenkins:

    wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
    sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
    sudo apt update
    sudo apt install jenkins
    
  2. 启动并启用Jenkins服务

    sudo systemctl start jenkins
    sudo systemctl enable jenkins
    
  3. 配置Jenkins Job: 在Jenkins中创建一个新的Job,并配置构建步骤。例如:

    • 构建触发器:选择“Poll SCM”并设置定时任务。
    • 构建环境:选择“Use secret text(s) or file(s)”并添加SSH密钥。
    • 构建
      • 执行Shell脚本:
        cd /path/to/your/project
        git pull
        composer install
        php artisan test
        ssh -o StrictHostKeyChecking=no $DEPLOY_USER@$DEPLOY_HOST "cd /path/to/your/project && composer install && php artisan migrate"
        

通过以上步骤,你可以在Debian上为你的Laravel项目设置持续集成。选择适合你项目的工具,并根据需要进行调整。

0