Jenkins 2 NPM_代币凭证

Jenkins 2 NPM_代币凭证,jenkins,npm,jenkins-pipeline,jenkins-2,Jenkins,Npm,Jenkins Pipeline,Jenkins 2,我正在尝试运行Jenkins 2管道(Jenkins文件),该管道将使用npm publish将包发布到本地npm存储库。 为了做到这一点,我尝试在Jenkinsfile中使用以下阶段: stage('TEST npm whoami') { withEnv(["PATH+NPM=${tool name: 'node-6', type: 'jenkins.plugins.nodejs.tools.NodeJSInstallation'}/bin"]) { withCredentia

我正在尝试运行Jenkins 2管道(Jenkins文件),该管道将使用
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`
从外观上看,
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'
    }
}

希望这有帮助

杰拉德·瑞安和加斯顿的回答是正确的,我只想补充一个我起初没有得到的细节:

如果要使用专用存储库,
.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'
            }
}
withCredentials([string(credentialsId:'registry',variable:'token')){
试一试{
sh“echo注册表=>>.npmrc”
sh“echo//:_authToken=${env.token}>>.npmrc”
sh'npm whoami'
}最后{
sh'rm~/.npmrc'
}
}

为了验证,CredentialId通常是一个guid。你在这里放了“npm代币”吗?如果您只是
echo$NPM_TOKEN
稍微调整了变量,将
.npmrc
写入构建服务器的
$HOME
目录,并包装在
try catch
块上,确保每次运行时删除文件:~~~~带有凭据([string(credentialsId:'NPM_TOKEN',variable:'NPM_TOKEN')){String text=“//registry.npmjs.org/:\u authToken=${env.NPM\u TOKEN}”String npmrc='\$HOME/.npmrc'writeFile:npmrc,text:text try{sh'NPM publish'}最后{sh“rm${npmrc}}}}~~显然,并不是所有版本的Jenkins都会接受
try{…}{…}
在此表单中。
在第194行第17列需要一个步骤。
尝试{