簡體   English   中英

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

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

我正在按照以下構建步驟進行自動化:-使用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日志: Jenkins構建日志這是存儲庫的鏈接: 源代碼

我有完全一樣的問題。 出於某種原因,雖然我手動構建映像時效果很好,但Dockerfile中的“ RUN npm install”在Jenkins管道中並未生效。

我通過運行“ npm install”作為管道中的步驟來使管道正常工作。 因此,在“構建”階段之前將其添加到您的Jenkinsfile中:

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

我不知道為什么會發生這種情況,但可能與Jenkins如何為Docker構建設置上下文有關。 我希望其他人可以對此進行詳細說明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM