Reputation: 393
I need to deploy my rails app on a fresh server and don't really know how to proceed at the moment. I read a lot of guides and tuts but since I don't have a lot of knowledge about deploying in general I would appreciate some direct help.
So what I started to do is this:
Now I know it's possible to push my data from the git repository on the server with "hooks" but I don't know how. Furthermore I am not sure if I need to create a database on my server and how I have to handle the database.yml
(is it enough to set the user and password for the production part?). Last but not least I am not sure if using git is secure or if I need to configure or install anything else to get a basic level of security.
I would appreciate any advice.
EDIT:
Followed this guide to set up capistrano. On step 7 I get following error message:
[6359a9ec] Warning: Permanently added 'github.com,192.30.252.130' (RSA) to the list of known hosts.
[6359a9ec]
[6359a9ec] Error reading response length from authentication socket.
[6359a9ec]
[6359a9ec] Permission denied (publickey).
[6359a9ec]
[6359a9ec] fatal: Could not read from remote repository.
[6359a9ec]
[6359a9ec]
[6359a9ec] Please make sure you have the correct access rights
[6359a9ec]
[6359a9ec] and the repository exists.
[6359a9ec]
Upvotes: 1
Views: 92
Reputation: 888
first, installing/running postgres should not be part of your rails deployment setup. maybe, at some point in the future, postgres won't even run on the same machine as your rails app.
as for your rails question, i can only recommend that you have a look at capistrano, it can do anything you described and is stable, battle-tested and open source. it does a lot less then the TeamCity application described in another answer and therefore, is a lot simpler to setup. once configured the workflow is really simple:
capistrano will then pull the new code, install your dependencies using bundler, run migrations when needed and restart your rails application server. it will also handle database.yml and other secret files that are not in your repo (by symlinking). finally, if you accidentally deployed something bad, you can also do a rollback easily.
tldr: use capistrano.
Upvotes: 1
Reputation: 6404
I would recommend that you look into TeamCity by JetBrains. I know it's another tool that you have to learn, but it will save you time in the long run.
Basically, I think you might be looking to setup a deployment pipeline. So you will need all the dependencies that your application depend on installed on another server somewhere.
For a Rails application, this will all the dependencies for ruby, the bundler and rails gems, a javascript runtime like node, postgres and maybe some other things. I'm going off the top of my head so I'm sure you'll need more stuff installed than just that. I would also recommend using a ruby version management tool like rbenv. Some folks like to automate installing all these dependencies as well, but it might be simpler to just do it by hand for now.
Basically what TeamCity is going to do is allow you to define your repo's url, which could be at Bitbucket or Github, and let you define a series of steps that should be taken to build and deploy your application. The application could be deployed to the same server that you install TeamCity on. I think that would be easiest for you, at least for now.
Whenever I setup a deployment pipeline in TeamCity, the deployment process looks similar to this:
1) I make a change and push my code to the repo. (Bitbucket or Github usually).
2) TeamCity sees that change and pulls down the source code for the Rails app.
3) TeamCity runs bundle install
to get any gems that my Rails app needs.
4) TeamCity runs my tests. If one fails, then it stops the build and sends me an email. (You can configure SMTP settings in TeamCity. I recommend using Mandrill since at this scale it would be free.)
5) After tests run successfully TeamCity then copies the source code out to a deployment directory (something like /var/www/myApp/ usually).
6) Lastly, a command is run to restart whatever server that is being used to serve out your Rails app. It could be Thin, Unicorn or whatever. Sometimes I'll place this behind Nginx and use reverse-proxying to load balance all the Rails app instances.
It will take a little while, but once you get a hang of it every project you do will have some sort of deployment pipeline in place. My first setup took 2 weeks.
Afterwards, it will put a smile on your face when you can just make a change to the code and push it up with git to see it become live on the internet.
EDIT:
And to answer your questions specifically:
Furthermore I am not sure if I need to create a database on my server and how I have to handle the database.yml (is it enough to set the user and password for the production part?)
In the current version of rails I believe that the production database credentials are stored in environment variables on the machine itself. You can store the credentials however you want, as long as they are not publicly accessible. If the source code for your app is private, then you can store then in the .yml file. If not, then either leave the production YAML file out of source control and copy it to the deployment server yourself or use environment variables. If you use TeamCity, then these variables could be set in TeamCity itself.
Last but not least I am not sure if using git is secure or if I need to configure or install anything else to get a basic level of security.
Git is secure, yes. But like many tools it can be used in an insecure way. Always try to use the git, ssh or https protocols when pushing and pulling code and you'll be fine.
EDIT 2:
I wouldn't recommend having a production environment application deployed automatically on code changes. In TeamCity you should have a beta environment deployed automatically and once it is thoroughly tested, then you can hit a button and deploy those changes to production.
Upvotes: 1