Running nodejs command on gocd pipeline - node.js

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.)

Related

npm command is not found when ssh with bitbucket pipelines on shared hosting

I've installed nodejs as described here.
Everything works fine when I ssh to the server myself. But I've created a script that deploys my application and call it via bitbucket pipelines. Everything else works fine (composer install, php artisan migrate etc.), except npm install. The error message is
./deploy: line 26: npm: command not found
In bitbucket-pipelines.yml I call my script like this:
- step:
script:
- ssh user#ip_address ./deploy
When I call the script by myself everything works. The problem is only with bitbucket pipelines. I have no clue why this happens :(.
Running which composer revealed that at least composer command is not getting picked up from your assumed location i.e., ~/composer dir. Instead, it was found in /opt/cpanel/composer/bin/composer.
which npm returned the following:
no npm in (/usr/local/cpanel/3rdparty/lib/path-bin:/usr/local/jdk/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/usr/local/bin:/usr/X11R6/bin:/root/bin:/opt/bin:/opt/cpanel/composer/bin:/home/handmast/.local/bin:/home/handmast/bin)
Now since you are able to manually run the command npm, you just need to figure about the path from where npm is running and ensure that the path is explicitly added to the user's ~/.bashrc file and things should work fine. You need to do this because as per your observation, ssh is not able to find it.
export PATH=$PATH:/path/to/npm/binary
Note: To avoid any confusion, just remember that while adding the path to your binary, you just have to add the path to the directory where npm resides. Don't add npm at the end. For example, following is incorrect way:
export PATH=$PATH:/home/handmast/bin/npm
Correct way is this:
export PATH=$PATH:/home/handmast/bin

TFS 2017 on-premise cannot find node modules in cmd

How to run npm modules in tfs agent running as a service?
Here's my setup.
I setup agent on local machine with TFS 2017 running on-premise.
And I try to build aurelia application on agent running as a service.
I already have installed node, aurelia cli and npm in machine where the agent run. It was globally accessed running manually cmd with command au build(aurelia cli build). However when I create a Command Line build step running au(Tool) and build(Arguments) the agent does not find au.
I think it will also not find other npm modules.
In general, the build agent (running as service) uses Network Service account and the installed npm packages are in current user folder, so you can call au command directly, but can’t call au command during the build.
You can change build agent service account to use your account or refer to these steps to install Aurelia-cli package and call au command in Command Line step if you still uses Network Service account:
Add Command line step to build definition (Tool: npm; Arguments: install Aurelia-cli -g)
Add Command line step (Tool: C:\Windows\ServiceProfiles\NetworkService\AppData\Roaming\npm\au; Arguments: build; Working folder: [project folder] (e.g. $(Build.SourcesDirectory))
Note: the step 1 just need to run only once, you can disable or delete it for next build to save time.
On the other hand, you can try to add C:\Windows\ServiceProfiles\NetworkService\AppData\Roaming\npm to environment variable (system variables > Path), then use au directly.

cordova plugin add via git fails, but only from a TeamCity build agent process

Our cordova builds in TeamCity have started failing while adding plugins. The plugins that are fetched via npm work fine. There are a few which we still install via git for various reasons (haven't been added to the registry, internal/private, etc.)
The error we get is the infamous
fatal: Unable to find remote helper for 'https'
Questions about that error have been asked before, but the solutions to those questions have not helped, as this problem only exists in the context of a TeamCity build agent process spawning a cordova plugin add which in turn spawns the git clone (via node's child_process.spawn).
Some things we know so far
On the same build agent machine, as the build agent service account,
we can add this plugin at a command prompt with no issues.
On the same build agent, same service account, we can git clone the associated repo at the command prompt with no issue.
We altered the cordova source to log out the cmd, args,
and spawnOptions passed to child_process.spawn in superspawn.js. We watch
that fail from the TeamCity build agent process, but can send the
same args to child_process.spawn in our own node script and it clones fine. For what it's worth, here is the child_process.spawn line created from the args in the build log: child_process.spawn('C:\\Program Files\\Git\\mingw64\\bin\\git.exe ', ['clone','--depth=1', 'https://github.com/EddyVerbruggen/cordova-plugin-backgroundaudio','C:\\TeamCity\\buildAgent\\temp\\buildTmp\\git\\1448324988055' ], {});
We've confirmed that the build agent process is running the same
version of node, npm, and git as we have interactively at the
command prompt.
We don't see any environment variables in the build agent process
that seem relevant or different from what we have at an interactive
command prompt.
We've tried reinstalling git, node, and the build agent itself.
Nothing has seemed to help. I'm hoping that someone else has seen this before and can point us toward what we're missing. If not, I'll answer my own question once we get this sorted out.

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

how to run npm/grunt command from jenkins

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.

Resources