问题
I want to use jenkins to run a nodejs project. All is going well except I do not see any success status in jenkins job.
I started the server with npm install & npm start command in jenkins. It starts the server, but does not give build success, as a result, I can not start other jobs which depends on the build success of the node server & when a new commit arrives, jenkins detects it but the job does not restart rather remains at pending status as the original job is running.
I am new to jenkins. can anyone suggest anything for that??
回答1:
Sounds like the execution of your js script is hanging your build, try using a & after to send it to background:
npm install
npm start &
Also note that running npm start
from a Jenkins job will spawn a new process and Jenkins will kill the jobs spawned during a build, That is a problem if you want this process to remain as a daemon.
To avoid that you need to to provide a flag right before your command:
BUILD_ID=dontKillMe npm start &
You haven't been specific about all the steps in your job pipeline but I imagine that you try to perform some post-build operation after your first job's build so finding out that your process is not running causing will cause failure in subsequent jobs.
Also, try using forever. It will ensure that your script is running continuously until you stop it, with it you can start your application like this:
BUILD_ID=dontKillMe forever start yourApp.js
回答2:
The Problem: It seems you nodejs server is not running on jenkins after execution, you have couple of Solutions that were investigated and got tested:
Option 1 - recommended: run a background process on Jenkins that keeping alive using dontKillMe flag and nuhup unix command in BUILD_ID parameter.
From documentation: A convenient way to achieve that is to change the environment variable BUILD_ID which Jenkins's ProcessTreeKiller is looking for. This will cause Jenkins to assume that your daemon is not spawned by the Jenkins build.
nohup is a POSIX command to ignore the HUP (hangup) signal. The HUP signal is, by convention, the way a terminal warns dependent processes of logout.
cd <yourApp>
BUILD_ID=dontKillMe nohup npm start &
Option 2. using node.js external package such as forever or pm2
- PM2 is a daemon process manager that will help you manage and keep your application online 24/7 - link
- forever - A simple CLI tool for ensuring that a given script runs continuously (i.e. forever) - link
forever example (installed locally):
cd <yourApp>
npm install forever
./node_modules/forever/bin/forever start -c "npm start"
pm2 example (installed globally):
npm install pm2 -g
cd <yourApp>
pm2 start scripts/start.js
Testing: Afterwards, can verify your server is up running using bash script commands:
# check process
ps -ef | grep node
# check connectivity in server port
telnet localhost <yourNodeJSPortRunning>
P.S sometimes some interrupted signals can cause issues in your start.js
script and terminate the node process, try avoiding this - for example:
['SIGINT', 'SIGTERM'].forEach(function(sig) {
process.on(sig, function() {
devServer.close();
process.exit();
});
});
})
.catch(err => {
if (err && err.message) {
console.log(err.message);
}
process.exit(1);
});
After removing this piece of code it worked correctly, the process is on air as long as the machine is up.
来源:https://stackoverflow.com/questions/38039123/nodejs-server-not-giving-success-status-in-jenkins