简体   繁体   English

Jenkins中的Docker代理-npm“找不到模块”

[英]Docker agent inside Jenkins - npm “cannot find module”

I'm working on automation of following build steps: - building frontend application with webpack - running tests on it 我正在按照以下构建步骤进行自动化:-使用webpack构建前端应用程序-在其上运行测试

I am using Jenkins with blue-ocean plugin enabled, here is Jenkinsfile : 我正在使用启用了blue-ocean插件的Jenkins,这是Jenkinsfile

Jenkinsfile:pipeline {
  agent {
    dockerfile {
      filename 'Dockerfile'
    }

  }
  stages {
    stage('Build') {
      steps {
        sh 'npm run build'
      }
    }
  }
}

I'm using following Dockerfile 我正在使用以下Dockerfile

FROM node:latest

WORKDIR /app
COPY . /app

RUN npm install webpack -g && npm install

The problem is that when running npm run build it can not find webpack: 问题是,当运行npm run build它找不到webpack:

> webpack --config webpack-production.config.js --progress --colors

module.js:529
    throw err;
    ^

Error: Cannot find module 'webpack'
    at Function.Module._resolveFilename (module.js:527:15)
    at Function.Module._load (module.js:476:23)
    at Module.require (module.js:568:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/var/lib/jenkins/workspace/l-ui-webpack-example_master-IXSLD4CQSVAM2DRFHYHOYUANEHJ73R5PUGW4BMYVT5WPGB6ZZKEQ/webpack-production.config.js:1:79)

It looks like commands are being executed in host context, not on container as manual running works just fine: 看起来命令是在主机上下文中执行的,而不是在容器上执行的,因为手动运行就可以了:

docker build . -t sample
docker run sample npm run build

Here is full jenkins log: Jenkins build log Here is a link to a repository: Source code 这是完整的jenkins日志: Jenkins构建日志这是存储库的链接: 源代码

I had exactly the same issue. 我有完全一样的问题。 For some reason, 'RUN npm install' within the Dockerfile didn't take effect in the Jenkins pipeline although it worked well when I built the image manually. 出于某种原因,虽然我手动构建映像时效果很好,但Dockerfile中的“ RUN npm install”在Jenkins管道中并未生效。

I got the pipeline working by running "npm install" as a step in the pipeline. 我通过运行“ npm install”作为管道中的步骤来使管道正常工作。 So add this to your Jenkinsfile before the 'Build' stage: 因此,在“构建”阶段之前将其添加到您的Jenkinsfile中:

stage ('install app') {
    steps {
        sh "npm install"
    }
}

I don't know why this happens but it might have something to do with how Jenkins sets the context for the Docker build. 我不知道为什么会发生这种情况,但可能与Jenkins如何为Docker构建设置上下文有关。 I hope someone else can elaborate on this. 我希望其他人可以对此进行详细说明。

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

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