代码之家  ›  专栏  ›  技术社区  ›  Karbos 538

编写Jenkins管道共享库以发布到Nexus NPM存储库

  •  5
  • Karbos 538  · 技术社区  · 7 年前

    我曾经使用DSL管道将我的NPM项目发布到Nexus,其中包含一个发布阶段,步骤如下:

    stage ('Publish') {
      nodejs(nodeJSInstallationName: 'Node LTS', configId: '123456ab-1234-abcd-1234-f123d45e6789') {
        sh 'npm publish'
      }
    }
    

    我在Jenkins上安装了一个名为“Node LTS”的NodeJS,还有一个带有此配置ID的npmrc配置文件。

    现在,我想将这个阶段导出到groovy SharedLib中。 根据 Declarative Pipeline documentation this nodejs-plugin issue ,我可以这样写:

        stage('Publish') {
            tools {
                nodejs 'Node LTS'
            }
            steps {
                sh 'npm publish'
            }
        }
    

    但这不会设置当前在我的npmrc配置文件中的身份验证配置:

    registry=http://my-nexus/repository/npm-private/
    _auth="some=base=64=credential=="
    always-auth=true
    

    有没有想过用声明性语法检索此配置并防止此错误消息?

    npm ERR! code ENEEDAUTH
    npm ERR! need auth auth required for publishing
    npm ERR! need auth You need to authorize this machine using `npm adduser`
    
    2 回复  |  直到 7 年前
        1
  •  5
  •   Karbos 538    6 年前

    通过查看npm日志文件和阅读文档,我最终发现最好的解决方案是在我的包中指定以下发布配置。json文件:

    {
      "name": "@my-company/my-project",
      ...
      "publishConfig": {
        "registry": "http://my-nexus/repository/npm-private/"
      },
      ...
    }
    

    我离开了 .npmrc 配置:

    registry=http://my-nexus/repository/npm-private/
    _auth="some=base=64=credential=="
    always-auth=true
    

    笔记 :the always-auth 在我的情况下,自动化脚本需要: https://docs.npmjs.com/misc/config

        2
  •  0
  •   viniciusalvess    2 年前

    我很难让jenkins pipeline将一个节点包发布到nexus 3,以下是对我有效的方法。这可能会对某人有所帮助。

    pipeline {
    
        agent any
    
        environment {
            registryCredentials = "nexus"        
            registryPrivate = "http://nexus:8081/repository/your-nexus-repo/" // nexus repository
        }
    
        stages {
    
            stage('Publish') {
    
                steps {
                    script {
                        nodejs('your-jenkins-nodejs-name') {
                            sh("rm ~/.npmrc || echo 'trying to remove .npmrc'") // remove .npmrc
    
                            // this token is copied from ~/.npmrc file after a interactive npm login
                            // do a npm login to your nexus npm hosted private repo and get the token
                            sh 'echo "//nexus:8081/repository/vinsystems-npm/:_authToken=NpmToken.302af6fb-9ad4-38cf-bb71-57133295c7ca" >> ~/.npmrc'
    
                            sh("cd ./WebClientWorkspace && yarn install")
                            sh("cd ..")
    
                            sh("yarn publish ./path/to/your/js-library --registry=${registryPrivate} --registry=${registryPrivate} --non-interactive --verbose")
                        }
                    }
                }
            }
        }
    }