piotrek
piotrek

Reputation: 14550

How to use jenkins pipeline with nvm wrapper plugin?

I'm using pipeline (Jenkinsfile) and I need to change node version. i added the Nvm Wrapper Plugin but i don't know how to use it properly from Jenkinsfile

should i add the nvm('...') {} inside steps? or should it be somewhere top level in the node step? currently i don't even have the node step - everything is done using sh

Upvotes: 7

Views: 11528

Answers (5)

mumumi
mumumi

Reputation: 1

If your CI pipelines consistently stop after 5 minutes, consider watching this tutorial.

The nvm.sh script would check for both 'nodejs/index.tab' and 'iojs/index.tab' before verifying if a version is installed with nvm_is_version_installed.

The nvm wrapper should set the environment variables for either Node.js or io.js (not both) and prevent external variables (such as 'HTTP_PROXY') from interfering.

Your pipeline script should follow this pattern:

pipeline {
  agent {
    label '!windows'
  }

  stages {
    stage("Build-MMM") {
      steps {
         nvm(nvmInstallURL: 'https://gitee.com/mirrors/nvm/raw/master/install.sh', 
             nvmNodeJsOrgMirror: 'https://npmmirror.org/mirrors/node && NVM_IOJS_ORG_MIRROR=https://npmmirror.org/mirrors/iojs',
             nvmIoJsOrgMirror: 'https://npmmirror.org/mirrors/iojs && NVM_NODEJS_ORG_MIRROR=https://npmmirror.org/mirrors/node', 
             version: '16') {
                 ...
              }
           }
        }
    ...

Upvotes: 0

mkelandis
mkelandis

Reputation: 31

The latest jenkins upgrade (2.319.2 --> 2.387.1) seems to introduce an issue with this nvm-wrapper plugin where it cannot be used more than one time within the pipeline steps. We did not have that problem before the upgrade... but that is how I landed here.

The solution above did not work for me as I needed node+npm binaries to be set on the path after nvm commands were invoked. Here is an adaptation from other solutions that replaces the nvm-wrapper in a scripted pipeline:

  env.ECHO_CMD = 'echo $NVM_BIN'
  env.NVM_BIN = sh (
     script: 'bash -l -c "source $HOME/.nvm/nvm.sh 1>&2; nvm use $NODE_VERSION 1>&2 || nvm install $NODE_VERSION 1>&2 && nvm use $NODE_VERSION 1>&2 && $ECHO_CMD "',
     returnStdout: true
  ).trim()
  echo "NVM_BIN: ${env.NVM_BIN}"
  env.PATH = "${env.NVM_BIN}:${env.PATH}"

  // do some node stuff in sh commands...
  sh 'node --version'
  sh 'npm --version'

Upvotes: 2

Olva Tito
Olva Tito

Reputation: 83

this works for me

sh 'bash -l -c ". $HOME/.nvm/nvm.sh ; nvm use <version> || nvm install <version> && nvm use <version> "' 

example:

sh 'bash -l -c ". $HOME/.nvm/nvm.sh ; nvm use 8.0 || nvm install 8.0 && nvm use 8.0 "' 

Upvotes: 5

miiimooo
miiimooo

Reputation: 131

I ended up using this and it works also with a .nvmrc file

sh 'bash -l -c ". $HOME/.nvm/nvm.sh ; nvm use || nvm install && nvm use"' 

This expects nvm installed in the jenkins home folder. But it would be easy to add a step that downloads nvm in the right place first.

Upvotes: 1

piotrek
piotrek

Reputation: 14550

what worked for me:

pipeline {
  agent any

  stages {
    stage("Build") {
      steps {
         nvm(nvmInstallURL: 'https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh', 
             nvmIoJsOrgMirror: 'https://iojs.org/dist',
             nvmNodeJsOrgMirror: 'https://nodejs.org/dist', 
             version: '8.1.2') {
                    sh "npm install"
                    echo "Build main site distribution"
                    sh "npm run build:dist"
              }
           }
        }
    ...

Upvotes: 9

Related Questions