我允许自己发布一个支持主题,因为我被一个Laravel项目的gitlab的CI配置文件的问题卡住了几天
我的.gitlab-ci.yml文件
stages:
- build
- test
- deploy
- prod
cache:
paths:
- vendor/
- node_modules/
- .yarn
build env file:
stage: build
image: alpine:latest
script:
- cp .env.example .env
- sed -i “s/{{DB_USER}}/$MYSQL_USER/g” .env
- sed -i “s/{{DB_PASSWORD}}/$MYSQL_ROOT_PASSWORD/g” .env
artifacts:
paths:
- .env.example
composer:
stage: build
image: edbizarro/gitlab-ci-pipeline-php:8.0-alpine
cache:
key: ${CI_COMMIT_REF_SLUG}-composer
paths:
- vendor/
script:
- composer install --prefer-dist --no-ansi --no-interaction --no-progress --no-scripts
artifacts:
expire_in: 1 month
paths:
- vendor/
- .env
yarn:
stage: build
image: edbizarro/gitlab-ci-pipeline-php:8.0-alpine
cache:
key: ${CI_COMMIT_REF_SLUG}-npm
paths:
- node_modules/
# - Modules/mon-mondules/node_modules/ si utilisation de modules supplementaire
script:
- yarn config set cache-folder .yarn
- yarn install --pure-lockfile
# - cd Modules/mon-modules/node_modules/ && npm install $$ npm run production
artifacts:
expire_in: 1 month
paths:
- public/css/
- public/js/
- public/modules/
- public/mix-manifest.json
testing:
stage: test
services:
- mysql:5.7
image: edbizarro/gitlab-ci-pipeline-php:8.0-alpine
script:
# - ./vendor/bin/security-checker security:check
- ./vendor/phpunit/phpunit --no-coverage
deploy:
stage: deploy
image: edbizarro/gitlab-ci-pipeline-php:8.0-alpine
dependencies:
- build env file
script:
- php artisan key:generate
- php artisan migrate:refresh --seed
deploying to prod:
stage: prod
image: edbizarro/gitlab-ci-pipeline-php:8.0-alpine
script:
- echo "Deploy all the things!"
when: manual
only:
- master前两个阶段运行良好,但在测试阶段,管道失败并出现以下错误:
SQLSTATE[HY000] [1045] Access denied for user 'root'@'172.17.0.4' (using password: NO) (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = 'BASE TABLE')
我不知道如何解决这个问题,你能帮我吗?
发布于 2021-03-18 13:44:20
当你在.Gitlab-CI.yml中设置变量时,runner知道它们,但是Laravel不知道。你必须把这些变量放在你的.env文件中。我这样做的方法是在.env.example中放入类似“{{DB_PASSWORD}}”的值
# .env.example
# ...
DB_USER=“{{DB_USER}}”
DB_PASSWORD=“{{DB_PASSWORD}}
# ...然后,我用实际值替换这些占位符。在使用实际数据库的情况下(如在生产管道中),我使用CI变量,您可以在项目或组的CI设置中定义这些变量。
testing:
stage: test
services:
- mysql:5.7
image: edbizarro/gitlab-ci-pipeline-php:8.0-alpine
script:
- cp .env.example .env
- sed -i “s/{{DB_USER}}/$MYSQL_USER/g” .env
- sed -i “s/{{DB_PASSWORD}}/$MYSQL_ROOT_PASSWORD/g” .env
- php artisan key:generate
- php artisan migrate:refresh --seed
- ./vendor/phpunit/phpunit --no-coverage
#- php artisan code:analysesed命令可以做很多事情,但是这里我们只关心字符串替换。语法sed -i “s/{{DB_USER}}/g” .env的意思是:
在分隔符说我们正在做项目之前,
-i告诉sed编辑文件内联s,在第一个分隔符是要被替换的值之后。以这种方式使用占位符很容易,因为它们是唯一且易于识别的,下一个分隔符后面的项就是我们要用替换的项,末尾的g表示“全局”:replace instances in
.env是我们正在处理的文件当使用没有sed的镜像时(或者您只是想将配置生成与部署步骤分开),您可以将.env文件作为工件上传:
stages:
- build
- test
build env file:
stage: build
image: alpine:latest
script:
- cp .env.example .env
- sed -i “s/{{DB_USER}}/$MYSQL_USER/g” .env
- sed -i “s/{{DB_PASSWORD}}/$MYSQL_ROOT_PASSWORD/g” .env
artifacts:
paths:
- .env.example
deploy:
stage: deploy
image: edbizarro/gitlab-ci-pipeline-php:8.0-alpine
dependencies:
- build env file
script:
- php artisan key:generate
- php artisan migrate:refresh --seed
# ...这里有很多东西需要拆开。首先,这里使用的图像实际上并不重要,只要它有sed可用。高山linux是一个非常小(而且很快)的linux发行版,我经常用它来完成这样的任务。修改.env文件后,我们将其作为作业工件上传到Gitlab。
默认情况下,后续阶段中的所有作业都将下载以前阶段的所有工件。我们可以用dependencies关键字来控制它。当作业具有此关键字时,将仅下载这些作业中的工件。如果不需要工件,可以使用dependencies: []。因此,deploy作业将从build env file作业中获取.env文件,并将其用于artisan命令。
https://stackoverflow.com/questions/66678238
复制相似问题