简体   繁体   English

如何使用 Docker 在我的 CI 中运行 jshint

[英]How can I run jshint in my CI using Docker

I am trying to create a task in our Azure pipeline to validate our javascript. We have a node container which performs an npm install when spun up:我正在尝试在我们的 Azure 管道中创建一个任务来验证我们的 javascript。我们有一个节点容器,它在启动时执行 npm 安装:

  node:
    image: node:12-alpine
    user: "node"
    working_dir: /home/node/app
    environment:
      - NODE_ENV=development
    volumes:
      - ./:/home/node/app
    expose:
      - "8081"
    command: "npm install"

To perform my task I have created a make command in the Makefile:为了执行我的任务,我在 Makefile 中创建了一个 make 命令:

js-check: ## Run Jshint
    docker-compose run node npm install && npm run jshint

Which I then call in the build job as follows:然后我在构建作业中调用如下:

- script: make js-check
            displayName: 'Run JSHint'

Locally when I call the make js-check it performs the install, followed by the jshint which outputs 0 vulnerabilities found.当我在本地调用 make js-check 时,它会执行安装,然后是 jshint,它会输出 0 个发现的漏洞。 However when the pipeline reaches this task remotely it fails claiming missing write access to /home/node/app但是,当管道远程到达此任务时,它无法声明缺少对 /home/node/app 的写访问权限

npm WARN checkPermissions Missing write access to /home/node/app
npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /home/node/app
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/home/node/app'
npm ERR!  [Error: EACCES: permission denied, access '/home/node/app'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/home/node/app'
npm ERR! }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

Your Makefile runs two commands;您的 Makefile 运行两个命令; the shell interprets the && marker before it gets to Docker. That command is equivalent to: shell 在到达 Docker 之前解释&&标记。该命令等效于:

js-check: ## Run Jshint
        docker-compose run node npm install
        npm run jshint # (without Docker)

It looks like your environment already has Node installed.看起来您的环境已经安装了 Node。 You need to resolve the permissions issues (generally the CI system will check out source trees as a user that can run commands), and then you can use this native Node:需要解决权限问题(一般CI系统会以可以运行命令的用户身份检出源码树),然后就可以使用这个原生Node了:

js-install: package.json
        npm install
js-check: js-install
        npm run jshint

This has the advantage of only depending on normal Javascript development tools;这样的好处是只依赖于普通的Javascript开发工具; you don't need the extra docker-compose.yml file or administrator privileges just to run your unit tests.你不需要额外的docker-compose.yml文件或管理员权限来运行你的单元测试。

If you really need to run this in Docker, you can either run this as two separate commands, or make the single container command be a shell that can interpret the && itself:如果您确实需要在 Docker 中运行它,您可以将其作为两个单独的命令运行,或者将单个容器命令设为可以解释&&本身的 shell:

js-install1: package.json docker-compose.yml
        docker-compose run node npm install
js-check1: js-install1
        docker-compose run node npm run jshint
js-check2: package.json docker-compose.yml
        docker-compose run node \
          sh -c "npm install && npm run jshint"

Above error is because /home/node directory is owned by the node user in the default node image.上面的错误是因为/home/node目录在默认节点映像中归节点用户所有。 The /app directory is created and owned by root. /app目录由 root 创建和拥有。 See this open issue about above error for more information.有关更多信息,请参阅有关上述错误的未解决问题

I reproduced the same error with your compose file.我用你的撰写文件重现了同样的错误。 When i changed the user to root.当我将用户更改为 root 时。 The error was gone.错误消失了。

So you can try changing the user to root instead of node in your compose file.因此,您可以尝试在撰写文件中将用户更改为root而不是node

 node:
    image: node:12-alpine
    user: "root"
    working_dir: /home/node/app

As David pointed out, you also need to change your makefile to docker-compose run node sh -c "npm install && npm run jshint" .正如大卫指出的那样,您还需要将 makefile 更改为docker-compose run node sh -c "npm install && npm run jshint" if you want to run the commands in docker.如果你想运行 docker 中的命令。

Another workaround is to build and run your container from a dockerfile instead of the compose file.另一种解决方法是从 dockerfile 而不是 compose 文件构建和运行容器。 See below simple example dockerfile.请参阅下面的简单示例 dockerfile。

from node:12-alpine
ENV NODE_ENV=development
RUN mkdir -p /home/node/app 
RUN chown -R node:node /home/node/app
USER node
WORKDIR /home/node/app
COPY . ./
RUN npm install
CMD [ "npm", "run", "jshint" ]

Then change the Makefile like example:然后更改 Makefile 示例:

js-check: ## Run Jshint
    docker build --tag nodejshint:1.0 . && docker run --detach --name jshintContainer nodejshint:1.0

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

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