简体   繁体   English

使用 Azure 管道和 Docker 运行 mocha 测试

[英]Running mocha test with Azure pipelines and Docker

I am very new to CICD.我对 CICD 很陌生。

I have an Azure web app running a Docker container built from an Express Nodejs image.我有一个 Azure web 应用程序运行一个 Docker 容器,该容器是从 Express Nodejs 图像构建的。

My current flow is minimalistic.我目前的流程是简约的。 I have an Azure-pipelines.yml file that looks like我有一个 Azure-pipelines.yml 文件,看起来像

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'    

- task: AzureCLI@1
  inputs:
    azureSubscription: 'xxxxxx'
    scriptLocation: 'inlineScript'
    inlineScript: 'az acr build --registry registry123 --image image123:latest --file Dockerfile .'

And a dockerfile that looks like还有一个看起来像的 dockerfile

FROM node:10

WORKDIR /poc/microservices

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD [ "node", "server.js" ]

Now I have added some mocha tests that I can run with npm test , and these are running fine locally.现在我添加了一些 mocha 测试,我可以使用npm test运行这些测试,这些测试在本地运行良好。

Now I want to add these to the pipeline so that it doesnt build the image if the tests are failing.现在我想将这些添加到管道中,以便在测试失败时它不会构建图像。

I have a mocha.opts file where I defined --reporter mocha-junit-reporter and the tests are using an process.env.ENDPOINT variable.我有一个 mocha.opts 文件,我在其中定义--reporter mocha-junit-reporter并且测试使用的是process.env.ENDPOINT变量。

So my question is how do I add the command to the pipeline?所以我的问题是如何将命令添加到管道中?

Do I simply add我是否只是添加

- script: npm install
- script: npm test

to azure-pipelines.yml and then configure the ENDPOINT variable in the build variables in azure devOps portal?到 azure-pipelines.yml 然后在 azure devOps 门户的构建变量中配置 ENDPOINT 变量?

If so, is it not a problem that I am running then twice npm install ?如果是这样,我运行两次npm install不是问题吗? once in Azure, and another time in Docker?一次在 Azure,另一次在 Docker?

Running NPM install twice would only slows your deployment and would not cause any other problems if you don't copy node_modules into your Docker container.如果您不将node_modules复制到 Docker 容器中,则运行 NPM 安装两次只会减慢您的部署速度并且不会导致任何其他问题。

Make sure you copy only the source code to the docker container and leave node_modules out.确保只将源代码复制到 docker 容器中,并将node_modules排除在外。 You can add a .dockerignore file for that:您可以为此添加一个.dockerignore文件:

node_modules
npm-debug.log
Dockerfile
.dockerignore

This would make sure the packages are installed properly in the Docker container.这将确保软件包正确安装在 Docker 容器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM