Jenkins integration with Grunt - node.js

I've setup Jenkins v1.550 on Windows Server 2008 R2. It runs as a service at http://localhost:8080 for now. I'm logged into the machine as an Administrator. I've installed Node.js and can run "npm" from the command line.
I've also installed the NodeJS plugin v0.2.1 for Jenkins. I then went into the Configure System section of Jenkins, scrolled down to NodeJS installations, clicked on Add NodeJS button, gave "NodeJS" as the name, and "C:\Program Files\nodejs" as the path to the installation directory. I didn't check the "Install automatically" option as I read on the plugin page that it is only available to Linux.
I then created a new job, clicked the checkbox that said "Provide Node & npm bin/ folder to PATH", created a new build step for "Execute Windows batch command" and typed in "node --version" and "grunt --version" and saved it.
I ran the job and this is the output -
Building in workspace C:\Program Files (x86)\Jenkins\workspace\Test_1.0
[Test_1.0] $ cmd /c call C:\Windows\TEMP\hudson1381541243088903083.bat
C:\Program Files (x86)\Jenkins\workspace\Test_1.0>node --version
v0.10.24
C:\Program Files (x86)\Jenkins\workspace\Test_1.0>grunt --version
'grunt' is not recognized as an internal or external command,
operable program or batch file.
C:\Program Files (x86)\Jenkins\workspace\Test_1.0>exit 9009
Build step 'Execute Windows batch command' marked build as failure
Finished: FAILURE
It looks like it's unable to find the grunt-cli for the user account Jenkins is running under (System). I tried to installing grunt cli globally (npm install -g grunt-cli) and also grunt locally (npm install grunt). No luck.
Can someone please help?

for nice easy to configure self-installed nodejs on the machine, i have to recommend the excellent -> http://wiki.jenkins-ci.org/display/JENKINS/NodeJS+Plugin
it will install nodejs and grunt on the machine, through easy to use web front end no shell required
jenkins jobs can then simply run nodejs build steps, hey presto
steps involved :
a) install this on your jenkins instance -> http://wiki.jenkins-ci.org/display/JENKINS/NodeJS+Plugin
b) create a nodejs installation on jenkins
go to
http://URL_OF_JENKINS/jenkins/configure
NodeJS- > NodeJS installations -> Add NodeJS -> Name = "NodeJS 0.11.10", tick "Install automatically", select "Install from nodejs.org", add "grunt-cli" to globally installed packages
c) create a job with "execute NodeJS script" build task
var sys = require('sys');
sys.puts('NodeJS Test');
sys.puts('***************');
sys.puts('helloworld');
volia :)
run the job and see the nodejs script run,
from their the world is your oyster you can use grunt by ticking "Provide Node/npm bin folder to PATH" and running a "execute shell" build task
npm update
grunt
grunt --force reporting

You will need to restart the Jenkins service after installing node, presumably to cause it to refresh its cached copy of your PATH environment variable

I have grunt doing some tasks for me in Jenkins, but I went the npm script route. Grunt and grunt-cli are dev dependencies, and I have the following defined in my package.json file:
"scripts": {
"test": "node node_modules/grunt-cli/bin/grunt test"
},
In Jenkins (running on Windows), I added two post-build tasks:
npm install
npm test
We just installed NodeJs normally on the Jenkins server.

Another solution that worked for me on Windows is to use the full path to the grunt exec file, which can be found by writing "where grunt" in the command shell. I used the full path in the regular bat-file.

Had the same issue on Windows. When I manually installed node and ran npm install -g grunt-cli from command line, jenkins could not recognize the grunt command. So uninstall node, reinstall it but dont run npm install. Then restart the jenkins slave. Then from the jenkins job that runs on your specific jenkins slave, make it run a Windows batch command that runs npm install -g grunt-cli After that again restart the jenkins service. Then from the job run npm install. Then everything worked for me. If issues still persist, then uninstall the slave, and reinstall it, then everything works fine immediately.

Related

ng is not recognised as an internal or external command. Jenkins + Angular CLI

I am trying to setup Jenkins for an Angular CLI project. I have installed node and Angular Cli on the Jenkins server under a specific user account.
if I open a command prompt on the server an execute the following commands to verify they are installed properly, this is the result:
I have configured the project with Jenkins, and i created two build steps two execute two bat files.
One runs: npm install
and the second one runs: ng build --prod
Then I build Jenkins, it runs the npm install but it fails running ng build --prod because it says " 'ng' is not recognised as an internal or external command".
Am I doing something wrong? Is there another way to probably use the angular cli on the node_modules folder, So it does not need to use the angular cli installed on the server. It seems like Angular CLI is installed only for my user on the server but not for the user Jenkins use to build.
PS: I installed Angular CLI globally using:
npm i -g #angular/cli
No need to install angular cli on server, just run
npm run ng -- build
That will run the local version from your project devDependencies
This way you can pass any flag to your local cli npm run ng -- test, npm run ng -- lint, etc
You can pass additional flags to ng just like that
run ng -- build --prod
More details at https://docs.npmjs.com/cli/run-script
Just for further clarification when someone searches for the same problem and finds this question (as I did):
If you want to use the --prod flag while running the build command, as asked in this question, you can use:
npm run ng -- build --prod
Important are the "--" between "ng" and "build" with spacing. This is due to the syntax of "npm run", more information can be found here: https://docs.npmjs.com/cli/run-script
This also solves the problem described in a comment below the accepted answer: "This is working but its excluding the additional parameters like --test when running the build"
npm run ng -- build
we can use as this without installing angular cli
If it is working on the Local command prompt, Restart the Jenkin server.
Restart Jenkins --> http://host-name:port/base-url/restart
ex:- http://localhost:8080/jenkins/restart
If it is not working on local command as well install angular CLI globally and set the environment settings and do the previous step.
try as below.
BUILD_ID=dontKillMe nohup ng serve
The only issue with npm run ng build is it omits any other parameter like --prod or --test after build.
Following are the commands what i am using to run my angular build successfully from Jenkins.The last command is executed the dirty way by setting up the path variables. Don't know if there is a cleaner way to do this. This does execute the commands properly without omitting anything.
#echo on
cmd /c npm install -g #angular/cli#latest
echo yarn Install
cmd /c yarn
echo Build
set PATH=%PATH%;C:\Users\Administrator\AppData\Roaming\npm;C:\Users\Administrator\AppData\Roaming\npm\node_modules\#angular\cli\bin;
ng build --prod --aot=true

how can I open web application requiring node.js

I must run micro-crawler https://github.com/WebMole/Micro-Crawler which is a crawler web application, that run with node.js.
I could not figure out how to open this app, I download node.js, and when I write install npm and install bower to the node.js command line nothing happened. Also I did not understand how to start web applicaion after installations
Please help me
Finally I solved the issue
First, I install node.js, then move node.js folder to wampserver directory,
and move the web sites files in the node.js directory
Second, I go to the path where node.js is from command line, then enter "install npm" command
line
The error appeared in this phase for solving I create a npm
folder in c:users/user_name_of_your_computer/app_data/roaming then I installed git and enter the directories of bin and cmd files of git to the system enviroments/path
Third I enter the commands "bower install" and "npm install -g grunt-cli" from the comand. from the path of where node.js is
and the micro-crawler works
Did you execute your .js file? You can do that with node.js cli thus:
path/to/your/file: node file.js
This is a good reference:
How to run a hello.js file in Node.js on windows?

Protractor is not recognized as an internal or external command, operable program or batch file

I have protractor installed on windows slave machine. I am trying to run my script using node index.js.
If I trigger job from jenkins to run above command it gives error like Protractor is not recognized as an internal or external command, operable program or batch file.. But when I run command directly on slave machine it execute it successfully.
You can use the Jenkins NodeJS plugin(https://wiki.jenkins-ci.org/display/JENKINS/NodeJS+Plugin) to install node and additional npm packages like protractor, grunt, etc.
After installing npm packages at global level, you can select the "Provide Node & npm bin/folder to PATH" checkbox to ensure you can run "protractor", "grunt" into shell or batch command.
Hope this helps.
I had same problem
In jenkins, just go to same directory where protractor and webdriver file is present after installing npm install -g protractor
like in build environment > cd go to same directory then
run commands with node appended to them such as
node webdriver-manager start
node protractor "path where config.js" is kept
This solution works for me!! Happy Automating
This Issue occurs because if you have installed the NodeJS after starting the Jenkins server.
The solution is simple:
Restart your Jenkins Server

Jenkins script quitting prematurely when using npm install on Windows

In my Jenkins job I want to build a JavaScript app using Grunt. The Jenkins build scripts creates a build directory (if it doesn't already exist), changes to that directory and runs:
npm install grunt
npm install grunt-zip
grunt --gruntfile=[something]
(Of course grunt-cli is installed globally.) When I build the job, the first statement causes Grunt and dependencies to be pulled down as expected. However, the job then terminates successfully:
Archiving artifacts
No emails were triggered.
Finished: SUCCESS
The second npm install is not run. Any idea why the script is terminating after running npm install instead of continuing to the subsequent statements?
So it turns out that npm is a batch file, not an executable, so it needs to be invoked using call from the Jenkins script:
call npm install grunt
i would recommend not using the local grunt / nodejs install, but instead getting jenkins to do this for you!
it's much easier and means there's less coupling to system specific installs and variables.
steps:
a) use nodejs jenkins plugin + get it to install nodejs on machine/grunt-cli -> Jenkins integration with Grunt
b) populate your package.json with any nodejs dependances required, eg grunt/grunt-zip etc
c) when running grunt just do a "npm update" before "grunt" command
that way your not doing explicit npm install, it's all configured from your package.json, and your build scripts will be less brittle, and your developers can use the same steps as the build server, eg "npm update;grunt" locally same as build server
For future googlers:
use command chaining for this.
This works:
npm install && npm install install grunt-zip
This wont work:
npm install
npm install grunt-zip

NodeJS/Testacular on Jenkins CI

I'm using Testacular which is a Node.js test runner for Angular/Jasmine. I can run it fine from the command line, but every time I try to run it from Jenkins build steps, it bombs out with all sorts of errors regarding environment variables. I tried the Nodejs plugin for Jenkins, but that's just to run node code snippets. Anyone know of a way to have node apps (eg. Testacular) running test under Jenkins?
You will need to:
have "testacular" as a dependency in your package.json file.
install your dependencies with npm install (do this as a build step)
call it as ./node_modules/.bin/testacular start --single-run
Assuming you have configured testacular to use PhantomJs browsers = ['PhantomJS'];, you just need to have the phantomjs binary in your path or tell testacular where it is located with an environment variable set in your shell:
export PHANTOMJS_BIN=$HOME/local/bin/phantomjs
good news!
" I tried the Nodejs plugin for Jenkins, but that's just to run node code snippets. "
nope!
install the nodejs plugin see instructions here -> NodeJS jenkins plugin broken?
then tick "Provide Node/npm bin folder to PATH" and when running a "execute shell" build task, you can use nodejs, here's a example using grui
npm update
grunt
grunt --force reporting

Resources