I’ve got a project that’s written in Node that uses forever to keep it running. I’ve always been a fan of Jenkins to process and handle the various tasks of building and releasing. Hooking Node to Jenkins was something that took a little time to get right. Here’s my code:
The Build Step is Execute Shell and here’s the code I’m running:
npm install export PROJECT_CONFIG='production' if forever list | grep 'project.js' ; then forever stop project.js fi forever start project.js forever list | grep 'project.js'
- updates your node project’s requirements
- Sets an environment variable of PROJECT_CONFIG to ‘production’
I’ve opted to use a special config name as well as a special js file name so as to allow Jenkins to run multiple projects at once, if I need to.
- Forever keeps a list of all node files currently active, we need to look through the list and see if our project is already running
- If our project is running, we stop it.
If we used restart, there’s a possibility that the config environment variable wouldn’t be set properly.
- Finally, start the project
- Ensure that the project is in the list of running projects (and get a location for the log file)