Create a gist now

Instantly share code, notes, and snippets.

@spion / Secret
Last active Dec 19, 2015

What would you like to do?

Migrating from forever to nac

Creating a nacfile

Add a nacfile to the project, then (optionally) add it to git. For example:

command: node
  - app.js
  NODE_ENV: production
  PORT: 8130

Whatever you put in env will be available from the process.env global in app.js. This means you can also edit *app.js` to change the listening command:

app.listen(process.env.PORT || 8130);

which means you can now control the port from the nacfile.

For other languages, search their documentation for APIs to access environment variables.

Create a nac app on the server

If the file is not in git, use scp (or other means) to copy it on the server.

Otherwise, if you deploy via git push you can just push the project with the added nacfile.

Alternatively, login to the server via ssh and do a git pull from the project directory to get the nacfile.

Now you can create a new app based on this nacfile. Pick a name for the app and then use

nac <appname> create path/to/nacfile.yaml

This will add your app to nac. You can check the status with

nac <appname> status

You will see that it's not running. nac doesn't start the app without explicitly being told to start it. Once its told to start it, it will try to keep it started at all times (remembering it across daemon/system restarts)

Update post-receive script (git push deploy only)

If you deploy via git push, you will need to find and edit the post-receive script. It can usually be found one directory up from the project dir. e.g. from the project dir type:

vim ../

There, find forever restart jsfile.js and change it with nac <appname> restart

Do the switcharoo

forever list

Find out which one is the app you're replacing then use the ID number it gives you to stop it

forever stop <ID>; nac <appname> start


forever stop 2; nac doxbee start;

At this point the old app will be stopped and the new will be started.

Switching away from post-receive

You can also do deploys via nac instead of using a post-receive hook.

Copy your post-receive script, but also add the git fetching and pulling at the top.

You can use $NACNAME instead of hard-coding the app name, $NACDIR for the working dir of the app and $NACFILE for the location of the yaml config file.

Then, add it to the scripts section in nac. E.g.

  deploy: ./scripts/

Here is an example deploy script:


if [ -z "$refspec" ]
git fetch upstream && git stash && git checkout upstream/$refspec
npm install
if [[ "$NACNAME" =~ .+-test ]]
    # simple restart works for all apps
    nac $NACNAME restart
    # use cluster reload signal for production
    # (works if your app is using learnboost up or recluster)
    nac $NACNAME kill SIGUSR2

You can now use this command to deploy the master branch of the app:

nac myapp run deploy master

It will fetch and checkout master, install all npm modules, run the grunt build script then gracefully reload the app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment