这两天在捣鼓 Gitlab
的 .gitlab-ci.yml
文件的编写,做个简单的使用记录
- 项目安装代码规范检查包:
squizlabs/php_codesniffer
composer.json
文件内 scripts
块增加便捷命令
"lint": "phpcs --standard=PSR1,PSR2,./ruleset.xml --ignore=config/us-zipcode.php app/ routes/ config/ ",
"lint-fix": "phpcbf --standard=PSR1,PSR2 --ignore=config/us-zipcode.php app/ routes/ config/ "
使用命令:composer run lint
即可执行代码检查 ,检出的错误:安装提示修复即可
项目根目录 编写 .gitlab-ci.yml
文件
image: php:7.2
services:
- mysql:latest
variables:
MYSQL_DATABASE: "newshop_testing"
MYSQL_ROOT_PASSWORD: "root"
VERSION: $CI_COMMIT_SHORT_SHA
GIT_DEPTH: 1
cache:
paths:
- vendor/
- node_modules/
before_script:
# Update packages 更新包
- apt-get update -yqq
# # Upgrade to Node 7 更新node 7
# - curl -sL https://deb.nodesource.com/setup_7.x | bash -
#
# # Install dependencies 安装依赖
- apt-get install git libcurl4-gnutls-dev libicu-dev libmcrypt-dev libvpx-dev libjpeg-dev libpng-dev libxpm-dev zlib1g-dev libfreetype6-dev libxml2-dev libexpat1-dev libbz2-dev libgmp3-dev libldap2-dev unixodbc-dev libpq-dev libsqlite3-dev libaspell-dev libsnmp-dev libpcre3-dev libtidy-dev -yqq
# # Install php extensions 安装php扩展
# - pecl install mcrypt-1.0.1
# - docker-php-ext-enable mcrypt
- docker-php-ext-install mbstring pdo_mysql curl json intl gd xml zip bz2 opcache
# Install Composer and project dependencies 安装composer和项目依赖
- cd platform
- curl -sS https://getcomposer.org/installer | php
- php -r "readfile('https://getcomposer.org/installer');" | php
- php composer.phar install
# Copy over testing configuration 复制测试配置。
# Don't forget to set the database config in .env.testing correctly 不要忘记在.env中设置数据库配置。测试正确
# DB_HOST=mysql
# DB_DATABASE=project_name
# DB_USERNAME=root
# DB_PASSWORD=secret
- cp .env.testing.example .env.testing
- cp .env.example .env
- php artisan key:generate
- php artisan config:cache
- php artisan migrate
- php artisan db:seed
- php artisan admin:publish --force --assets
store-codestyle:
stage: test
script:
- php vendor/bin/composer run lint
phpunit:
stage: test
script:
- php vendor/bin/phpunit --coverage-text --colors=never
推送代码即可在gitlab CI/CD模块的流水线内看到事件在运行了
检查代码格式 和phpunit
运行中有任何一个错误,将会显示 ×
通过则为绿色的 √
点击可看见详情
自动化部署项目,docker
镜像等此篇文章不介绍,.gitlab-ci.yml
文件内含义不介绍,需要有一定的 docker
基础,可前往 docker
官网查看文档