Wednesday, October 10, 2012

Git and Amazon EC2

At Memeja, we're running on Amazon EC2 linux servers.

As we want to launch and iterate quickly, our top concern was committing, merging and deploying quickly.

The optimal setup would be to

  • make a change
  • quickly commit, push to the repo and then push to the server in one go
  • restart the server to apply changes


First, we set up a quick way to ssh into Amazon EC2 in a shell script. Saves repetitive typing and time.

 ssh -v -i ~/key.pem amazon-ip-address

Next, we're going to set up a post-receive hook so that repo pushes also update the server. But to do that with Amazon EC2, we must add provide a valid .pem file. This goes into the shell script every time we go ahead and push.

 ssh-add ~/Dropbox/Memeja/server/memejalaunch.pem  

That does it. Now for the post-receive hook:

We created a new repository on the Amazon EC2 to mirror the local one, changed directory into it and added the post-receive hook.

Then we added a new remote with the ssh and a shell script to automate the committing/pushing:
 mkdir /path/to/memeja  
 cat > hooks/post-receive  
 #!/bin/sh  
 GIT_WORK_TREE=/path/to/memeja git checkout -f  
 chmod +x hooks/post-receive  
 git remote add web ssh://path/to/repo/on/server.git  
 git push web +master:refs/heads/master  

And to automate deployment...
 # !bin/sh  
 cd ~/memeja  
 echo 'ADDING ALL NEW FILES (IF ANY)'  
 git add .  
 echo '...........done.............'  
 echo "Please type commit message"  
 read commit_message  
 git commit -am "$commit_message"  
 echo "Pushing onto repo"  
 git push   
 echo '...........done.............'  
 echo "Pushing onto Amazon EC2 server"  
 git push production  
 echo 'DONT FORGET TO RESTART THE SERVER'  

the $commit_message allows you to enter something on the fly. You can modify the shell script to allow something empty if you want to be really fast.

Then we can ssh and restart the server.

No comments:

Post a Comment