gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:59:32 28 4
gpt4 key购买 nike

我是 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com