mysql - 在连接到真实数据库的 gitlab CI 中运行单元测试

标签 mysql node.js gitlab gitlab-ci gitlab-ci-runner

我想在 Gitlab CI 上运行单元测试。我正在使用nodejs、jest、mysql。我正在尝试在 CI 上运行 mysql 以在运行单元测试时连接到同一服务器。为了简单起见,我还插入了一些虚拟数据。

当我在本地运行测试时,测试运行时出现一些错误,但测试通过,但出现一些运行后错误。但是,在 gitlab CI 上,测试脚本无法连接到 mysql 服务。

以下是我的gitlab-ci.yml

services:
  - mysql:8.0
variables:
  MYSQL_DATABASE: test_database
  MYSQL_ROOT_PASSWORD: root

stages:
  - build
build:
  stage: build
  services:
    - mysql
  only:
    refs:
      - master
  image: klvenky/node-12-alpine-docker:latest
  cache:
    key: "$CI_COMMIT_REF_SLUG"
    paths:
      - .yarn
  script:
    - yarn config set cache-folder $PWD/.yarn
    - export FROM_GIT_URL="git.ssh://git"
    - export TO_GIT_URL="https://gitlab-ci-token:$CI_JOB_TOKEN"
    - sed -i "s#$FROM_GIT_URL#$TO_GIT_URL#" package.json yarn.lock
    - yarn --ignore-optional --pure-lockfile --prefer-offline
    - yarn test

我的存储库托管在 Gitlab here 上。 请让我知道出了什么问题。

最佳答案

有关管道中 mysql 的错误消息指出:

ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client

Node 包mysqljs不支持MySQL 8的默认身份验证方法。

您可以找到答案和可能的解决方案here .

gitlab-ci 文件中的服务定义也存在问题。正如评论中提到的,您已使用相同的名称指定了两次服务。仅定义一次(使用正确的命令替换)应该有效:

services:
  - mysql:8.0
    alias: mysql
    command: [ "--default-authentication-plugin=mysql_native_password" ]

variables:
  MYSQL_DATABASE: test_database
  MYSQL_ROOT_PASSWORD: root

stages:
  - build
build:
  stage: build
  only:
    refs:
      - master
  image: klvenky/node-12-alpine-docker:latest
  cache:
    key: "$CI_COMMIT_REF_SLUG"
    paths:
      - .yarn
  script:
    - yarn config set cache-folder $PWD/.yarn
    - export FROM_GIT_URL="git.ssh://git"
    - export TO_GIT_URL="https://gitlab-ci-token:$CI_JOB_TOKEN"
    - sed -i "s#$FROM_GIT_URL#$TO_GIT_URL#" package.json yarn.lock
    - yarn --ignore-optional --pure-lockfile --prefer-offline
    - yarn test
 

关于mysql - 在连接到真实数据库的 gitlab CI 中运行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64043552/

相关文章:

php - 如何将 jQuery 回调函数的值传递给 PHP 脚本以进行进一步验证?

PHP-获取前一行的值

javascript - ReactJs:无效的 DOM 属性 `classname` 错误

linux - 如何在 CentOS 6 上更改 GitLab 端口?

PHP CSV 上传脚本,使用新行?

MYSQL基于多级产品的产品总销售额

node.js - 在 Compute Engine 上连接 Redis 时 Google Cloud App Engine 标准环境 Node JS 连接超时

node.js - 模板网址不起作用

docker - 在 Gitlab CI 上更改 Xdebug 模式 PHP Docker

linux - 如何更改 gitlab-ci runner 构建路径