node.js - 如何在 Gitlab 中为 nodejs 应用程序运行测试

标签 node.js continuous-integration gitlab gitlab-ci gitlab-ci-runner

我是 gitlab 的新手,所以对它了解不多。

我正在尝试设置 gitlab 以在构建图像之前运行测试。我已经设置了一个配置正确的私有(private)运行者,我可以构建图像,但如果我运行 npm 命令来测试代码,它就不起作用。这是我的 gitlab-ci.yml 文件。

image: docker:latest
variables:
  IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME
services:
- docker:dind
before_script:
  - docker login -u gitlab-ci-token -p "$CI_BUILD_TOKEN" "$CI_REGISTRY"

stages:
  - build-container-test
  - test-run


build-container-test:
  stage: build-container-test
  script:
    - docker build -t "$IMAGE_TAG" .
    - docker push "$IMAGE_TAG"
  only:
    - test

test-run:
  stage: test-run
  script:
    - npm run test
  only:
    - test

这是我在运行时遇到的错误。 enter image description here

我是否需要在 gitlab runner 上单独安装 npm 才能运行它,还是我在这里遗漏了什么?

最佳答案

在 CI 中运行测试的 gitlab-ci 文件的简单示例:

image: node:8.9.4

stages:
  - npm
  - test

npm:
  stage: npm
  script:
    - npm config set registry ${CI_NPM_REGISTRY}
    - npm install
  cache:
    paths:
      - node_modules/
  artifacts:
    expire_in: 1 days
    when: on_success
    paths:
      - node_modules/

test:
  stage: test
  dependencies:
    - npm
  script:
    - npm test

更新

我将管道一分为二,第一个用于安装所有依赖项,在此作业中使用了工件,工件用于在作业之间共享数据(您可以看到 documentation ) .然后在测试作业中,您可以运行测试。

回答关于环境变量的问题:

  • 是的,您可以使用环境变量,转到 CI/CD 配置,在环境变量中您可以定义自己的变量并标记为 protected 或不 protected (要使用此环境变量,请参阅 documentation)

关于node.js - 如何在 Gitlab 中为 nodejs 应用程序运行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54775558/

相关文章:

git - 无法通过 SSH 连接到自定义端口上的 Gitlab 容器

javascript - 如何在node.js上引用 Handlebars 中带有空格的元素?

node.js - 错误: Cannot read property 'send' of null

javascript - Nodejs Electron : focusOut event?

node.js - NodeJS逐个文件读取巨大的目录

Azure 应用服务 + Docker 自动部署?

ios - 使用 Xcode 机器人进行持续集成 : shared schemes vs. 构建配置

c++ - GitHub 上的项目需要带有 Qt4、sqlite3、cmake、git、gcc 的托管 CI 服务器

git - 如何防止 Gitlab 在分支 merge 时创建额外的 merge 提交

node.js - npm:找不到命令 - Gitlab CI 特定运行程序