how to run npm/grunt command from jenkins - node.js

I'm new to this area and was trying to run the following commands from jenkins:
npm install
grunt quickStart
So far I've a jenkins running on a window machine as a window service and I've also installed NodeJs plugin for jenkins.
However, I'm stuck and quit confused following instructions here, its asking me to to add one or mode nodeJs installation and I could not find those setting and not even sure if I even need them in the first place.
Here is the bit that's asking me to do:
I cannot see this setting for the jenkins job I create. Is there an easy way to run those command in jenkins from a .bat or .sh script, a .bat would be recomended since I'm on window machine.
Note : I've already checked out a project using git in jenkins!!!
Thanks

"its asking me to to add one or mode nodeJs installation and I could not find those setting and not even sure if I even need them in the first place."
I don't think you need that I have pointed jenkins to the node installation folder an nothing more.., for this you go to Manage Jenkins->Configure System->NodeJS->NodeJS installations.. type in any name you like and point to where the node home folder is.
...cannot see this setting for the jenkins job I create...
Once you have configure that in your jenkins configuration you should have that configuration available like so:
...Is there an easy way to run those command in jenkins from a .bat or .sh script, a .bat would be recomended since I'm on window machine
I'm sorry don't get what commands are you referring to...
so summarizing :
you have to tell jenkins where you have your node installation
use that configuration in your jobs
hope this helps...

The way I made it, was trough execute shell, as the build tool for nodejs is npm, I simply wrote a shell script that instructs jenkins to run npm install in the workspace directory inside jenkins where it clones the git repository and then to zip and move the package if successful to another folder.

Related

How to create git hooks in Windows with Node.js?

I have been following this guide on how to use Node.js to script git hooks. However, the guide is using a Unix based system whilst I am running on a Windows machine.
I have also found this guide on running git hooks on a Windows machine, but it is not using Node.js
I am running a pre-install script in my package.json file to set a custom git hooks location.
I am using VSCode as my editor and would like the git hooks to run when I use the UI for commits etc. However I am using command line initially to try and get the hooks to fire.
package.json excerpt
"scripts": {
"preinstall": "git config core.hooksPath ./git.hooks"
},
In my git.hooks folder I have a pre-commit.js file.
I have updated the first line to reflect the fact I'd like to execute the script running Node.js
pre-commit.js
#!C:/Program\ Files/nodejs/node.exe
console.log('Hello world!');
process.exit(1);
If I run this script directly I get a Microsoft JScript compilation error - Invalid character on line 1 char 1.
If I do a commit, I get no errors but nothing happens.
Can anyone guide me through the process of creating a Node.js hook in Windows. I would rather create one myself than use a package.
Name the hook exactly pre-commit, without .js.
Change the first line to #!/usr/bin/env node. But make sure that C:/Program\ Files/nodejs/node.exe has been added to the environment variable PATH.
Place it in <repo>/.git/hooks.
Make it executable. In git-bash, run chmod a+x <repo>/.git/hooks/pre-commit.
Now it should work as expected.

Running nodejs command on gocd pipeline

I created a new gocd pipeline and have three shell script files to run on different stages.
The problem is the go agent doesn't know npm.
Note: I have npm installed on the machine with go agent and I manually run the shell script from the pipeline.
Here is my shell script to install the packages.
#!/bin/sh
npm install
The error:
01:34:43.674 [go] Start to execute task: <exec command="./install.sh" />.
01:34:43.680 ./install.sh: line 3: npm: command not found
01:34:43.814 [go] Current job status: failed.
Problem
Assuming you have npm/nodejs installed on the agent, the problem probably lies in the fact the user doesn't have its PATH environment variable configured to look into the folder npm was installed in.
Solution
1) You can specify the whole path (/usr/bin/npm) when creating a task.
2) You can edit the .bashrc/.bash_profile of the user running the gocd agent server. In which case you'll be able to call '/usr/bin/npm' without the path prepended.
Example Working Configuration
Consider modification of the agent init script. Changing .bashrc/.bash_profile of the user running the gocd agent does not work because the go agent insulates itself from the calling environment. So on our systems we add these PATH items to the go agent startup scripts. (I use puppet to create agents. The default agent init scripts are not that good - you need to own them.)

NodeJS installation to jenkins : npm not found

Maybe someone can tell me what I missed installing nodeJS to jenkins, because when I want to execute shell script npm update I get error npm: not found.
I installed jenkins nodejs plugin then added installer:
After I created job with prams:
What I need to do more, to have accessible npm in any jenkins job?
It works for me. The only difference is that where you have a text box that reads "0.12.7", I have a popup menu that reads "Node.js 0.12.7". Maybe something has gone wrong and you should delete the installer and add it back?
Windows users, did you restart?
Do keep in mind that you need to restart active processes after installing npm.
During installation the folder of your npm.cmd is added to the PATH variable. However, active applications don't refresh their PATH variable automatically. That also holds for your active Jenkins service (and its child processes). They are still running with an old version of your PATH variable.
In other words, you need to restart the Jenkins service (or restart your entire system). Once restarted, Jenkins will have an updated PATH variable, and it will be able to find the npm.cmd processes and others.
What's wrong with using absolute paths?
Using the full path is not a solid solution. A package.json file can contain references to other cli processes (e.g. node, ng, react-scripts, npm-run-all, ...). You will probably have similar issues there. There is no way to keep up and reference all of them with full paths.

Jenkins build step fails when calling "npm" on mac-os-x Yosemite

Before I start, I want to say that I already checked these answers:
Jenkins build step fails on 'npm install <whatever>'
Jenkin's build failing on npm install
Now, I'm dealing with this issue for a while already and thus I tried a bunch of stuff.
Firstly, I installed node + npm via homebrew. A simple $ node -v and $ npm -v echoed the version v0.10.36 for node and v2.3.* for npm, which also means I HAVE THEM IN THE PATH and they work while called in the terminal.
Simply adding node -v; npm -v to the execute shell in Jenkins didn't do it. After a bit of tinkering I copied what $: which node yielded in the terminal to the above mentioned script, which now looked like this: /usr/local/bin/node and apparently that worked. The Jenkins build succeeded and 'node-v0.10.36' was proudly displayed in the console output.
When doing the same for 'npm' which happened to be /usr/local/bin/npm --version the computing gods weren't so merciful anymore. A big 'env: node: No such file or directory' error was thrown this time and the whole build failed.
The actual command that fails is
$ /bin/sh -xe /var/folders/wr/g_dl81tn5_x0t_yz3jw602cr0000gn/T/hudson8770480548136671253.sh and "surprisingly" when I run the same command in the terminal it succeeds.
I also uninstalled the homebrew node & npm versions and installed them afterwards via the package manager. Same results.
Ultimately I also did this: https://gist.github.com/DanHerbert/9520689, with no luck.
Notes:
I'm running Jenkins 1.613 and tried with 1.5**
I didn't create a "Jenkins" specific user but instead I'm using the admin. This happens to be the same user that Jenkins runs, since the who am i command inside the executable script yields the admin's user name.
sudo'ing doens't help
I'm also running the whole thing in a Virtual Environment - vagrant
I'm not running Jenkins as a deamon, as it's conflicting with xtools, but as a simple process
I also tried out jenkins-node plugin with various configs (can detail if needed)
Thanks a lot for your help, and let me know if you need any other info, screenshots, logs, etc.
I found my own solution. The problem was that the PATH although visible in shell was not exported for the Jenkins job, and so, the first workaround, as found here, was to export it in the actual script like so:
but this feels like a hack!
The right and elegant solution is to use Jenkins EnvInject Plugin and export the path in the added Properties content textarea on the configuration page, like so:
Manage Jenkins -> Configure System -> Global properties -> Environment variables

Can Jenkins monitor cron jobs that I run using crontab?

I have written some cron jobs using the crontab feature on my ubuntu. Can I use Jenkins to monitor these jobs?
Edit 1: I have Jenkins already installed on my local machine: localhost:8080. So right now, I have cronjobs running on crontab. I cannot create a new job on Jenkins and run them on Jenkins's shell because of certain requirements.
Following Pragmatic's solution, I looked at https://wiki.jenkins-ci.org/display/JENKINS/Monitoring+external+jobs and tried to sudo apt-get install jenkins-external-tool-monitor, but I am getting: E: Couldn't find package jenkins-external-tool-monitor. Any work arounds?
Edit 2: I have figured it out. Very Important: I tried to set this up using jenkins-core-1.47*.jar file, and it didnt work. It did work with jenkins-core-1.466.jar. So if you are using some .jar file, stop jenkins, replace it with a jenkins.war that corresponds to jenkins-core-1.466.jar and start jenkins after. That should work!
Yes, you can do that!!
Have a look at this link: Monitoring external jobs.
I could have explained the whole method here itself but the details in the question were minimal.
Hope this helps you.
Possible...See this
https://wiki.jenkins-ci.org/display/JENKINS/Monitoring+external+jobs
As others have stated above, follow: https://wiki.jenkins-ci.org/display/JENKINS/Monitoring+external+jobs
Then I started getting this ugly error: https://issues.jenkins-ci.org/browse/JENKINS-14107
If you face this problem as well, replace the current jenkins.war file with one from version 1.466. That is the one that worked for me. Before I replaced the war file, I stopped the jenkins server by : /etc/init.d/jenkins -stop and then replaced the war file and then started the server: /etc/init.d/jenkins -start
An alternative approach is to use an instance of Jenkins to replace your cron jobs entirely.
See:
Drop that cron; use Hudson instead
Replace Local Cron With Jenkins

Resources