![](/img/trans.png)
[英]how to pull laravel project from gitlab to remote server by gitlab-ci.yml
[英]Problem .gitlab-ci.yml [ Mysql connection error]
我允許自己發布一個支持主題,因為我在 gitlab 的 Laravel 項目的 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')
我不知道如何解決這個問題,你能幫我嗎?
當您在.Gitlab-CI.yml
中設置變量時,跑步者會知道它們,但 Laravel 不會。 您必須將這些變量放在您的.env
文件中。 我這樣做的方法是將“ {{DB_PASSWORD}}
”之類的值放入.env.example
:
# .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:analyse
sed
命令可以做很多事情,但這里我們只關心字符串替換。 語法sed -i “s/{{DB_USER}}/g”.env
表示:
-i
告訴 sed 內聯編輯文件s
表示我們正在做替換。g
表示“全局”:替換文件中的所有實例。.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
可用。 Alpine linux 是一款超小(且速度快)的 linux 發行版,我經常用於此類任務。 修改.env
文件后,我們將其作為作業工件上傳到 Gitlab。
默認情況下,后續階段的所有作業都會下載之前階段的所有工件。 我們可以使用dependencies
關鍵字來控制它。 當作業具有此關鍵字時,只會下載來自這些作業的工件。 如果不需要工件,您可以使用dependencies: []
。 因此, deploy
作業將從build env file
作業中獲取.env
文件,並使其可用於artisan
命令。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.