【问题标题】:Docker agent inside Jenkins - npm "cannot find module"Jenkins 内的 Docker 代理 - npm“找不到模块”
【发布时间】:2018-04-06 05:30:18
【问题描述】:

我正在努力实现以下构建步骤的自动化: - 使用 webpack 构建前端应用程序 - 对其进行测试

我正在使用启用了 blue-ocean 插件的 Jenkins,这里是 Jenkinsfile

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

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

我正在使用以下 Dockerfile

FROM node:latest

WORKDIR /app
COPY . /app

RUN npm install webpack -g && npm install

问题是运行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)

看起来命令是在主机上下文中执行的,而不是在容器上,因为手动运行就可以了:

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

这里是完整的詹金斯日志:Jenkins build log 这是一个存储库的链接:Source code

【问题讨论】:

    标签: node.js docker jenkins jenkins-pipeline


    【解决方案1】:

    我遇到了完全相同的问题。出于某种原因,Dockerfile 中的“RUN npm install”没有在 Jenkins 管道中生效,尽管当我手动构建映像时它运行良好。

    我通过运行“npm install”作为管道中的一个步骤使管道正常工作。因此,在“构建”阶段之前将其添加到您的 Jenkinsfile 中:

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

    我不知道为什么会发生这种情况,但这可能与 Jenkins 如何为 Docker 构建设置上下文有关。我希望其他人可以详细说明这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-17
      • 2014-08-24
      • 2015-12-16
      • 2016-04-29
      • 2014-11-18
      • 2015-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多