Direkt zum Hauptbereich

ruby on rails deployment with capistrano and git

Hello everybody!

I mentioned it before me trying ruby on rails. The language is pretty fast forward with its meaning. And I am already in love with the rails framework, although not having worked with it full time, not even semi-professional.

The internet is full with howtos and tutorials, but sometimes the hard thing is to seperate them, and finding you own "truths", some are:


I ran into serveral problems like:

  • no pictures remotely -> precompile locally!
  • make sure EVERY gem is noted in Gemfile
  • rake failed almost everytime, and blaimed permission rules
  • ... [until 5 hours of my live were gone]
This is my approach to easily developing webapps:


[do not just copy and paste, I dont post all the code;
and in retroperspect I should have read into it more deep too to save time]


For easy access set up ssh with keys, do it locally with ssh-keygen and copy your public one to the ~/.ssh/[FILE] on your server, where [FILE] is the RSA-file specified in your /etc/ssh/sshd.conf (or so).

Now make sure there is one appropiate user for git to have access to your remote repo as well as to your ruby/passenger webroot.

Furthermore I put that user in the "apache group" (www-data or www or...) and chown the webfolders to user:"apache group" and chmod it to 770.

The next step is to set up a remote BARE git repo as the "git user" on your server:

mkdir -p ~/git/[APPNAME].git
cd [APPNAME].git
git --bare init

If using ssh (or not) do this in deploy.rb:

ssh_options[:forward_agent] = true
#default_run_options[:pty] = true # Must be set for the password prompt from git to work

Do the local git stuff as well and prepare your cap and deploy.rb files
(see first link).

[edit: also make sure ssh-agent is running!
Locally do ssh-add, locally! Put it into your bashrc.]

Now you should be able to run the first test with git:


cd your_local_webapp_folder
git add -A
git commit -m "my first commit"
git push

And this in your deploy.rb for loading the remote git, instead of uploading everything all the time:
set :deploy_via, :remote_cache

Before doing the capistrano stuff, make sure you have a project with database and everything, otherwise capistrano might strike because a database migration is not there or so on:

cd your_local_webapp_folder
capify .
cap deploy:setup
cap deploy:check
cap deploy:migrations

The setup will set up all the folders, e.g. a /current/ between your root folder and your app, to where passenger should point as well.

You could run into problems like libv8 not found or rake aborted.
Solve this by installing x64 AND x32 on your local machine,
and make sure you echo ".bundle\n" >> .gitignore locally for not include specifications of your local machine since it goes to the server.

So I manually installed the gems for x64 and did bundle install
again (also bundle pack, also git add Gemfile.lock vendor/cache).

Some problems are just based on the differences between local and remote machine, so test every capistrano step locally also, e.g. precompile.

For me the precompiling of assets did not work. I discovered the whole topic by being happy it worked but without pictures. So I did this in Capfile:

load 'deploy/assets'

But it did not work, so I commented it out again, and found this solution for your deploy.rb file:

desc "deploy the precompiled assets"
    task :deploy_assets, :except => { :no_release => true } do
       run_locally("rake assets:clean && rake precompile")
       upload("public/assets", "#{release_path}/public/assets", :via =>
:scp, :recursive => true)
    end

[edit: this works smoother:
    run_locally("bundle exec rake assets:clean && 
bundle exec rake assets:precompile RAILS_ENV=#{rails_env}")  ]

Now your assets are precompiled locally and uploaded, and I dont care if it is bandwidth sucking, for it is the only solution other than hacking rails.

Hopefully I got/fixed all the bumbs on the road.
Now you should be able to do this and have your app deployed:

cd your_local_webapp_folder
git add .
git commit -m "next deployment"
git push
cap deploy


And dont exspect me to help you since I am a novice as well ;)

PS 7.9.2013:
if you ever have to setup a new server [folder] then:
  1. I did a set :deploy_via, :copy
  2. then set :deploy_via, :remote_cache
  3. I cloned my local repo into a local bare repo
    git clone --bare . remote_bare_repo_name.git
  4. that file I upload via scp
    scp -r remote_repo_name.git remoteuser@domain.com:~/git/remote_repo_name.git
  5. now my bare repo on the server is up to date again
  6. git add .
  7. git commit -m "bla bla bla"
  8. git push
  9. and make sure these settings are there
    ssh_options[:forward_agent] = true
    default_run_options[:pty] = true




Kommentare

Beliebte Posts aus diesem Blog

Pflanzsack aus Jute

Nachdem ich die Bücher vom Arche Noah Verein schon vorstellte, möchte ich hier eine Topfalternative zeigen, die obendrein noch dekorativ ist. Pflanzsäcke Oder "grow bags", wenn man etwas moderner daher komme möchte bzw. mehr Suchergenisse will. Dabei wollte ich wenig Geld ausgeben, und auch nicht ganz spartan bleiben und den Blumenerdesack dafür nutzen; und bestellte mir 50 kg Jutesäcke für ca. 1,20 EUR/Stück. Nimmt man den ganzen Sack, wird der Topf schon recht groß, auch wenn man etwas des Materials nach innen oder außen faltet. Also nahm ich mir die Freiheit und nähte aus einem großen vier kleine und verbrauchte den letzten Rest unserer Erde fürs Umtopfen der Habanero. Die machte nämlich nun Platz für die sibirische Hauschili und die restlichen von beiden Sorten mischte ich zusammen und stellte sie auf die Fensterbank. Der Vorteil dieser Art von Töpfen liegt auf der Hand: sie sind billig im Vergleich zum Volumen sie sind dekorativ - wirken rustikal biet...

rails + carrierwavve + plupload = multiple file upload

Right now I am very happy! I finished my work on creating a file upload method in rails for more than one file, altogether handled by rails sessions and as objects. First I had to learn a bit more about how rails works and I achieved that by reading a lot: books, screencasts and blogs. In this blog post I like to share what I accomplished, but without the hussle of putting code blocks in here, so watch out for bold formatted text as that is the code. My goal was to have one site with just the pure option of uploading files. All file handling would be done behind the scenes by myself logging onto my server. That said you are not going to see any fancy CRUD here. Lets start then! And dont forget to versioning your app BEFORE you try something new! [e.g. git] > rails g scaffold Photo title:string Now add carrierwave to you Gemfile and run > bundle install >rails g uploader Photo Alltogether you have model, controller, views and an uploader folder with the Ph...

Serienbrief für Einladungen mit LaTeX bzw. XeTeX

Bei uns im Hause wird gerade eingeladen, also wollte ich meine Kenntnisse in LaTeX nutzen, um dies "schön" zu machen. Dabei sollte man dann einfach anstatt LaTeX XeTeX nehmen, welches durch fontenc sämtliche Schriften des laufenden Systems verwenden kann. Und wenn man dann die Leute eingetragen hat in eine solche Tabelle: Hans;Wurst;0 Klaus;Müller;1 Maria;Müller;0 Peter;Huber;2 Katrin;Ingens;0 Dann kreiert folgender Code drei Querseiten a la: -------------------------------------------------------------- |                            |                               | |     Namen            |     ...