在Atlassian生态里,Bitbucket Pipelines是最常用的内置CI工具。对PHP开发者来说,不需要额外搭建Jenkins或GitLab Runner,只需在仓库根目录放一个bitbucket-pipelines.yml,就可以在每次推送时自动拉取PHP镜像、安装依赖并运行测试。

一、基础配置文件结构
Bitbucket Pipelines使用YAML来描述流水线。一个最基础的PHP单元测试配置如下:
version: 2.1
pipelines:
default:
- step:
name: 运行PHP单元测试
image: php:8.2-cli
caches:
- composer
script:
- apt-get update && apt-get install -y unzip git
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
- composer install --no-interaction --prefer-dist
- ./vendor/bin/phpunit
二、关键配置项说明
1. image指定PHP版本
可以使用官方PHP镜像,例如<code>php:8.1-cli</code>或<code>php:8.2-fpm</code>。如果项目需要扩展,可选择<code>php:8.2-apache</code>或在script里自行编译。
2. caches缓存composer
把composer加入caches能避免每次都重新下载vendor,显著提升速度。注意缓存键名要在文件顶层声明。
3. script执行顺序
script里的命令按顺序执行,任何一条失败都会导致该step标红。建议先装系统依赖,再装PHP依赖,最后跑测试。
三、多步骤流水线示例
当项目需要代码规范检查、测试、部署分开时,可写成多个step:
version: 2.1
pipelines:
default:
- step:
name: 代码规范与单元测试
image: php:8.2-cli
caches:
- composer
script:
- apt-get update && apt-get install -y unzip git
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
- composer install --no-interaction
- ./vendor/bin/php-cs-fixer fix --dry-run
- ./vendor/bin/phpunit
- step:
name: 部署到测试环境
deployment: test
script:
- echo 开始部署
- rsync -avz --delete ./ user@192.168.0.1:/var/www/html/
四、常见问题
- 权限不足:在script里用chmod补权限,或改用合适基础镜像。
- 扩展缺失:用<code>docker-php-ext-install</code>安装pdo_mysql等扩展。
- 测试太慢:开启composer缓存并并行拆分step。
Bitbucket Pipelines免费额度有限,私有仓库注意每月构建分钟数。
五、本地调试建议
可用如下PHP脚本模拟简单校验逻辑,确认命令本身没问题再写进yml:
<?php
// 模拟检查环境变量是否齐全
$required = ['BITBUCKET_COMMIT', 'BITBUCKET_BRANCH'];
foreach ($required as $key) {
if (!isset($_ENV[$key])) {
fwrite(STDERR, "缺少环境变量: $keyn");
exit(1);
}
}
echo "环境变量检查通过n";
将上述思路落到bitbucket-pipelines.yml中,PHP项目就能平稳接入Atlassian生态的持续集成流程。
PHPBitbucket_PipelinesCI修改时间:2026-07-27 21:30:25