Issue
I am facing below errors in my jenkins builds, this is a frustrating issue happening since past 3 weeks, no matter how much i try, i am unable to get these commands worked, in the local they work fine, but thru the jenkins pipeline code using our own shared libraries, this does not work at all
ERROR: for the command yarn typecheck, same happens with prettier:check, test, ci etc
+yarn typecheck
yarn run v1.22.17
$ tsc --noEmit
/bin/sh: tsc: command not found
error Command failed with exit code 127.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
def jenkins
DuploPipeline(jenkins) {
this.jenkins = jenkins
}
jenkins.steps.node('docker-nodejs-slave') {
jenkins.steps.withEnv(['NODEJS_VERSION=16','NPM_AUTH_KEY = ""','NPM_EMAIL = ""',]) {
jenkins.steps.stage('Install dependencies') {
jenkins.steps.sh 'yarn'
}
jenkins.steps.stage('Check types') {
jenkins.steps.checkout jenkins.scm
jenkins.steps.sh '''
export PATH=$(npm bin -g):$PATH >> ~/.profile
echo $PATH
which yarn
ls -l /tools/nodejs/node-v16-linux-x64/bin
yarn typecheck;
'''
}
The output of PATH has the location of nodejs installation - node-v16-linux-x64 is my nodejs installation and next you can see the files under the bin
/tools/nodejs/node-v16-linux-x64/bin:/tools/fortify/Fortify_SCA_and_Apps_19.2.0/bin:/tools/awscli/awscli-1/lib/aws/bin:/tools/awscli/sessionmanagerplugin/bin:/tools/nodejs/node-v16-linux-x64/bin:/tools/fortify/Fortify_SCA_and_Apps_20.1.4/bin:/tools/awscli/awscli-1/lib/aws/bin:/tools/awscli/sessionmanagerplugin/bin:/usr/local/bin:/usr/bin:/tools/awscli/aws-cli-saml:/tools/awscli/aws-cli-saml
The files under bin location of node-v16-linux-x64/bin
total 78432
lrwxrwxrwx. 1 1001 1001 35 Feb 25 05:32 bower -> ../lib/node_modules/bower/bin/bower
lrwxrwxrwx. 1 1001 1001 45 Feb 8 12:49 corepack -> ../lib/node_modules/corepack/dist/corepack.js
lrwxrwxrwx. 1 1001 1001 35 Feb 25 05:32 dredd -> ../lib/node_modules/dredd/bin/dredd
lrwxrwxrwx. 1 1001 1001 39 Feb 25 05:32 ember -> ../lib/node_modules/ember-cli/bin/ember
lrwxrwxrwx. 1 1001 1001 39 Feb 25 05:32 grunt -> ../lib/node_modules/grunt-cli/bin/grunt
lrwxrwxrwx. 1 1001 1001 36 Feb 25 05:32 gulp -> ../lib/node_modules/gulp/bin/gulp.js
lrwxrwxrwx. 1 1001 1001 40 Apr 23 05:01 newman -> ../lib/node_modules/newman/bin/newman.js
lrwxrwxrwx. 1 root root 62 Apr 23 05:01 newman-reporter-htmlextra -> ../lib/node_modules/newman-reporter-htmlextra/bin/htmlextra.js
lrwxrwxrwx. 1 1001 1001 38 Feb 8 12:49 npm -> ../lib/node_modules/npm/bin/npm-cli.js
lrwxrwxrwx. 1 1001 1001 38 Feb 25 05:32 npm-cache -> ../lib/node_modules/npm-cache/index.js
lrwxrwxrwx. 1 1001 1001 38 Feb 8 12:49 npx -> ../lib/node_modules/npm/bin/npx-cli.js
lrwxrwxrwx. 1 1001 1001 52 Feb 25 05:32 phantomjs -> ../lib/node_modules/phantomjs-prebuilt/bin/phantomjs
lrwxrwxrwx. 1 1001 1001 37 Feb 25 05:32 retire -> ../lib/node_modules/retire/bin/retire
lrwxrwxrwx. 1 1001 1001 36 Feb 25 05:32 yarn -> ../lib/node_modules/yarn/bin/yarn.js
lrwxrwxrwx. 1 1001 1001 36 Feb 25 05:32 yarnpkg -> ../lib/node_modules/yarn/bin/yarn.js
lrwxrwxrwx. 1 1001 1001 33 Feb 25 05:32 yo -> ../lib/node_modules/yo/lib/cli.js
lrwxrwxrwx. 1 1001 1001 46 Feb 25 05:32 yo-complete -> ../lib/node_modules/yo/lib/completion/index.js
-rwxr-xr-x. 1 1001 1001 80310904 Feb 8 12:49 node
The output to the command which yarn
/tools/nodejs/node-v16.14.0-linux-x64/bin/yarn
Everything seems fine to me and yet the error is still happening
Strangely, when i wrote a sample job, it works well
pipeline {
agent none
stages {
stage('NodeJS Build') {
agent {
node { label 'docker-nodejs-slave' }
}
steps {
script {
env.NPM_AUTH_KEY = ' '
env.NPM_EMAIL = ' '
sh '''
export NODEJS_VERSION=16
source /etc/profile.d/jenkins.sh
npm -v
node -v
yarn typecheck;
'''
}
}
}
}
}
Output:
+npm -v
8.3.1
+node -v
v16.14.0
+yarn typecheck
yarn run v1.22.17
$ tsc --noEmit
Done in 17.01s.
+yarn prettier:check
yarn run v1.22.17
$ prettier -l 'src/**/*.{js,ts,tsx}'
Done in 7.18s.
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
Finished: SUCCESS
but why it is not working in my API Jenkins pipeline using shared library is a big question, i am using the same slave for both, i am missing something in the declarative pipeline, kindly help me with this issue.
Solution
Try defining PATH variable before the sh block. Refer to the following pipeline syntax. PATH+NODEHOME will append the defined path to the PATH variable. Here NODEHOME is just a string to improve readability.
withEnv(['PATH+NODEHOME=/tools/nodejs/node-v16-linux-x64/bin']) {
echo "PATH is: $PATH"
sh '''
which yarn
<YOUR Commands>
'''
}
Answered By - ycr
Answer Checked By - David Goodson (JavaFixing Volunteer)