我正在尝试运行Jenkins2管道(Jenkinsfile),它将使用npm publish将包发布到本地NPM库中。
为了做到这一点,我尝试在Jenkinsfile中使用以下阶段:
stage('TEST npm whoami') {
withEnv(["PATH+NPM=${tool name: 'node-6', type: 'jenkins.plugins.nodejs.tools.NodeJSInstallation'}/bin"]) {
withCredentials([[$class: 'StringBinding', credentialsId: 'npm-token', variable: 'NPM_TOKEN']]) {
sh """
npm whoami
"""
}
}
}目前我只运行npm whoami,一旦可以工作,我将用npm publish取代它。
这是我得到的输出:
+ npm whoami
npm ERR! Linux 4.7.5-1.el7.elrepo.x86_64
npm ERR! argv "/var/lib/jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node-6/bin/node" "/var/lib/jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node-6/bin/npm" "whoami"
npm ERR! node v6.5.0
npm ERR! npm v3.10.3
npm ERR! code ENEEDAUTH
npm ERR! need auth this command requires you to be logged in.
npm ERR! need auth You need to authorize this machine using `npm adduser`发布于 2017-05-26 03:20:03
从this GitHub issue的角度来看,NPM_TOKEN似乎不是npm本身可以识别的东西,而是heroku (可能还有其他平台)解释的自定义环境变量。
根据这个问题中的一些讨论,我所做的是在作业执行时根据我的凭据中的令牌env var创建一个项目级.npmrc,然后在继续之前再次删除该文件。例如:
stage('TEST npm whoami') {
withCredentials([string(
credentialsId: 'npm-token',
variable: 'NPM_TOKEN')]) {
sh "echo //npm.skunkhenry.com/:_authToken=${env.NPM_TOKEN} > .npmrc"
sh 'npm whoami'
sh 'rm .npmrc'
}
}希望这能有所帮助!
发布于 2019-09-23 19:15:48
Gerard Ryan和Gaston的答案是正确的,我只想补充一个我一开始没有得到的细节:
如果要使用私有存储库,.npmrc还应指定注册表:
withCredentials([string(credentialsId: 'registry', variable: 'token')]) {
try {
sh "echo registry=<your-registry-URL> >> .npmrc"
sh "echo //<your-registry-URL>/:_authToken=${env.token} >> .npmrc"
sh 'npm whoami'
} finally {
sh 'rm ~/.npmrc'
}
}https://stackoverflow.com/questions/40016486
复制相似问题