如何 运行 在 Gitlab 中对 nodejs 应用程序进行测试

How to run a test in Gitlab for nodejs application

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

我正在尝试将 gitlab 设置为 运行 在构建图像之前进行测试。我已经设置了一个配置正确的私有 运行ner,我可以构建图像,但是如果我 运行 一个 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

这是我在 运行 处理它时遇到的错误。

我需要在 gitlab 运行ner 上单独安装 npm 到 运行 还是我在这里遗漏了什么?

一个简单的例子gitlab-ci文件到运行测试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

更新

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

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

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