Jenkins script quitting prematurely when using npm install on Windows - node.js

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

Related

NodeJS Build Package (zip) using Grunt on Jenkins

I'm trying to build a package (zip) using grunt in Jenkins (though Jenkins is kind of irrelevant at this stage).
Basically the code is checked out of git, and I run
npm install --production
but grunt needs the files installed locally.
So I run
npm install
But now I have all the grunt packages in the node_modules directory.
As part of the grunt build I simply want to copy the node_modules directory into the package.
Am I headed down the right track with trying to package my code using grunt?
Or should I be using grunt to only run jslint, unit tests etc and use this to package: https://github.com/zeit/pkg
The output is another tool that I will be calling from the command line.

how to set grunt variable env path

I am trying to set grunt for my node.js project and I have followed below steps:
1)I have installed node.js and it is working fine.
2)Installed git.
3)Installed grun by running: npm install -g grunt-cli.
C:\Users\user\Downloads\bpost-gs-api-1.1.1>npm install -g grunt-cli
C:\Users\user\AppData\Roaming\npm\grunt -> C:\Users\891153\AppData\Roaming\npm\node_modules\grunt-cli\bin\grunt
C:\Users\user\AppData\Roaming\npm
`-- grunt-cli#1.2.0
Now I have run the cmd grunt -version, I got below error:
grunt is not recognized as an internal or external command,
operable program or batch file.
Can you please help me to set up grunt for my project. How to setup variable env and how to setup path etc.
Appreciate your help
https://gruntjs.com/getting-started
In order to get started, you'll want to install Grunt's command line interface (CLI) globally. You may need to use sudo (for OSX, *nix, BSD etc) or run your command shell as Administrator (for Windows) to do this.
npm install -g grunt-cli
This will put the grunt command in your system path, allowing it to be run from any directory.
Note that installing grunt-cli does not install the Grunt task runner! The job of the Grunt CLI is simple: run the version of Grunt which has been installed next to a Gruntfile. This allows multiple versions of Grunt to be installed on the same machine simultaneously.
start from reading this:
https://gruntjs.com/installing-grunt
If you need a specific version of Grunt or a Grunt plugin, run npm install grunt#VERSION --save-dev where VERSION is the version you need.
for better understood on how to start setup a grunt project: https://gruntjs.com/getting-started#preparing-a-new-grunt-project
A typical setup will involve adding two files to your project: package.json and the Gruntfile.
package.json: This file is used by npm to store metadata for projects published as npm modules. You will list grunt and the Grunt plugins your project needs as devDependencies in this file.
Gruntfile: This file is named Gruntfile.js or Gruntfile.coffee and is used to configure or define tasks and load Grunt plugins. When this documentation mentions a Gruntfile it is talking about a file, which is either a Gruntfile.js or a Gruntfile.coffee.

Is there good way to run a script only when the user install my npm package manually?(not installed by dependency)

I'm under developing my npm package and I want to run a specific script only when user installed my package manually like npm install my-package --save-dev or something npm command.
I'd not like to run the command when the package was installed via package dependency.
For example, My package is dependency of the other package other-package.
Even if the user installed other-package manually and npm should install my-package as dependency, I'd not like to run the script.
Is there good way to handle this?
npm has a set of scripts that will automatically run when npm is launched a particular way. The scripts you might be interested in are:
prepublish: Run BEFORE the package is published. (Also run on local npm install without any arguments.)
publish, postpublish: Run AFTER the package is published.
preinstall: Run BEFORE the package is installed
install, postinstall: Run AFTER the package is installed.
There is no event that exactly matches your criteria but you could use one of the install events and then have an intermediate script that detects the npm command line options before your actual script.
Due to the (imo horrible) way prepublish works, a number of people have written modules to do a similar task and these could easily be adapted to your requirements.
iarna/inpublish is a good example. It check's process.env['npm_config_argv'] for the existence of /^i(n(s(t(a(ll?)?)?)?)?)?$/
Using the following package.json setup:
"scripts": {
"postinstall": "my-install && install-manual-tasks || not-my-install"
}
If my-install uses process.exit(0) then install-manual-tasks will run. If you process.exit(1), not-my-install will clean up so the npm task doesn't fail.
I think this setup actually has an issue. If your install-manual-tasks fails, the exit status is silenced and the npm task won't fail but it's a start at least. You could work around this by doing all your checks in the install-manual-tasks script, then you don't need use the shell tricks to run multiple scripts.
You can take a look at https://superuser.com/a/105389/627275 to see how to create a shell function that acts as an alias to a command. This way, npm install would act as an alias to whatever you want to run in reality. It could also be an alias to bash something.sh & npm install.
Hope that answers your question!

Continuous integration and deployment of Node.js application on Bamboo

The application I want to implement continuous deployment on Bamboo has node modules and bower component dependencies. On the bamboo server nodejs, npm have been installed.
There are only three tasks on default job:
Source Code Checkout
Build dependencies:
npm install
bower install
Deploy to the staging server
The problem is on the second task, bamboo fails with the message "No failed tests found, a possible compilation error occurred." I don't even run any tests.
The log file is not explanatory at all:
Starting task 'Build dependencies' of type 'com.atlassian.bamboo.plugins.scripttask:task.builder.script'
Failing task since return code of [/bin/sh /home/ubuntu/bamboo-installation/temp/WEB-WEB-JOB1-8-ScriptBuildTask-4430338079602360707.sh] was 1 while expected 0
Ok, I solved the problem. The issue was the wrong node (which obviously messed things up) was installed on the bamboo server. Uninstalled the wrong one and everything worked as expected.
Good to see you solved it.
There is a setup I use and which could prevent further problems with CI:
export npm_config_prefix=.npm/
export PATH=.npm/bin:$PATH
export CI=true
npm install -g bower
bower install
npm install
This installs bower (and others like grunt-cli if you want) in your project folder so you can e.g. have a specific version, sets CI=true as advised in bower docs, and then installs all dependencies.
Bamboo AMI originally have npm version 1.4.28 installed and you are probably using a more recent version on you development environment. I had the same issue and resolved it by creating a script task to update npm version on the very beginning of my build process. Here is the script:
# update npm
curl -O -L https://npmjs.org/install.sh
chmod +x install.sh
sudo PATH=$PATH:/opt/node-0.10/bin ./install.sh

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.

Resources