简体   繁体   中英

npm install fails in Jenkins pipeline even though I configured withNpm step to use a custom .nrpmrc file

This is my pipeline:

pipeline {
    agent any
    tools {nodejs "node10"}

    stages {
        stage('Build') {
            steps {
                withNPM(npmrcConfig: 'xxx') {
                sh "npm config ls"
                sh "npm install"
                sh "ng build"
                }
            }
        }
    }
}

The Jenkins server doesn't have internet connection . It must download the npm artifacts from a Nexus server.

I have uploaded this managed file with the npm config:

registry=http://xxxx:8081/nexus/repository/central-npm-registry/

central-npm-registry is a proxy against the npm registry.

NOTICE: If I use this.npmrc file from my local machine it works ok.

When building from Jenkins I see that apparently the npm config file is setup correctly but the installations fails, any clues?

Using settings config with name central-npm-registry
A workscape local .npmrc already exists and will be overwrriten for the build.
Writing .npmrc file: /root/.jenkins/workspace/central-npm-registry/myapp/.npmrc
[Pipeline] {
[Pipeline] sh
[myapp] Running shell script
+ npm config ls
; cli configs
metrics-registry = "http://xxxxx:8081/nexus/repository/central-npm-registry/"
scope = ""
user-agent = "npm/6.4.1 node/v11.0.0 linux x64"

; project config /root/.jenkins/workspace/central-npm-registry/myapp/.npmrc
registry = "http://xxxxx:8081/nexus/repository/central-npm-registry/"

; node bin location = /root/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node10/bin/node
; cwd = /root/.jenkins/workspace/central-npm-registry/myapp
; HOME = /root
; "npm config ls -l" to show all defaults.

[Pipeline] sh
[myapp] Running shell script
+ npm install

> node-sass@4.10.0 install /root/.jenkins/workspace/central-npm-registry/myapp/node_modules/node-sass
> node scripts/install.js

Downloading binary from https://github.com/sass/node-sass/releases/download/v4.10.0/linux-x64-67_binding.node
Cannot download "https://github.com/sass/node-sass/releases/download/v4.10.0/linux-x64-67_binding.node": 

connect ECONNREFUSED 140.82.118.3:443

Hint: If github.com is not accessible in your location
      try setting a proxy via HTTP_PROXY, e.g. 

      export HTTP_PROXY=http://example.com:1234

or configure npm proxy via

      npm config set proxy http://example.com:8080

> node-sass@4.10.0 postinstall /root/.jenkins/workspace/central-npm-registry/myapp/node_modules/node-sass
> node scripts/build.js

Building: /root/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node10/bin/node /root/.jenkins/workspace/central-npm-registry/myapp/node_modules/node-gyp/bin/node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library=
gyp info it worked if it ends with ok
gyp verb cli [ '/root/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node10/bin/node',
gyp verb cli   '/root/.jenkins/workspace/central-npm-registry/myapp/node_modules/node-gyp/bin/node-gyp.js',
gyp verb cli   'rebuild',
gyp verb cli   '--verbose',
gyp verb cli   '--libsass_ext=',
gyp verb cli   '--libsass_cflags=',
gyp verb cli   '--libsass_ldflags=',
gyp verb cli   '--libsass_library=' ]
gyp info using node-gyp@3.8.0
gyp info using node@11.0.0 | linux | x64
gyp verb command rebuild []
gyp verb command clean []
gyp verb clean removing "build" directory
gyp verb command configure []
gyp verb check python checking for Python executable "python2" in the PATH
gyp verb `which` succeeded python2 /usr/bin/python2
gyp verb check python version `/usr/bin/python2 -c "import sys; print "2.6.6
gyp verb check python version .%s.%s" % sys.version_info[:3];"` returned: %j
gyp verb get node dir no --target version specified, falling back to host node version: 11.0.0
gyp verb command install [ '11.0.0' ]
gyp verb install input version string "11.0.0"
gyp verb install installing version: 11.0.0
gyp verb install --ensure was passed, so won't reinstall if already installed
gyp verb install version not already installed, continuing with install 11.0.0
gyp verb ensuring nodedir is created /root/.node-gyp/11.0.0
gyp verb created nodedir /root/.node-gyp/11.0.0
gyp http GET https://nodejs.org/download/release/v11.0.0/node-v11.0.0-headers.tar.gz
gyp WARN install got an error, rolling back install
gyp verb command remove [ '11.0.0' ]
gyp verb remove using node-gyp dir: /root/.node-gyp
gyp verb remove removing target version: 11.0.0
gyp verb remove removing development files for version: 11.0.0
gyp ERR! configure error 
gyp ERR! stack Error: connect ECONNREFUSED 104.20.23.46:443
gyp ERR! stack     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)
gyp ERR! System Linux 2.6.32-754.3.5.el6.x86_64
gyp ERR! command "/root/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node10/bin/node" "/root/.jenkins/workspace/central-npm-registry/myapp/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd /root/.jenkins/workspace/central-npm-registry/myapp/node_modules/node-sass
gyp ERR! node -v v11.0.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok 
Build failed with error code: 1
npm WARN lifecycle poc-polizas@0.0.0~postinstall: cannot run in wd poc-polizas@0.0.0 node node-internals-webpack.js (wd=/root/.jenkins/workspace/central-npm-registry/myapp)
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: node-sass@4.10.0 (node_modules/node-sass):
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: node-sass@4.10.0 postinstall: `node scripts/build.js`
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: Exit status 1

up to date in 17.414s
[Pipeline] sh
[myapp] Running shell script
+ ng build
Your global Angular CLI version (7.1.4) is greater than your local
version (7.1.0). The local Angular CLI version is used.

To disable this warning use "ng config -g cli.warnings.versionMismatch false".

Date: 2018-12-21T12:00:06.525Z
Hash: 7e41e1c4bdf3d4157b68
Time: 25382ms
chunk {app-domain-list-list-module} app-domain-list-list-module.js, app-domain-list-list-module.js.map (app-domain-list-list-module) 46.1 kB  [rendered]
chunk {app-domain-login-login-module} app-domain-login-login-module.js, app-domain-login-login-module.js.map (app-domain-login-login-module) 14.3 kB  [rendered]
chunk {main} main.js, main.js.map (main) 198 kB [initial] [rendered]
chunk {polyfills} polyfills.js, polyfills.js.map (polyfills) 410 kB [initial] [rendered]
chunk {runtime} runtime.js, runtime.js.map (runtime) 8.91 kB [entry] [rendered]
chunk {scripts} scripts.js, scripts.js.map (scripts) 220 kB  [rendered]
chunk {styles} styles.js, styles.js.map (styles) 451 kB [initial] [rendered]
chunk {vendor} vendor.js, vendor.js.map (vendor) 4.15 MB [initial] [rendered]

ERROR in ./node_modules/jwa/index.js
Module not found: Error: Can't resolve 'crypto' in '/root/.jenkins/workspace/central-npm-registry/myapp/node_modules/jwa'
ERROR in ./node_modules/jws/lib/sign-stream.js
Module not found: Error: Can't resolve 'stream' in '/root/.jenkins/workspace/central-npm-registry/myapp/node_modules/jws/lib'
ERROR in ./node_modules/jws/lib/verify-stream.js
Module not found: Error: Can't resolve 'stream' in '/root/.jenkins/workspace/central-npm-registry/myapp/node_modules/jws/lib'
ERROR in ./node_modules/jws/lib/data-stream.js
Module not found: Error: Can't resolve 'stream' in '/root/.jenkins/workspace/central-npm-registry/myapp/node_modules/jws/lib'
[Pipeline] }
[Pipeline] // withNPM
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: Unable to notify JIRA: [403] Forbidden
ERROR: Failed to notify JIRA at http://192.6.4.13:8080 on this builds completion -> java.net.ConnectException: Conexión rehusada (Connection refused)
ERROR: script returned exit code 1
Finished: FAILURE

I have never used Jenkins, so I can't talk from experience. Neverthless, you're getting an ECONNREFUSED message, making me think the issue lays in your network settings, and not in your script. Does curl www.google.com , git <any repo> or ping 140.82.118.3 work for you from Jenkins?

Just to explain what's happening here:

You (or your client) don't want any external internet access from this machine, so you've configured npm to hit a proxy server. That seems to be working, presumably your package.json file has a bunch of dependencies in it, and they are processing without an issue.

However, some larger and/or more complicated packages have custom installation scripts, and just setting up an npm proxy doesn't have any impact on these installation scripts. These scripts might, for example, compile an extension library for node, or download a native binary from GitHub and use that.

(In fact, a very common pattern for node libraries optimized for speed is that they'll require compilation, and on install, they'll first see if they can download a compatible pre-built binary from the project's GitHub page; if not, they'll download the source and compile it. That's what happened in this case, but of course it failed to download the binary, and then it failed again to download the source to compile, so the installation aborted.)

Some possibilities:

  • If you can pre-install troublesome packages on the machine ahead of time, as suggested by Travenin, you can avoid this error.

  • If the client is doing some heavy internal networking, and has custom DNS, they could force DNS resolution for github.com (and any other domains as needed) to resolve to another proxy you set up, which would forward the request to GitHub as necessary.

  • You could also force resolution on the machine itself, using /etc/hosts entries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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