2

Previously and on my local machine, I've been using nodemon which watches for changes made to a Node app and reloads it upon every change. But running the development server on my own machine is no longer feasible, so I've setup git for the app on a designated development server.

In advance, I prefer Sublime Text, so editing files on the development server via the terminal doesn't match my workflow, plus I like having a copy of everything on my local machine by default. I had also checked out rsync, but I like the fine-grained version control that git offers.

So how can I edit files locally, git push them to a development server, and have the Node app automatically reload after every push?

timbur
  • 558
  • 6
  • 10

2 Answers2

8

You can write a server side hook. In your .git directory there is a hook directory. Just cd in to .git/hooks. There you can write a script in whatever language you need to write it in. Essentially after you push it will run the script you tell it to. Here is more information on git hooks

https://git-scm.com/book/es/v2/Customizing-Git-Git-Hooks

Quick tutorial to make this work:

  1. On the development server, navigate to /home/dev-user/Node and initialize bare repository at /home/dev-user/Node/example.git using git init --bare example.git.

  2. Clone repository into /home/dev-user/Node/example using git clone example.git.

  3. Add files to /home/dev-user/Node/example as necessary, then git add . and git commit -m "init" and finally git push origin master which will push those files to example.git.

  4. Edit or create /home/dev-user/Node/example.git/hooks/post-receive and add the following line: GIT_WORK_TREE=/home/dev-user/Node/example/ git checkout -f

    This will automatically update the files in /home/dev-user/Node/example/ upon any changes pushed to /home/dev-user/Node/example.git.

  5. If you don't have nodemon installed already, install it with npm install -g nodemon. You may have to use sudo.

  6. Assuming your main Node app is located at /home/dev-user/Node/example/app.js, start the app using nodemon /home/dev-user/Node/example/app.js (or if you're already within /home/dev-user/Node/example, just nodemon app.js of course).

  7. On your local machine, navigate to /home/timbur/Node, and assuming you're able to connect to the server automatically via SSH, clone the bare repository using git clone dev-user@dev.server.ip.address:Node/example.git. You'll now have everything in /home/timbur/Node/example.

  8. Edit files on your local machine and add/commit/push files to the development server as usual, and changes will automatically update the server's example directory, which nodemon will detect and the app will be restarted.

timbur
  • 558
  • 6
  • 10
technoY2K
  • 2,442
  • 1
  • 24
  • 38
  • Thanks! This pointed me in the right direction and I've got it working now. Do you mind if I edit your answer to describe exactly what I did? It's actually quite simple, as I had expected/hoped. :) – timbur May 20 '15 at 21:35
  • 1
    no problem, and sure please do, it'll probably help someone else a lot too. – technoY2K May 20 '15 at 22:03
0

The best way would be to setup a continuous integration server, like Jenkins: https://jenkins-ci.org/

And then there are plugins for basically whatever you want to do, like this one for node.js for instance: https://wiki.jenkins-ci.org/display/JENKINS/NodeJS+Plugin

But that's probably not the easiest way. You could also setup a post-receive hook on your server, that checks out the code whenever you push any changes, and then let it restart your server. Here's a gist I found (but never tried) https://gist.github.com/tlrobinson/8035884

Robert Falkén
  • 2,287
  • 2
  • 16
  • 16