gitlab ci runner 不知道 mocha

gitlab ci runner doesnt know mocha

我有一份 gitlab.ci 这份工作:

build_image:
  stage: build
  tags:
    - BUILD
  script:
    # recuperation de la derniere image
    - docker pull ${CI_REGISTRY_IMAGE}:${TAG} || true
    # build ap artir de la derniere image taggé
    - docker build --cache-from ${CI_REGISTRY_IMAGE}:${TAG}
      --rm
      --pull
      --tag ${CI_REGISTRY_IMAGE}:${CI_COMMIT_SHA}
      -f cicd/Dockerfile
      .
    - docker push ${CI_REGISTRY_IMAGE}:${CI_COMMIT_SHA}
  only:
    - master
    - develop
    
test_api_image:
  image: ${CI_REGISTRY_IMAGE}:${CI_COMMIT_SHA}
  stage: testbuild
  variables:
    #on force la connexion au service local
    MONGODB_URL: mongodb://mongo:27017/BDD
  script:
    - npm run cicdtest
  tags:
    - TEST
  only:
    - master
    - develop 

首先,在构建作业中,我们使用 Dockerfile 构建镜像并将其推送到 nexus。

在接下来的工作中,gitlab 运行ner 拉取此图像,并使用“npm 运行 cicdtest”启动 mocha 测试 我们发现了这个错误。

$ npm run cicdtest
> api@0.1.0 cicdtest /builds/data/api
> mocha test/api/**/tests.js --file test/helper --reporter list --exit
sh: mocha: command not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT

在我的桌面上,我在本地拉取相同的图像进行测试,然后 运行 并进入容器。当我执行“npm 运行 cicdtest”时我没有问题。

有什么想法吗?

有关信息,这是我的 Dockerfile:

FROM centos:latest

RUN mkdir -p /var/www/myapp
WORKDIR /var/www/myapp
RUN yum update -y \
&& yum install -y gcc gcc-c++ make \
&& curl -sL https://rpm.nodesource.com/setup_14.x | bash - \
&& yum install -y nodejs

COPY ["package.json", "package-lock.json*", "./"]

RUN npm install

COPY . .

EXPOSE 3000

CMD [ "node" , "./src/app.js"]

我的 package.json 包含:

  "dependencies": {
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "crypto-js": "^4.0.0",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "mongoose": "^5.10.13",
    "mustache": "^4.2.0",
    "nodemailer": "^6.6.0",
    "nodemon": "^2.0.6",
    "restify": "^8.5.1",
    "web-push": "^3.4.4"
  },
  "devDependencies": {
    "chai": "^4.2.0",
    "chai-http": "^4.3.0",
    "eslint": "^7.26.0",
    "eslint-plugin-chai-expect": "^2.2.0",
    "husky": "^6.0.0",
    "mocha": "^8.3.0",
    "supertest": "^6.1.3"
  }

RUN npm install之前添加这一行:

RUN npm install --global mocha

mocha 命令适用于您的本地,因为您过去在全局安装了它。

解决方案是添加 GIT_STRATEGY None 因为我们正在测试带有代码源的构建图像,所以我们不需要 git 再检查一次来源。

test_api_image:
  image: ${CI_REGISTRY_IMAGE}:${CI_COMMIT_SHA}
  stage: testbuild
  variables:
    # We do not need GitLab to clone the source code.
    GIT_STRATEGY: none 
  script:
    - npm run cicdtest
  tags:
    - TEST
  only:
    - master
    - develop