Rails why use capistrano when you have git post-receive hook? - ruby-on-rails

I'm not familiar with Capistrano at all, I've just read some basic posts about configuring it. But my question is why bother with Capistrano if you can deploy your Rails application files using a git post-receive hook?
What does Capistrano offer that the hook doesn't?
I was looking into automated ways of deploying my Rails apps without using FTP and came across both git hooks and Capistrano.
Or am I missing something?
Thanks.

Git hooks allow you to execute script related to the git repository on a given action.
Capistrano allows you to administer your production deploys.
A single call to 'cap deploy' will clone your master branch on your target machine, archive the previous release, precompile your assets, restart passenger. Capistrano can also migrate your database, or execute any number of arbitrary commands you want on any server configured in your deploy.rb file.
Go with Capistrano for deploying to app/db server environments, you will have no trouble finding help when you get a problem with a deploy.

Related

deployment not updating the local branch using capistrano

I am trying to deploy the changes in the repo I made locally(merged master to local branch), but while the changes work on localhost, it does not update in the deployment, using bundle exec cap preproduction deploy.
I am new using capistrano.
Any idea what I am doing wrong?
Thanks

Use capistrano to only update with git instead of deploying whole new release

I have a very small app that even uses sqlite3 in production because there are never going to be any issues with multiple writes, etc. I want to use capistrano to quickly and painlessly deploy updates to this app. But when I run cap production deploy it dumps the entire app into a release folder and symlinks it to current. I know I can include the production.sqlite3 file in the deploy.rb to keep the data but it still seems overkill to clone the entire repo every time I want to push an update.
I couldn't find anything in the capistrano documentation for updates.
Essentially all I need cap to do is
make sure my local git HEAD is the same as master
SSH into the prod server and do a git pull
Run rake db:migrate if necessary
Run rake assets:clean assets:precompile
Restart Phusion Passenger
How would I accomplish that?
Just write your own bash or ruby script that does this. I think you are missing the point of Capistrano. Cloning the whole repo allows you to do deploy rollbacks, leaving the previous version as is. It takes into account deploys that fail, and will not mess with your production site during the deploy process.

Straight-forward Rails deployment

I am new to ruby and I want to learn more about how it works. So I have been testing a server configuration in a virtual machine to make quick Rails deployments.
I have RVM, Ruby, Rails, Git, Gitolite, PostgreSQL, Thin and Nginx running in an Ubuntu 10.04 environment.
Now I want to tie everything together. I got stuck, though, in the deployment process.
After I commit the project to the Git trunk, I want to hook a deployment action to put the application in the correct place, set to production, install the bundles, make the migrations and restart Nginx.
But I fail to find simple references on how it works. All I find in google are guides to use passenger, capistrano and others. I want to trigger the deployment on the git commit action, similar to heroku, but what would be the best tools to do that 100% server-side?
What about making some shell scripts? How do I deploy a project manually? What are the steps? Are there any guides out there that do not assume I know every details in Rails deployment?
Thanks!
The think you are probably looking for is a git post-receive hook (a tutorial could be found here: http://toroid.org/ams/git-website-howto).
By this hook you should trigger eg. a shell script which should perform all the steps you need - which are:
checkout HEAD commit from the git repo (git checkout -f, see linked tutorial)
run bundle install
run bundle exec rake db:migrate - this assumed that you have already created your DB
restart/start the Thin server cluster (no sure exactly here, if it is similar to passenger which I use this operation is just to create some restart.txt file) - I presume that you have your nginx as a reverse proxy in front of it, right?
This is the long-story short. It is little bit more complicated, eg. if you use the asset pipeline (rails >= 3.1), you would like to precompile you assets, etc. But the above is a good starting point.
Well, I managed to get it almost completely operational.
The main actions I could trace until now are:
User pushes to trunk, must use git hooks to trigger the next steps using a script.
The script must do the following:
Clone the project to the /var/www folder;
Insert the 'thin' gem into the Gemfile;
Run 'bundle' command in the application folder;
Precompile the assets in the application folder;
Migrate the database;
Stop nginx and thin;
Restart thin and nginx again.
If the application is new, we must also:
Create a new user that matches the database information;
Create the production database;
Insert a new nginx configuration file;
Export the thin configuration from the application folder, like this:
thin config -C /etc/thin/app.yml -c /var/www/app --servers 1 -e production
The sequence of actions is more or less this:
$ bundle package
$ bundle install --deployment
$ RAILS_ENV=production rake db:migrate
$ rake assets:precompile
$ thin start -C /etc/thin/app.yml
This is the basic by now. I want to make it work 100% and then I want to post a guide on the Internet.
Update
The guide I said I would do:
https://github.com/sentient06/RDH/wiki

Development of Rails with github after deployment on Heroku

I have a Rails 3.2.3 app that I host on github .Today I deployed it on Heroku.
During deployment, I made some changes to the Gemfile.
Run rake assets:precompile which generate a /public/assets folder.
I had to add and commit those change to local repository in order to
push to heroku by running git push heroku master
The deoployment was fine but now my development environment is like:
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
Those are the changes I made for deployment on heroku (run precompile and some other stuffs) that I don't want to exist on my github repo. How do I do to continue my development (be able to push on github) and keep updating the change on heroku) ?
Clarification: public/assets is generated and should be added to local repo in order to push to heroku. So I can't put it in .gitignore But I don't want it to be pushed on github. What is the best practice?
I'm not sure why you're running rake assets:precompile in development. Are you debugging something? In general, it's best not to commit compiled assets to your repo since they are generated content.
On Heroku, you have the option to precompile your assets during slug compilation. This makes for a cleaner repo albeit a slightly slower deploy. Just remove public/assets so Heroku knows what to do. Also, make sure you add the following line to config/application.rb:
config.assets.initialize_on_precompile = false
https://devcenter.heroku.com/articles/rails3x-asset-pipeline-cedar

How do I target a specific commit SHA with capistrano deploy

I am wondering how I can target a specific commit SHA in Git for deployment, using Capistrano? It should be something like
cap deploy --version=<sha targeted>
Can't seem to find the answer to this after a lot of searching.
For Capistrano 2.9 until 3.0:
cap -S revision=80655da8d80aaaf92ce5357e7828dc09adb00993 deploy
For older versions of Capistrano, you can deploy a particular git commit/tree/branch/tag by doing this:
cap -s branch=80655da8d80aaaf92ce5357e7828dc09adb00993 deploy
In some cases there may be a need of specifying the Environment as an argument as well. production is just an example.
cap production -S revision=80655da8d80aaaf92ce5357e7828dc09adb00993 deploy
molf's answer didn't work for me (using capistrano 2.11.2). I had to use "revision" instead of branch, like this:
cap -S revision=80655da8d80aaaf92ce5357e7828dc09adb00993 deploy
Capistrano 3
In your deploy.rb or stage-specific file like config/deploy/production.rb
set :branch, ENV.fetch('REVISION', 'master')
This allows you to point to a specific git revision. It accepts a SHA but also anything that resolves to a real revision (e.g. git tag, annotated tag, or branch).
Use it on the command line by setting the REVISION environment variable, e.g.
bundle exec cap production deploy REVISION=80655da8d80aaaf92ce5357e7828dc09adb00993
bundle exec cap staging deploy REVISION=my-topic-branch
ask :branch, 'master'
Prompts for input but defaults to 'master' if you press return.

Resources