NodeJS/Testacular on Jenkins CI - node.js

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

Related

Building different versions of angular in same machine at same time

I'm aware that I can select the node version to use with NVM but, can I build two angular projects (with different ng version and node version) at the same time without issues? My scenario is a self-hosted build server (Windows) with two agents. Each of these might be, at the same time, on charge of building an Angular app with different version.
Regards
Sure you can, instead of running the globally installed ng run the local one with npx like this npx ng build, npx will use the local installed #angular/cli ng command found under ./node_modules/.bin/ of your project, npx comes installed with npm.
Another option is to add a script in package.json:
"scripts": {
"build": "ng build --prod=true --build-optimizer=true --aot=true",
},
And runt it with npm run build.
As #Andrei stated, is not possible to use NVM to set the Node version at the beginning of pipeline because it will change the version in all open consoles (so, if another pipeline with a different version of Node is running, would be affected).
Luckily, I found an easy workaround which does not require install additional tools or change package.json:
Download the node version you want as zip file
Unzip to a folder in Agent (like C:\LocalNode\node-v17.8.0-win-x64)
Add Node path to PATH environment var only for current pipeline
To add Node path only for current pipeline, we have to add a Powershell task as first task of the pipeline with the current command:
Write-Host ("##vso[task.setvariable variable=Path;]D:\LocalNode\node-v17.8.0-win-x64;$Env:Path")
Rest of the tasks of the pipeline will use the Node version from D:\LocalNode\node-v17.8.0-win-x64
If you want to create a pipeline for a different Node version, just add the version to D:\LocalNode and use the above command with the right path as first task of the pipeline. No problem if both pipelines run at the same time.

wedriverio jenkins - npm command not found error

I am trying to run webdriverio test from jenkins and following this . I installed nodeJS plugin for jenkins and under global tool config, for NodeJS , added a name and installation directory as /usr/local/bin (node executable is in this folder)
In Jenkins, in project config , under build environment, I have checked
Provide Node & npm bin/ folder to PATH and chosen the node installation name from previous step
But when I am trying to run npm install
npm test ,by adding these commands under execute shell under Build and building it, I receive this error
Building in workspace /Users/Shared/Jenkins/Home/workspace/wdio-project
[wdio-project] $ /bin/sh -xe /Users/Shared/Jenkins/tmp/hudson1478028169114509075.sh
+ npm install
/Users/Shared/Jenkins/tmp/hudson1478028169114509075.sh: line 2: npm: command not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE
In case someone is looking for same issue, I resolved the above error by adding export PATH=/usr/local/bin to the execute shell under build. This post helped me
There is currently a bug opened about this (JENKINS-26583 and JENKINS-27170)
I could workaround this bug by adding explicitly node on the PATH by adding this line :
export PATH=$PATH:/home/jenkins/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node_js/bin/
I had just fixed the same issue as you did.
I wonder if my fix would also help you. When I was using the alpine-based docker image, the Jenkins can never find the npm, but when I use the debian-based docker image, problem solved immediately.
Try this export PATH=/usr/local/bin:$PATH
After installation of NodeJS restart pc
In my case this worked -
export PATH=$PATH:/usr/local/bin

Jenkins and NodeJS

So I have a MEAN application up and running and Im looking into a continuous integration solution. I have successfully gotten Jenkins up and running with web hooks that grab my project from a bitbucket repo when a merge happens to master.
Right now I do not have any tests so Jenkins just runs some shell commands that 'deploys' the server. Which is great. My goal would be to have this run tests and fail a deploy if they fail.
So my problem is that the build never completes. My goal would be to when it completes it will keep the server running or deploy it and keep it running.
Here are the shell commands I run one the build is kicked off.
npm install
npm install bower
bower install
npm install grunt-cli
grunt prod
node server
And it successfully runs the server and such but it just hangs up after the node server command is executed
How do I make it so Jenkins sees this as successful and then deploys it? I have crawled the internet with no much luck.
EDIT:
So looking at some docs and such. I would need to configure my tests to run when the build gets ran. If there are not tests then it passes (by default)... So what I need is when that happens, jenkins needs to run a deploy script. After looking around in jenkins I am still unable to figure out how to do so.
EDIT #2
So moving those shell script out of the build allows it to finish and is 'successful' since no tests are present. I see that jenkins keeps the project in a workspace directory. Is there a way to get jenkins to deploy from there or some kind of other application to deploy that build in that workspace?
Thanks
So what I ended up doing was something simple but im not sure if its best practice..
Jenkins has the webhook to my bitbucket repo and watches for pulls into master. This then kicks off my jenkins build which it runs
npm install bower
bower install
npm install grunt-cli
grunt prod
which builds the project. Then I installed nodemon which watches the last "successful build" folder run the server from there. When it gets refreshed the server restarts. This seems to run pretty smoothly so far.

Jenkins integration with Grunt

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.

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

Resources