Karbos 538
Karbos 538

Reputation: 3055

Writing a Jenkins Pipeline Shared Library to publish to Nexus NPM repository

I used to publish my NPM projects to Nexus using a DSL pipeline containing a publish stage with this kind of step :

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

I have a NodeJS installation named "Node LTS" on my Jenkins and a npmrc config file with this configId.

Now I want to export this stage into a groovy SharedLib. According to Declarative Pipeline documentation and this nodejs-plugin issue, I could write this :

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

But this does not set authentification configuration that is currently in my npmrc configuration file :

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

Any idea to retreive this configuration with declarative syntax and prevent this error message ?

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`

Upvotes: 5

Views: 10752

Answers (2)

viniciusalvess
viniciusalvess

Reputation: 814

I struggled with having an node package published to nexus 3 from jenkins pipeline and here is what worked for me. It might help someone.

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")
                    }
                }
            }
        }
    }
}

Upvotes: 0

Karbos 538
Karbos 538

Reputation: 3055

Taking a look to npm log files and reading documentation, I finally find the best solution was to specify the following publish configuration in my package.json file :

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

I leave the .npmrc configuration :

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

Note : the always-auth is needed, in my case, for automation script : https://docs.npmjs.com/misc/config

Upvotes: 6

Related Questions