【问题标题】:Problem .gitlab-ci.yml [ Mysql connection error]问题 .gitlab-ci.yml [Mysql 连接错误]
【发布时间】:2021-03-17 17:27:31
【问题描述】:

我允许自己发布一个支持主题,因为我被困在 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')

我不知道如何解决这个问题,你能帮帮我吗?

【问题讨论】:

    标签: laravel gitlab dockerfile gitlab-ci


    【解决方案1】:

    当你在 .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 命令。

    【讨论】:

    • 谢谢你的回答,但是这个“sed”命令在这个阶段测试中是“不支持的命令”它可以来自docker镜像吗?
    • 啊,它一定不在你的形象中。我将用替代方法编辑我的答案。
    • 看不懂,这个sed命令还是“不支持的命令”
    • 您能用您的.gitlab-ci.yml 文件现在包含的内容更新您的帖子吗?
    猜你喜欢
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 2020-02-14
    • 2019-03-14
    相关资源
    最近更新 更多