项目自动化发布方案

1. 基础发布流程

1.1 传统发布方式

# 上传代码到服务器
rsync -avz --delete ./ user@production:/var/www/html/

# 设置权限
ssh user@production "chown -R www-data:www-data /var/www/html && chmod -R 755 /var/www/html"

# 重启服务
ssh user@production "systemctl restart apache2"

1.2 现代化 CI/CD 流程

2. 自动化工具配置

2.1 GitHub Actions

# .github/workflows/deploy.yml
name: PHP CI/CD

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.1'
          extensions: mbstring, xml, mysql
      - name: Run tests
        run: composer test

  deploy:
    needs: test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Install dependencies
        run: composer install --no-dev --optimize-autoloader
      - name: Deploy to production
        uses: appleboy/scp-action@master
        with:
          host: ${{ secrets.PRODUCTION_HOST }}
          username: ${{ secrets.PRODUCTION_USER }}
          key: ${{ secrets.SSH_KEY }}
          source: "./"
          target: "/var/www/html"

2.2 Jenkins 配置

pipeline {
    agent any
    
    stages {
        stage('Checkout') {
            steps {
                git branch: 'main', url: 'git@github.com:your/repo.git'
            }
        }
        
        stage('Test') {
            steps {
                sh 'composer install'
                sh 'vendor/bin/phpunit'
            }
        }
        
        stage('Deploy') {
            steps {
                sshPublisher(
                    publishers: [
                        sshPublisherDesc(
                            configName: 'production-server',
                            transfers: [
                                sshTransfer(
                                    sourceFiles: '**/*',
                                    removePrefix: '',
                                    remoteDirectory: '/var/www/html',
                                    execCommand: 'chown -R www-data:www-data /var/www/html && systemctl reload apache2'
                                )
                            ]
                        )
                    ]
                )
            }
        }
    }
}

3. 数据库迁移处理

3.1 Laravel 迁移

# 在部署脚本中添加
php artisan migrate --force

3.2 独立数据库迁移

# 单独迁移步骤
db_migrate:
  stage: deploy
  script:
    - ssh user@production "cd /var/www/html && php artisan migrate --force"
  only:
    - main

4. 环境配置管理

4.1 环境变量管理

// config/database.php
return [
    'connections' => [
        'mysql' => [
            'host' => env('DB_HOST', '127.0.0.1'),
            // 其他配置...
        ],
    ],
];

4.2 部署时注入配置

# 使用.env.production 替换 .env
scp .env.production user@production:/var/www/html/.env

5. 零停机部署策略

5.1 符号链接切换版本

# 服务器上执行
ln -s /var/www/releases/v1.2.3 /var/www/html

5.2 完整部署脚本

#!/bin/bash

# 创建新版本目录
RELEASE_DIR="/var/www/releases/$(date +%Y%m%d%H%M%S)"
mkdir -p $RELEASE_DIR

# 同步代码
rsync -avz --exclude='.env' ./ user@production:$RELEASE_DIR/

# 保留.env文件
scp .env.production user@production:$RELEASE_DIR/.env

# 切换符号链接
ssh user@production "ln -sfn $RELEASE_DIR /var/www/html"

# 重启服务
ssh user@production "sudo systemctl reload php-fpm"

6. 监控与回滚

6.1 健康检查端点

// routes/api.php
Route::get('/health', function() {
    try {
        DB::connection()->getPdo();
        return response()->json(['status' => 'healthy']);
    } catch (\Exception $e) {
        return response()->json(['status' => 'unhealthy'], 500);
    }
});

6.2 快速回滚方案

#!/bin/bash

# 回滚到上一个版本
LAST_RELEASE=$(ssh user@production "ls -td /var/www/releases/* | head -2 | tail -1")
ssh user@production "ln -sfn $LAST_RELEASE /var/www/html"

7. 最佳实践建议

  1. 版本控制​:确保所有代码和配置都纳入版本控制
  2. 环境一致性​:开发、测试、生产环境尽量保持一致
  3. 自动化测试​:部署前必须通过单元测试和功能测试
  4. 小步发布​:每次发布尽量只包含少量变更
  5. 备份策略​:数据库和文件系统都要有可靠的备份方案
  6. 监控报警​:部署后实时监控系统状态

8. 高级部署方案

8.1 Docker 化部署

# Dockerfile
FROM php:8.1-apache

WORKDIR /var/www/html

COPY . .

RUN composer install --no-dev --optimize-autoloader \
    && chown -R www-data:www-data /var/www/html \
    && a2enmod rewrite

8.2 Kubernetes 部署

# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: php-app
  template:
    metadata:
      labels:
        app: php-app
    spec:
      containers:
      - name: php
        image: your-registry/php-app:latest
        ports:
        - containerPort: 80
        envFrom:
        - secretRef:
            name: php-env-secrets

通过以上方案,可以实现 PHP 项目的安全、可靠、自动化发布,从简单脚本到高级容器化部署,满足不同规模项目的需求。

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇