How to update your app on AppFog with Git
As it stands, if you want to manage your apps on AppFog using the command line, you need to use the af Ruby gem to do so. We do not offer direct support for Git or other version control systems. Fortunately, proper use of Git hooks enables you to integrate usage of the af gem within your Git usage and to thereby speed up your workflow.
How Git hooks work
Once you’ve run git init on a repository, Git creates a hidden .git folder with a hooks subdirectory. If you open up this directory, you’ll see that there are already a number of non-executable sample hook files (update.sample, commit-msg.sample, etc.). If you drop the .sample from one of these, Git will execute it in accordance with other Git commands. For the full list of when each of these commands is executed, I recommend checking out the docs on the official Git page or this githooks manual.
I’m also a big fan of this video instructing you how to use Git hooks to commit code only when certain Mocha tests have passed. This is another phenomenal use case.
Setting up a Git hook in your AppFog project directory
In this brief tutorial, I’m going to set up what’s called a post-commit hook that will run a customized script immediately after a successful git commit.
Let’s say you have an app deployed on AppFog in the directory /my-app and you want to set up a Git hook on that directory. If you’re in a *nix environment, you’ll first want to open up your hooks folder:
cd .git/hooks
Then, you’ll want to replace the sample post-commit hook (post-commit.sample) with an executable file. To do so, just remove the .sample:
mv post-commit.sample post-commit
Now, open up the post-commit file and input the following:
#!/bin/sh af update echo “Success! App has been updated on AppFog” exit 0
The top line indicates that a classic Bourne shell script will be run. If you want to run Ruby, Python, Perl, Bash, Zsh, or other scripts, go for it. Below that, simply specify the commands that you would otherwise run in the command line. In this example, I’ll simply run af update and then have the script notify me in the command line that the af update command has been committed.
Within this script, of course, you could do any number of things beyond just af update. You could run af login followed by af update in case you’re not usually logged into AppFog, or af logs to get your most recent logs, or af tunnel to tunnel into a database associated with your app. And even more fundamentally, you can insert AppFog-related scripts in any of the eight places into the version control process that is supported by Git hooks.
At the bottom, I need to insert exit 0 because if I don’t, then Git may think that the hook was not successful. Making sure to insert exit 0 at the end of the script is especially important if you’re implementing pre-commit hooks, because Git will not actually execute the commit if a script ends with exit 1 or greater.
Please note: the af update command in the Git hook will only work if you have the af gem installed and if your app is already running on AppFog.
But wait: hooks are not just for Git
The beauty of using hooks is that they are supported by all major version control systems. Mercurial supports them, as do Subversion, CVS, and many others. The example I’ve provided here is solely for Git and shouldn’t necessarily be taken as a template for other VCSs, but I highly encourage you to do some experimentation on your own if you use other another system. The solution you need might indeed end up being a lot like the one I’ve provided here.
So go for it. Play around. Use hooks to do all kinds of things with AppFog application management. There are tons of unexplored possibilities that not even we have thought of. And if you have any favorite Git + af use cases (or Subversion or Mercurial or whatever), please share in the comments!
-
Anonymous
-
Luc Perkins
-
http://www.facebook.com/pawel.neubauer Paweł Neubauer
-
Luc Perkins
-
seyfin
-
http://twitter.com/apathetic012 ‘David Cyrus
