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!

Share this post
Facebook Twitter Google
Try AppFog: The new PaaS Hackers love
  • Anonymous

    Please make it simpler. Something like this:
    af push `appname`

  • Luc Perkins

    You can already do that with the af Ruby gem! Check out the docs: docs.appfog.com/getting-started/af-cli.

    This tutorial is intended for people who would prefer to integrate the gem’s functionality into Git.

  • http://www.facebook.com/pawel.neubauer Paweł Neubauer

    Hi Luc,

    At the beginning, thanks for taking care of this problem. I haven’t tested it yet, but I want to ask, what about branches? Whether this solution supports it (so far I noticed that “af update” sends entire work directory to the server)?

    p.s. besides “post-commit” hook is not a good example, because it updates our production on every commit. I think “post-receive” placed in repo directory is better one (this will updates server code on every push, not every commit).

  • Luc Perkins

    As for choosing a post-commit hook, I did that just as an example. You could run hypothetically run the sample script with any of the different Git hooks (though some might not be as useful!). Go ahead and do the exact same thing with a post-receive hook. I just tried it and as far as I can tell it works fine.

    Using branches might be somewhat trickier. At least intuitively, I don’t see a way that that could be implemented for precisely the reason you suggested: “af update” updates the entire directory. But there could be a work-around. I’ll explore and let you know.

    Thanks a lot for your comment, Pawel!

  • seyfin

    It would be great to have a possibility (or a workaround), when using “af update”, to update specific sub-directories/files in the directory, but not the entire directory.

  • http://twitter.com/apathetic012 ‘David Cyrus

    When will you support pushing via git alone? Like what Heroku does.

  • http://www.vinay-sharma.com/ VINAY Kr. SHARMA

    Is there any way to update application directly from git repository server like github.com. If commit was made for master branch. appfog automatically pull last changes from repository server.

    Below af command is crap.

    $ af update

    af update restarts application, but this should not be?

  • http://www.vinay-sharma.com/ VINAY Kr. SHARMA

    AppFog support is very bad.

    Support team replies after a very long time or never replies on some topics.

    I’m worry about my live application running on AppFog. Every time when I executes `$ af update`, this stops my application for some seconds. This is very bad.

    can you add a new option `–no-restart` to af update. When this option is passed to update action application should stage directly without restarting.

Powered by Olark