Files in gitignore - ruby-on-rails

I have a repository with a rails app in the production Server.
In the repo there is the .gitignore file:
...
config/database.yaml
...
Every developing client have a cloned repo with different config/database.yaml.
My problem is this:
database.yaml is in .gitignore, then when I run git clone (on the server) the database.yaml will not be created, but I need it.
I thought than I am doing wrong something.
Can you see where is my mistake ?
thank you,
Alessandro

In my projects, I usually make a copy of a usable database config in a database.example.yml, then when someone clones the projects, copy the database.example.yml to database.yml and make the needed changes.

(here is a copy of my original answer to your first question)
You don't version the right file.
You should version:
a database.yaml.template
2 files (one for server values, and one for generic client values)
1 script able to build database.yaml on the checkout
That script would be called by the smudge step of a filter driver.
The resulting database.yaml would be "private" (never versioned/pushed or pulled), and can be modified at will.

keep a template like database.example.yml as robertokl suggests
when you deploy your application to production server you should be using something like capistrano and part of your capistrano recipe(script) will be to rename that file or to place the correct database.yml file in the config dir

All these database.yaml files have something in common: you can commit a base configuration, and let all your developing clients maintain a fork of it in their own branches with appropriate settings filled in; they can rebase on top of the base configuration in the master branch whenever it gets updated.

aleds - it's generally a bad idea to have your database.yml in a repository which is why you see it in .gitignore. robertokl is right about providing a template file.

Related

Rails: Capistrano change database.yml to database.yml.example causes error

When I deploy a new app to nginx using Capistrano.
I follow tutorial to do git mv database.yml database.yml.example and git mv secrets.yml secrets.yml.example , then created a new database.yml file on remote server. But now when I want to run app on my local mechine, it shows me an error
No such file - ["config/database.yml"]
Because there is no database.yml on my local repo.
Can I create an new and empty database.yml to fix this?
The guide just tells you that storing database credentials in a repository is bad practice and you shouldn't do it, but it doesn't mean you don't need to have this files at all.You application still needs it, so you definitely need to create it, just don't store it in main repo with code, this security critical information is better to store it elsewhere you decide to keep your authentification data like separate repository for credentials, key-pass storage or whatever place you want for such critical information.
PS Of course, if you just learning since it's not a big deal, you COULD keep your "root-123" credits in repository, but it's better to develop right habit from the beginning or at least get the idea why it should be separated.

Which is the safest way to manage Rails database.yml?

Since database.yml file contains some private information, we shouldn't put it under version control.
I just dig out a bunch of solutions for sync database.yml to my server, such as this, I just want to know which is the safest way? Do I should only use scp to for that when I use Cap3 deploy?
You should just create the database.yml on your server manually when you first deploy, and then use Capistrano's linked_files directive to persist the file across deployments. I think the default capistrano configuration contains an example of using the linked_files directive for your database.yml.
Copy the database.yml during deployment from your development machine to your server to shared/db if it doesn't already exist. After deploy and before assets are compiled copy the database.yml to current/config/database.yml.

Maintain database.yml on server

I have a Rails 4 app version controlled with git.
I would like to have a version of database.yml on my server that never changes. What's the best way to allow me to continue to edit this file locally, without changing the remote database.yml file?
You should include your local version of database.yml in .gitignore, so that it's not in the repository and won't change with subsequent deployments.
For instance, in your application root, create a ".gitignore" file and add the following in:
config/database.yml
You can also hide entire directories:
config/*
Basic shell wildcard syntax will work.
*.sql
Etc.
Take it out of version control, it shouldn't be there anyway.
We don't track our config/database.yml in version control (we do track a sample file so it's easier to get setup on new development machines). Our deployment script symlinks in a custom database.yml that's stored in the application user's home directory with permissions set to "600". This way the app user is the only user that can see the database password, and we don't have to do anything manual on deploy.
move database.yml to some secure folder, like /etc/config/database.yml and then create symlink
of that file with rails database.yml after deployment but before starting server.

Fixing github rails deployment

I'm just getting to know rails deployment so forgive me if this question sounds silly.
I have (on my local machine) created a rails app, capified it, initiated git, pushed it (origin -> master to a private repo) and then cloned it on a VPS. However, after that I had to modify several files on the server due to the server's peculiarities and now the repos obviously don't match. I want to start working with Capistrano, but as it is, I cannot do anything (I didn't even catch up with the changes on my local machine). So, I've got several questions.
a) What's the best way to go on about this? Can I delete the github repo, then create another one by pushing from the VPS and clone it on my local machine? If so, should I 'degit' (delete the .git folder) the repo on a server first? Or is the best way to copy the app directory to the local machine and then go through all this once again?
b) As far as I'm concerned, the only file that has to be different between server and the development machine is the database.yml file, do I have to add it to .gitignore and if yes, will it be deleted the next time I pull the changes from the master?
c) If (at first) I push it to github from the VPS as origin, will I be able to change the role afterwards, for it to change every time I push changes to the master from my local machine?
d) Does it even make sense to use Capistrano if there's another way to pull the changes automatically (I've heard some people are somehow using commit hooks)? Because at this point in time I just want to keep the app folder on the VPS up-to-date with what's on github and the capfile and deploy.rb seem to be offering far too many options. Not elegant in the slightest bit.
Thank you for your attention, have a nice day.
Sincerely yours,
Eugene.
a), can't you get the combined diff as a diff file and apply to the local code? Or just commit from there? Then, you will have the changes in the github repo. (ignore the changes that expose private information, use git add -p on the critical files)
for b), the database.yml file is already configured to have different conviguration per environment (dev/production). If you need something more, you can read environment variables (setting them in the production machine) using ERB:
Failing to access environment variables within `database.yml` file
On the other side, if you add the database.yml to the ignored files, you'll need some other way to generate it.
c) does not make sense for me. The VPS's repo will have a default remote configured, and your local repo too, you can push and pull and fetch from wherever you want.
d) capistrano is good enough, keep in mind to not overload the configuration with lots of hooks etc, it may become difficult to manage. Deploying through capistrano is a matter of seconds, I wouldn't worry about it.

first time pushing to github

I have a rails 3 application in local. I created reposority and i know the basics of git like git. I am using git for heroku but i haven't push my application to public place before. What i want to learn is :
Which files should i add to git ignore? Because i have some personal passwords and keys in environments.rb and also have some keys in initializers.
If i add some files in git ignore, i guess it will be a problem for people who clones it, because some files will be missing.
I will also keep on working on my application, i can always add new keys to environment.rb or somewhere else. Do i have to clone(branch) my application? I heard something "branch, master" but i have no idea about these terms?
The idea of gitignore is that often there are temporary files, or files that are specific to your IDE. They add nothing to source code, and sometimes they contain sensible information about your machine that you probably don't want to share.
This repo on github is a nice collection of gitignore templates:
https://github.com/github/gitignore
Rails template is available: https://github.com/github/gitignore/blob/master/Rails.gitignore
EDIT
If you have files with you current configuration, which are important for your app, remove all the sensitive information from them, check them in, and after that modify gitignore file.
If you have already checked in a file with passwords, use this manual: http://help.github.com/removing-sensitive-data/
Create a .gitignore file in your root directory, and add all the files which you do not want to track. Make sure those files are not already tracked. If they are, delete the files, commit the change to the git repository, and then add those files to your .gitignore file.
Your .gitignore file could look something like this
log
db/*.sqlite3
db/*.sql
*.tmproj
tmp
coverage
config/database.yml
*~
\#*\#
.\#*
target/*
**/.DS_Store
.DS_Store

Resources