jenkins pipeline sh step node not found

1.3k Views Asked by At

I have the following sh step to execute some commands on Jenkins Pipeline, note the npm install installs bower from package.json for later commands to work

    sh """
    . .env/bin/activate
    . ${JENKINS_HOME}/nvm_init.sh
    npm install
    node --version
    which node
    echo $PATH
    export PATH=\$(npm bin):$PATH
    echo $PATH
    which bower
    which node
    node --version
    bower install
    """

The output starting from npm install as below:

+ npm install
npm WARN [email protected] No repository field.
npm WARN [email protected] No license field.
+ node --version
v6.9.2
+ which node
/data/jenkins/.nvm/versions/node/v6.9.2/bin/node
+ echo /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin
+ npm bin
+ export PATH=/data/jenkins/workspace/GoConnect_feature_jenkins2-KGJYBCCDJPBPOLJ2K2Q4JVQP6TKHIVKXYDJZ4WUMFHTSSDIZWCAQ/node_modules/.bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin
+ echo /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin
+ which bower
/data/jenkins/workspace/GoConnect_feature_jenkins2-KGJYBCCDJPBPOLJ2K2Q4JVQP6TKHIVKXYDJZ4WUMFHTSSDIZWCAQ/node_modules/.bin/bower
+ which node
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1

Here is a couple of things I don't understand:

  1. Notice the second echo $PATH, it has the same path as previously and didn't contain the previous prepend from export PATH=\$(npm bin):$PATH
  2. The last which node didn't work, why?
1

There are 1 best solutions below

0
On

Thanks to colleague @john-carter pointing out that without escaping the the dollar sign, groovy will evaluate before executing the script.

sh """
. .env/bin/activate
. ${JENKINS_HOME}/nvm_init.sh
npm install
node --version
which node
echo $PATH
export PATH=\$(npm bin):\$PATH
echo $PATH
which bower
which node
node --version
bower install
"""