Note: You have to at least know git or have created your first
heroku app (which is easy to do), in order to fully get the context here. Ideally, you at least have used something like openshift or heroku.
My first time playing with heroku was very cool, but mystifying - it wasn't clear how or why it was that I needed to run "git init", and why I was "pushing" code to heroku. As a java developer, I'm used to setting up a tomcat server, dropping a jar in some random folder, and then praying to see something on localhost:8080. Heroku is much more elegant - it uses the same tested model for commiting code (git), as it does for committing deployments !
At first this seems confusing, because we associate git only as a tool for managing source code (some of us only use git in conjunction with github, and have never fathomed the idea that we can use git to send runnable code to the cloud).
So, I was forced to decouple my notion of "git" as a version control tool from "git" as a generic source code commisioning utility, when I decided I wanted to experiement with deploying a blog (using the excellent 5-minute toto blogging tool : http://www.rubyinside.com/deploy-blog-with-toto-and-heroku-2962.html), that others could help and collaborate on.
I quickly found that I didn't fully understand heroku's model - i.e. what the spirit behind "git push heroku master" as the deployment was.
At first this seems confusing, because we associate git only as a tool for managing source code (some of us only use git in conjunction with github, and have never fathomed the idea that we can use git to send runnable code to the cloud).
So, I was forced to decouple my notion of "git" as a version control tool from "git" as a generic source code commisioning utility, when I decided I wanted to experiement with deploying a blog (using the excellent 5-minute toto blogging tool : http://www.rubyinside.com/deploy-blog-with-toto-and-heroku-2962.html), that others could help and collaborate on.
I quickly found that I didn't fully understand heroku's model - i.e. what the spirit behind "git push heroku master" as the deployment was.
Here is a visual explanation of the way it all works :
- The commands are labelled edges.
- The arrows represent the flow of information.
The mystery (to me) about this all was simply that git doesn't care how many repos you associate it with... So you can push to github and heroku from the same repo. Each repo is independent.
So : A web app that is deployed on heroku, but can use github for source control -and might be created and maintained in a way that looks like this - where the "circles" represent machines, and the "arrows" represent the flow of information:
Must-knows for using github, git, and heroku together:
1) Everyone knows this already, but just to be complete : "heroku create" is the normal "first step". You create an application. This registers a new app with heroku.
Heroku returns back an application URL to your command line prompt. If you already "have" an application, this step is unnecessary. See the next step to understand why.
Heroku uses git for code deployment. So... what if your code is already on git ?
3) Git lets you have multiple repositories ! The "git push" command actually can take 2 arguments - a repo, and a branch. Thus, when we deploy
git push heroku master
We are actually pushing the master branch to heroku. We can also push to git using :
git push origin master, if the origin of source is from git.
The punchline : You can easily reproduce your environment for pushing code to heroku, and pulling it from github, on multiple or any machines as follows -
1) use heroku to add your public keys :
jaylinux@ubuntu:~$ heroku keys:add
Found the following SSH public keys:
1) github_rsa.pub
2) id_rsa.pub
Which would you like to use with your Heroku account? 2
2) git clone your project from the git repo.
3) git add your (already existing) heroku app to your git configuration as a 2nd remote repository :
git add remote heroku git@heroku.com:fierce-samurai-6972.git
4) Make some changes, push them to heroku :
git push heroku master
5) Don't forget to sync your changes with github so you don't lose your work on github !
git push (or 'git push origin master', or 'git push github master')
Some helpful commands
To see all the apps you've created :
jaylinux@ubuntu:~/Development/$ heroku apps
quiet-warrior-557fierce-samurai-6972
gentle-ice-2166
falling-autumn-9592
simple-rain-5194
To see all the details for an app you're working on :
=== fierce-samurai-6972
Addons: Shared Database 5MB
Collaborators: mfenwick100@gmail.com
sesanker0@gmail.com
Domain Name: fierce-samurai-6972.heroku.com
Dynos: 1
Git URL: git@heroku.com:fierce-samurai-6972.git
Owner: jayunit100@gmail.com
Repo Size: 500k
Slug Size: 1M
Stack: bamboo-mri-1.9.2
Web URL: http://fierce-samurai-6972.heroku.com/
Workers: 0
GraphViz
FYI i used the following to create the graphviz...
digraph x
{
github -> your_laptop [label="git clone git@github.com:jayunit100/mynewherokusite.git"];
your_laptop -> your_laptop [label="git add remote heroku git@heroku.com:fierce-samurai-6972.git " ];
your_laptop -> heroku_server [label="git push heroku master" ];
"fierce-samurai-6972" -> your_laptop [label="heroku create"]
your_laptop -> github [label="git push origin matser"]
}
Real nice post. I also didn't know that you could have multiple remotes.
ReplyDelete✓ Good stuff, thanks for writing this out.
ReplyDeleteGreat Article, Jay. It shows in a nice visual way, how the different entities work.
ReplyDeleteI have one query though. When we push to heroku, I see a lot of activity happening. It not only pushes to the git server on heroku, but it also runs bundle install on the applications ,and sets up everyting nicely on the server.
How does this happen?
Another thing which is still not clear is this. I assumed that when I do a git push heroku master, my code gets pushed to my heroku git repository, but it is also getting pushed to the dyno. How does this magic happen? Would the dyno be using some shared directory which points to the git repository? I did a "heroku run bash" command and it opened up a nice terminal where I could type an 'ls' command. I expected to see a .git directory here, but it wasnt there.
So to summarize, how does the code get deployed on the dyno when I do a git push to heroku?
Any help would be appreciated. This is driving me crazy :)
Hmmmmmm wow i never tried to dive that deep into the internals of how code moves around in the repos.
ReplyDeleteI wonder if this is even public information ?
git add remote heroku git@heroku.com:fierce-samurai-6972.git
ReplyDeleteshould be
git remote add heroku git@heroku.com:fierce-samurai-6972.git
I make that mistake all the time.