Gradle exec block not using passed environment - jenkins

I am trying to use npm to build some projects. In jenkins there are various node versions installed but I need to use a specific version of npm to build my project. I am passing the environement with 'PATH' variable value in exec block but it is not picking that path.
def pathEnvironment = [:]
pathEnvironment["PATH"] = "/scratch/jenkins/.nvm/versions/node/v16.18.1/bin:" + System.getenv("PATH")
exec {
environment = pathEnvironment
workingDir workspaceLocation
commandLine "npm", "install"
}
exec {
environment "PATH", "/scratch/jenkins/.nvm/versions/node/v16.18.1/bin:" + System.getenv("PATH")
workingDir workspaceLocation
commandLine "npm", "install"
}
I am getting error shown in above image.
I am using jenkins gradle plugin with gradle version 5.4.1.
What needs to be done to be able to set the path for the exec block?

Related

Passing arguments to build.gradle

I am building a springboot application using gradle.
The build eventually outputs a docker container and I want my build.gradle to support multiple environments.
I am using gradlew build docker to build and the docker section of my build.gradle is :
docker {
dependsOn build
name "app-production"
files bootJar.archivePath, 'application.yml'
buildArgs(['JAR_FILE': "${bootJar.archiveName}"])
}
I successfully passed arguments to the dockerfile but now I also want to pass arguments to the build.gradle file. Is it possible using gradlew build docker?
I need something like this : gradlew build docker --env=qa and then :
docker {
dependsOn build
name "app-${env}"
files bootJar.archivePath, 'application.yml'
buildArgs(['JAR_FILE': "${bootJar.archiveName}"])
}
NOTE: ${bootJar.archiveName} is a local variable in my build.gradle, not an environment variable
Any ideas?
You can use Project Properties to pass values to your build script from command line, as follows:
gradlew build docker -Penv=qa
Then in your build script you can access this property :
docker {
name "app-${env}"
// ....
}

How to resolve grunt not recognizable error on jenkins whereas Grunt is running in command prompt?

How to resolve grunt not recognizable error on jenkins whereas Grunt is running in command prompt?
Jenkins output:
test cases will be executed
'grunt' is not recognized as an internal or external command, operable program or batch file.
Build step 'Execute Windows batch command' marked build as failure
Finished: FAILURE
Most probably if the grunt-cli package is installed globally, you do not have the executable path added to system's PATH variable.
Or in the case when you do not have the package installed, npm install -g grunt-cli in windows shell.
Issue was related to path,basically my user's path variable had npm and grunt paths but was missing from system path variable.So i added npm path to system path variable and it worked for jenkins!
In my case the path was defined correctly. It worked when I execute it on CMD, but not from Jenkins. Turns out it was due to Jenkins using its own managed path, as defined in Manage Jenkins -> Configure System -> Global Properties.
Define Environment variable named "Path" there, and add the location of your npm directory to it, such as: "C:\Users\your-user-name\AppData\Roaming\npm;"

Grunt integration with Jenkins on windows issue

I am running Jenkins on Tomcat7 - Windows 7. I have provided the node bin path in my jenkins configuration. Then running a shell script as follows:
echo $PATH
node --version
npm install -g grunt-cli
npm install
grunt cssmin
As suggested in other post Jenkins integration with Grunt, I have restarted my jenkins several times, and tried to work on all the answers written in that post, but still it shows error, grunt: command not found.
Error stack trace from jenkins console output:
/c/apache-maven-3.2.5/bin:/c/Program Files/Java/jdk1.7.0_79/bin:/c/Program Files/nodejs/bin:/c/Program Files/Java/jdk1.7.0_79/bin:/c/Program Files/nodejs/:
+ node --version
v0.10.30
+ npm install -g grunt-cli
C:\Windows\system32\config\systemprofile\AppData\Roaming\npm\grunt -> C:\Windows\system32\config\systemprofile\AppData\Roaming\npm\node_modules\grunt-cli\bin\grunt
grunt-cli#0.1.13 C:\Windows\system32\config\systemprofile\AppData\Roaming\npm\node_modules\grunt-cli
├── resolve#0.3.1
├── nopt#1.0.10 (abbrev#1.0.7)
└── findup-sync#0.1.3 (lodash#2.4.2, glob#3.2.11)
+ npm install
npm WARN package.json Trademust#1.0.0 No repository field.
+ grunt cssmin
C:\Program Files\Apache Software Foundation\Tomcat 7.0\temp\hudson2968878175697925824.sh: line 6: grunt: command not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE
I have also followed the steps mentioned on this site grunt-on-jenkins
package.json and Gruntfile.js are in root directory the very first time when I executed the jenkins build, grunt installed all modules from my gruntfile.js, and after that in all other build's its showing the above output.
Can anyone please check what's going on wrong here.
After searching a lot, I found where my grunt is installed. As far as jenkins build is concerned it installs in drive:/.jenkins....../workspace/node_modules/.bin.
After providing this path in jenkins using shell script export path=$PATH:drive:/.jenkins....../workspace/node_modules/.bin, grunt started executing.
Also what I learnt in this process was checking where the executable's are available on system path or which path jenkins refers to is using which "executable_name" without qoutes. you can use this command both on windows as well as linux. Ex: which grunt will show the path where grunt executable file is present.
From the error message it seems, sheel is not able to find grunt. Could you please check if it is present in the $PATH variable. On which node this shell script is running? You cab check the $PATH of the particular node. You can also add grunt installation path to $PATH variable during the shell script.
grunt_path="grunt_installtion_path"
export PATH=${PATH}:${grunt_path}

Executing custom shell script to build in jenkins

I have setup a jenkins job to build my project. I have a jake.sh file in my project and the code is pulled from github. I want "npm install" command to be executed and then jake.sh to be executed once the the code is checked out.
How can I configure this in jenkins? I have tried givin ./jake.sh and jake.sh in Build->Execute Shell section
According what you tell I think the problem can be
The script is not marked as a executable. In this case add in Build -> Execute Shell (in case you have linux) sudo chmod 777 path_to_script/jake.sh.
The script is not in the base directory. Remembeber that when you execute a bash script, the current directory is /path_to_job/workspace. So you have first to move to the script folder (cd path_to_script) or specify the path when running it: ./path_to_script/jake.sh.
I hope this solves your problem.
A workaround for shell scripts can be to run the script as
bash ./jake.sh
instead of
./jake.sh
Then you don't have to do chmod. Useful when you wipe the workspace before every build.
In the same manner, if you have a nodejs shell script or python script, you can run node myscript.js / python myscript.py.

new grunt task added to Jenkins is not found

I am running Jenkins on Windows and grunt v0.4.1. I added a new task grunt-replace to the grunt.js however jenkins is having an issue seeing it:
C:\Jenkins\workspace\xxx>grunt
Local Npm module "grunt-replace" not found. Is it installed?
I've tried several things...
npm install -g grunt-replace
Restarting the jenkins service
npm install grunt-replace
I have installed these as the service jenkins is running under..
What am i missing here? What is the correct way to get jenkins to see the new task?
whats the way you start your task? i would add grunt-replace to the devDependencies in your package.json , add your test-command to the scripts.test-property in your package.json, and then let jenkins execute that.
package.json
{
...some other configs
"scripts": {
"test": "grunt test"
},
"devDependencies": {
"grunt-replace": "*",
... all the grunt-modules you need to be installed
}
}
jenkins excute
$ npm install && npm test

Resources