本文目录导读:

我来详细介绍几种在PHP项目中实现自动化构建的方法:
使用 PHP 构建工具
Composer 脚本
{
"scripts": {
"build": [
"@php vendor/bin/phpunit",
"@php vendor/bin/phpcs --standard=PSR2 src/",
"@php vendor/bin/phpstan analyse src/"
],
"deploy": [
"@composer install --no-dev --optimize-autoloader",
"@php artisan config:cache",
"@php artisan route:cache"
]
}
}
Phing (PHP版Ant)
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" default="build">
<target name="clean">
<delete dir="${build.dir}/reports" />
</target>
<target name="prepare">
<mkdir dir="${build.dir}/reports" />
</target>
<target name="lint">
<phplint>
<fileset dir="${src.dir}">
<include name="**/*.php" />
</fileset>
</phplint>
</target>
<target name="phpunit">
<phpunit printsummary="true">
<formatter type="xml" todir="${build.dir}/reports" />
<batchtest>
<fileset dir="${test.dir}">
<include name="**/*Test.php" />
</fileset>
</batchtest>
</phpunit>
</target>
<target name="build" depends="clean, prepare, lint, phpunit" />
</project>
CI/CD 平台集成
GitHub Actions
name: PHP CI
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
extensions: mbstring, intl, pdo_mysql
- name: Validate composer.json
run: composer validate
- name: Install dependencies
run: composer install --prefer-dist --no-progress
- name: Run tests
run: vendor/bin/phpunit
- name: Check coding standards
run: vendor/bin/phpcs --standard=PSR2 src/
- name: Static analysis
run: vendor/bin/phpstan analyse src/ --level=max
GitLab CI
image: php:8.1
stages:
- test
- build
- deploy
cache:
paths:
- vendor/
before_script:
- apt-get update && apt-get install -y git unzip
- docker-php-ext-install pdo_mysql
- curl -sS https://getcomposer.org/installer | php
- php composer.phar install
phpunit:
stage: test
script:
- vendor/bin/phpunit --configuration phpunit.xml
artifacts:
reports:
junit: build/reports/phpunit.xml
phpcs:
stage: test
script:
- vendor/bin/phpcs --standard=PSR2 --report=full src/
phpstan:
stage: test
script:
- vendor/bin/phpstan analyse src/ --level=max
build:
stage: build
script:
- composer install --no-dev --optimize-autoloader
- php artisan optimize
artifacts:
paths:
- .
expire_in: 1 week
deploy:
stage: deploy
script:
- rsync -avz --delete ./ user@server:/var/www/project/
- ssh user@server "cd /var/www/project && php artisan migrate"
only:
- main
Jenkins Pipeline
pipeline {
agent any
tools {
php 'PHP8.1'
composer 'latest'
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Install Dependencies') {
steps {
sh 'composer install --prefer-dist'
}
}
stage('Code Style Check') {
steps {
sh 'vendor/bin/phpcs --standard=PSR2 src/'
}
}
stage('Static Analysis') {
steps {
sh 'vendor/bin/phpstan analyse src/ --level=max'
}
}
stage('Unit Tests') {
steps {
sh 'vendor/bin/phpunit'
}
}
stage('Build Artifacts') {
steps {
sh '''
composer install --no-dev --optimize-autoloader
php artisan config:cache
php artisan route:cache
'''
}
}
}
post {
success {
archiveArtifacts artifacts: 'build/**/*.zip'
junit 'build/reports/phpunit.xml'
}
}
}
构建流程示例
完整的自动化构建脚本 (build.sh)
#!/bin/bash
set -e
echo "Starting build process..."
# 1. 代码检查
echo "Running PHP CodeSniffer..."
vendor/bin/phpcs --standard=PSR2 --extensions=php src/ tests/
# 2. 静态分析
echo "Running PHPStan..."
vendor/bin/phpstan analyse src/ --level=max
# 3. 运行测试
echo "Running PHPUnit..."
vendor/bin/phpunit --coverage-html build/coverage
# 4. 清理和优化
echo "Optimizing for production..."
composer install --no-dev --optimize-autoloader
# 5. 生成生产环境配置
php artisan config:cache
php artisan route:cache
php artisan view:cache
# 6. 打包
echo "Creating deployment package..."
tar -czf build/deploy.tar.gz \
--exclude='vendor/*' \
--exclude='node_modules/*' \
--exclude='.git/*' \
--exclude='tests/*' \
--exclude='storage/logs/*' \
.
echo "Build completed successfully!"
常用工具集成
PHPUnit 配置
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
colors="true"
verbose="true"
failOnRisky="true"
failOnWarning="true">
<testsuites>
<testsuite name="Unit Tests">
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="Feature Tests">
<directory>tests/Feature</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
<report>
<html outputDirectory="build/coverage"/>
<xml outputDirectory="build/coverage-xml"/>
</report>
</coverage>
</phpunit>
PHPStan 配置
parameters:
level: max
paths:
- src
excludePaths:
- tests
checkMissingIterableValueType: true
checkGenericClassInNonGenericObjectType: true
Docker 构建示例
FROM php:8.1-fpm as builder
WORKDIR /app
# 安装依赖
RUN apt-get update && apt-get install -y \
git \
unzip \
libpq-dev \
&& docker-php-ext-install pdo_pgsql
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
COPY . .
# 安装依赖并优化
RUN composer install --no-dev --optimize-autoloader
# 第二阶段:生产镜像
FROM php:8.1-fpm-alpine
WORKDIR /app
COPY --from=builder /app .
# 健康检查
HEALTHCHECK --interval=30s --timeout=3s \
CMD php-fpm -t || exit 1
EXPOSE 9000
CMD ["php-fpm"]
最佳实践建议
- 分阶段构建:检查、测试、构建、部署分阶段执行
- 缓存依赖:缓存 Composer 依赖加速构建
- 并行执行:可以并行运行 linting 和测试
- 失败快速:如果关键步骤失败,立即停止构建
- 版本控制:使用构建编号或 Git SHA 标记版本
- 通知机制:构建失败时发送通知(Slack、邮件等)
- 环境隔离:为开发、测试、生产环境分别配置
选择合适的工具取决于你的项目需求、团队规模和基础设施,对于小型项目,Composer 脚本可能就足够了;对于大型项目,建议使用专业的 CI/CD 平台。
版权声明:足球直播
足球直播
足球直播
足球直播
足球直播
中超直播
中超直播
中超直播
中超直播
中超直播
英超直播
英超直播
英超直播
英超直播
英超直播
德甲直播
德甲直播
德甲直播
德甲直播
德甲直播
西甲直播
西甲直播
西甲直播
西甲直播
西甲直播
意甲直播
意甲直播
意甲直播
意甲直播
意甲直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
足球直播
足球直播
足球直播
足球直播
足球直播
中超直播
中超直播
中超直播
中超直播
中超直播
英超直播
英超直播
英超直播
英超直播
英超直播
德甲直播
德甲直播
德甲直播
德甲直播
德甲直播
西甲直播
西甲直播
西甲直播
西甲直播
西甲直播
意甲直播
意甲直播
意甲直播
意甲直播
意甲直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
足球直播
足球直播
足球直播
足球直播
足球直播
中超直播
中超直播
中超直播
中超直播
中超直播
英超直播
英超直播
英超直播
英超直播
英超直播
德甲直播
德甲直播
德甲直播
德甲直播
德甲直播
西甲直播
西甲直播
西甲直播
西甲直播
西甲直播
意甲直播
意甲直播
意甲直播
意甲直播
意甲直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播