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

时间:2019-02-19 21:55:51

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

我是gitlab的新手,所以对此知识不多。

我正在尝试设置gitlab以在生成映像之前运行测试。我已经设置了一个私有转轮,该转轮已正确配置,并且可以生成图像,但是如果运行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

我是否需要在gitlabRunner上单独安装npm才能运行它,还是我在这里丢失了东西?

1 个答案:

答案 0 :(得分:1)

一个简单的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

更新

我将管道分成两部分,第一个用于安装所有依赖项,在此作业中使用 artifacts ,这些工件用于作业之间的共享数据(您可以看到{{ 3}})。然后,在测试作业中,您可以运行测试。

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

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