Tintin81
Tintin81

Reputation: 10215

What is the best workflow for updating / deploying a Rails app through Git?

I just deployed my first Ruby on Rails app on a VPS at Digital Ocean.

To get started quickly, I did this by simply dragging my Rails directory tree (and its containing files) onto the server via (S)FTP.

I know this isn't the best solution in the long run. So how can I link my app on the server to my git repository at GitHub?

Ideally, when I work on my app locally, and then git commit and git push to my git repository, my app on the VPS will also get updated automatically.

How can this be achieved or what is the best strategy to achieve this?

Since I am building this app just by myself, I can probably keep things simple and stick to a single master branch, rather than having multiple branches.

Thanks for any help.

Upvotes: 1

Views: 315

Answers (4)

Michael
Michael

Reputation: 10474

You should spend a little time now setting up deploys with some automation. Since you are using rails, you should try Capistrano Gem

Capistrano will help you deploy and maintain your application with just a few simple commands. The Readme will show you how to get started, but in general, you will add the Gem by adding this to your Gemfile:

gem 'capistrano', '~> 3.2.0'

then run bundle install to install Capistrano into your bundle. If you are not already using bundler, you should start.

then run bundle exec cap install to setup your local repo for Capistrano.

Basically now you have a nice structure for deployment scripts as part of your repo. You will have to write some deploy scripts now, or modify the examples.

Once done, Capistrano will help you deploy new code (once committed and pushed to your remote repo) and restart the services.

Upvotes: 1

Jacob Brown
Jacob Brown

Reputation: 7561

If you can put your repo on your server, you can set up post-receive hooks to pull your branch into your web app directory.

To do so you would create a bare repo on your server, add it as a remote on your development machine, and then (on your server) create the file /my/app.git/hooks/post-receive and add these lines:

#!/bin/bash
#CONFIG
LIVE="/home/saintsjd/www"

read oldrev newrev refname
if [ $refname = "refs/heads/master" ]; then  
  echo "===== DEPLOYING TO LIVE SITE ====="
  unset GIT_DIR
  cd $LIVE
  git pull origin master
  echo "===== DONE ====="
fi

Code from Automated Deployment of PHP Applications using git, by Jon Saints.

Note that it would be possible to do something like this without putting the repo on your server if you use Github's webhooks (https://developer.github.com/v3/repos/hooks/).

However, I would highly recommend using Capistrano (https://github.com/capistrano/capistrano), which can deploy your application code and help you with a lot of administrative tasks (like restarting the server, etc.).

If you want to stick relatively close to git, you might also check out the git-deploy gem.

Upvotes: 0

hermansc
hermansc

Reputation: 738

If I were you, I'd do the pulling and updating on the remote manually. Sorry, but this is not only best practice, but will also force you to learn something useful about system administration and don't require you to be dependent on one host, but can switch service provider and setup as easy it is to make a git-clone somewhere else.

So my workflow would be:

Client:

# Do some changes, commit and add a nice message
$ git commit myfiles

# Push to remote once I'm happy.
$ git push

# SSH to server, and continue from there.
$ ssh username@server

Server:

# Enter project directory
$ cd /var/www/myproject

# Pull code
$ git pull

Done. Or perhaps finish by refreshing server container (uWSGI, fcgi, gunicorn, what have you...)

Reading other similar answers, they hint to looking at the following resource using Capistrano:

Capistrano documentation at GitHub

Upvotes: 2

Josh
Josh

Reputation: 511

It depends on whatever service you are using to publish your app. Depending on the provider, they may or may not provide rails service. For example, a site like Heroku, where you can actually host for free up until certain restrictions is accessible via github and you can do exactly what you're saying and just push up and publish.

Upvotes: 0

Related Questions