Rails keeping git versions correctly - ruby-on-rails

I hope this is an ok question for StackOverflow. If it isn't, let me know. Thanks!!
I have my first Rails app about to go production. I'm using GIT and Github for version control. And I'm using CAP to deploy to our own servers.
We have one server for Staging and one for Production.
So, lets say it's day one of production. And I'm also coding new features on my iMac. So, I'm making changes and saving to GIT, Github and staging.
But, then the users run into a small error that I need to make a quick fix on production.
Well, I've already started making major changes to the code and I don't want to put that into production.
How would I make the quick fix the users need?
Thanks for the help!

Ill assume you have a prod branch and a devel branch, and the tip of prod is what has been released to prod.
You can;
1. git stash all your current work if you havent committed it yet.
2. git checkout prod
3. Create a new branch for your hot fix, fix it, merge it back to prod and release it.
4. git checkout devel and git stash apply your stash if you need to.
5. git merge prod into your devel branch so it has the hot fix you just deployed.
If you dont have separate branches for prod and devel, now might be a good time to set them up :)
See the section 6.3 Stashing of the progit book.
http://git-scm.com/book

You may create a branch for fixies, fix a bug there, and update your application on production server with this branch. In some moment you will merge them (branches).

Related

rails server not serving git files of current branch

I setup a rails project and started working in a specific branch (branch1). I was doing ok with rails s until I wanted to setup another branch (branch2) with different files. I did git fetch origin branch2 for getting the second branch, and was doing fine configuring this branch (bundle, rake db:setup, etc...) up until running rails s. When I start my server, it should serve the files of my current checkout branch (branch2) but it serves the files of the previous branch I was working on (branch1).
Things I tried:
Thought git fetch won't actually get the remote branch to my workspace for the server to load up the correct files. So I deleted the fetched branch and did git checkout -b branch2 origin/branch2. Server still loads files in branch1.
Did spring stop so the server don't load up files stored in the cache (I was told this... I don't really know how spring works). Still, server loads files in branch1
I cloned the repository again, and set up everything for branch2. But still rails s manages to load branch1 (I would say that branch1 is the default when you clone the repo)
What I expect:
Doing rails s in checkout branch1 should load and display app in branch1.
Doing rails s in checkout branch2 should load and display app in branch2.
Any help would be really really REALLY appreciated, I've been stuck with this for hours now... thanks in advance.
P.S. The branches share the same database, I don't if that would help but I thought it was worth mentioning
Thanks for the help on the comments, unfortunately those answers didn't work for me.
Turns out, the server was loading the app correctly in localhost but because the navigation menu on the project is too big, it was saved in the cache to optimize the load in every branch. So I needed to run a script for changing the navigation menu (the script was provided to me by my colleagues that were in the project long before me).
So if you have this problem, ask your colleagues, contributors, boss if there is something you are missing.

Why does my Capistrano Deployment point to an old Commit?

I finalized my last work on a git branch and merged it to the branch for my staging environment. But after my Capistrano Deployment Script finished i rubbed my eyes, because although my changes are pushed to Github my last Commit was completely ignored. They point is that even the remote branch points to the real last commit, but that is not recognised by capistrano for some reasons. Any Ideas?
Capistrano 3 maintains a mirror of your Git repo on the server. It is stored alongside the current, releases, and shared directories in a directory called repo. Usually that means it is here:
/var/www/my_app_name/repo
If you are absolutely sure that the remote is up to date and yet Capistrano is not seeing it, then perhaps there is something wrong with this cached repo.
Try deleting the repo directory (or move it to a backup location) and deploying again. Capistrano will recreate it and hopefully pick up your new commits.
I found the Problem. There was a bad entry in the hosts file which leads to that strange behavior. Thank you for your efforts.

Capistrano: How can I leave a git repository on live/deployed

Ok, Using capistrano 3.2 with Rails 4.2.
Put simply I want to have each release on the live server to have an intact git repo with it. I know Cap uses git to clone the files but afik it deletes the .git folder by default. I swear I had this working before on earlier versions of Capistrano but no amount of Docs or Googling is finding the right setting. Or if I had an odd version of Cap
And before I get jumped on with "Version control on live? Never make changes to live, develop on your dev server you idiot!?"
Having an active git repo on live is invaluable if something changes out of your control, or if there is an emergency you have no choice but to monkey patch. Because now you have the change shown up by git and you can commit it, and push it back into your central repo and have it go up stream neatly. Its saved my ass before and means I don't have to copy by hand what I know has fixed the "live" issue.
Anyway, justification over. Anyone know how?
I apologies for the simple question, I think it's unlucky google fu which has left me without an answer from searching for this. Searching "Capistrano leave git on live" or other such terms are swamped with using git to deploy.
Cheer in advance.
This would be non-trivial. Capistrano uses git archive piped through tar to create the release folder. Hypothetically, you could override the task which does this, but you would probably spend more effort than it would be worth. I would highly recommend that you look at just creating a workflow where you commit a hotfix and push again. We use a prod branch at which the production deploy points, thus you could commit the change to the prod branch and cap production deploy, then merge your change back down to your development branch.
If you do choose to try and override this, look at the Capistrano source for the git tasks. It uses the Git Strategy class, so you would need to subclass and override it, then override the task to use your class. Capistrano is basically just a subclass of Rake, so look for documentation around overriding a Rake task, e.g. Overriding rails' default rake tasks.
Good luck!

Capistrano: save release

I frequently make very small releases to my production server, but often there are some milestones.
I haven't tried anything in particular, I usually just have to revert on my local machine manually and push a new update.
Is there a good way to save a particular release that I could revert to by saying something like "cap revert production -v '1.0'"?
Maybe there's some underlying git understanding I need?
Please advise!
If not, it'd sure be a nice feature...or maybe I just need to improve my development deployment knowledge!
Create git tags for your releases
git tag v1.0
git push --tags
It is then trivial to redeploy any tag. You might also create branches: a common strategy after you deployed version 1 would be to have main development happen on master, and a 1.x branch where you can backport fixes to (and then create tags 1.0.1, 1.0.2 etc. from that b
With capistrano 3 you then just need to do
cap -S branch=v1.0 deploy
Although the setting is called branch it can be a branch, a tag, a sha etc.
With capistrano 2 it's basically the same. Stick
set :branch, ENV['BRANCH'] || 'master'
in deploy.rb and we then do
cap deploy BRANCH=v1.0

Understanding GitHub work flow; what to do after cloning from github?

I am working with RoR, and have recently cloned a project via GitHub. I have a specific RVM gemset for this project, but nothing inside of it. I am not sure if I should create a new branch and then run bundle install, or vice versa. My concern is I do not want to work on the master branch, but I know I need to set up everything first run bundle install.
What is the right method to get started on this project, with out breaking the master?
GitHub is just a hosting for git repositories. Well, writing just is not giving it proper justice.
However you should learn how truly git works. I heartily recommend reading free Git Book.
In short:
git is distributed
you're working only on local working copy
if you don't tell git specifically you want a file to be tracked - it won't (surprise, surprise!).
So running bundle install will not modify what is tracked by git and - specifically - won't affect remote master. It won't modify your master until you modify some files being already tracked. I think bundle install does not. And even then to modify remote master you need to commit and push changes.
You can work on your master. It's safe.
If its someone else's project, or an open-source project, you should Fork the project on Github. Then clone the fork. This will allow you to work on the project, commit your changes locally and push them back to the forked repository.
If it is your own project, and you do indeed want to work in a branch, then clone the repo and make a branch git checkout -b my_branchname. You will then work in the branch, check your code into the branch. Eventually you will want to merge this branch into the master branch. To do this you can merge locally:
git checkout master
git merge my_branchname
Or, you can create a pull request on Github and then merge the branch into master through the Github web UI.
The bundle command, used in conjunction with RVM will just install gems locally, most likely in your home directory (~/.rvm). Gems are not packaged with the rest of the code, so you don't need to fear messing up the master repo.

Resources