whiskey_diskmakes it very easy to quickly deploy a new version of your site to one or more servers. It’s very efficient, as files are copied directly from your git repository to the server. This means you can very quickly make large deployments even when you are on a slow internet connection. You do need a secure way for your server to access the git repository while the deploy is going on, read on to see how this is easily done.
For this example, I’m going to deploy a small static site from a private Github repository to a server via ssh using
whiskey_disk. There are a few steps in getting this all setup, but once you have the ssh infrastructure playing nicely, a complete deployment of a small static site takes on the order of 2-5 seconds. Stay with me, and I’ll show how all the pieces fit together.
To begin with, I’ll assume you have a site or application already pushed to a private Github reposity that you now want to do a deploy of. You also have ssh access to the server you are deploying to for this to work. For this example, I’m deploying the
masterbranch of the following private git repository:
Adding the configuration file
Make sure you have your appropriate code pushed to Github, then we’ll add the whiskey_disk config file
config/deploy.ymlto the project:
This is a simple YAML file (indentation matters), you will need to adjust the domain and paths as appropriate. I use the name
demofor the environment, other common examples would be
production, etc. A lot more options are available, see the official whiskey_disk readme, but this all we need to get started.
Running whiskey_disk setup
whiskey_disk has two commands: a
setupcommand, for doing the initial repository clone, and the
deploycommand used for all subsequent deploys.
config/deploy.ymlfile in place inside the project, we’re going to just try and run the
setupcommand from the local computer, seeing what happens. For now, this errors out:
Okay, we’re not quite ready to do deployments just yet.
Resolving all ssh issues
Github hangs up on us - what’s happening? Testing from my local machine as described in the github ssh guide:
That’s the kind of success we’re looking for on the server as well. Ssh to the server as the deploy user, and try the same command there:
Try the command again, now that the host key is trusted:
Notice how we get a new error later in the connection process, a
Permission denied (publickey)instead of
Host key verification failed.
For the sake of completeness, this is what this situation looks like from whiskey_disk:
ssh-agent to the rescue
The root cause for this difference is Github knows my public key and has access to the corresponding private key, but this key is not available to my server, so it cannot authenticate against Github. The solution is fortunately simple. Do this on your local machine:
This prompts you for the password for your private key, then keeps the private key unlocked in memory for automated access. You need to do this after each time you restart before you can do new deployments, as the unlocked key is only stored in memory. We also need to enable ssh-agent support for the connection to the server - as ssh-agent gives a server the possibility to use our private key without further prompting, you need to trust the administrator of servers you deploy to. With that caveat out of the way, open your
~/.ssh/configfile, and add a config block like this:
Adjust the hostname to taste, obviously. You can also add port-number here, if your admin moved ssh access from the default port. While you have that file open, also check out these extremely handy ssh productivity tips for a lot of extra ssh goodness.
Close the previous connection to your server, open a new one (so ssh-agent can do its magic), then re-run the test:
We have ssh-win!
Time to re-try the deployment
setupfrom the local machine:
This means the repository was successfully cloned into place. We only have to do this once, from now on deployments are just a matter of:
I’m seeing a total time taken for a deployment in the area of 2-5 seconds. Remember to push to Github before running your deployment, as it is from Github your server is pulling versions.
This method for deployment is extremely awesome, and the amount of awesome grows with the size of your deployments, as server-server connections are most likely a lot faster than your own internet connection.
There is a lot more to whiskey_disk that I’ve shown here. In particular I’d like to highlight the “Configuration Repository” section of the official whiskey_disk readme. Editing per-application configuration files on individual servers is effectively a thing of the past.
Years ago I read Streamed Lines: Branching Patterns for Parallel Software Development, and many of the thoughts have stayed me. Think of it as a catalogue of Design Patterns for version control workflows. There are so many ways to efficiently use branches to both collaborate and isolate work. Lively discussions can be had on how to best organize a repository, and having a great overview of the options makes for more informed discussions.
I have previously described how I’ve been using small hack && ship commands to simplify and streamline my personal git workflow. Recently I’ve switched to using the Git Flow setup as my branching and release workflow.
Setting up git-flow
The git-flow project also has instructions for installation on Linux and Windows.
Installing git-flow adds a few helpful commands to the git environment for creating and managing branches for features and releases. A fresh git repository is born with a
masterbranch. In the default git-flow setup, this is where the current production release lives. Furthermore, a branch called
developis created, this is where development takes place. Note that git-flow is just a series of shortcuts to having a development branch and a production branch with sensible ways of shuttling changes back and forth. After installing the git-flow package, configure your local repository for git-flow use with:
You can most likely accept the defaults by pressing enter at each question - this also makes it easier for others to initialize git-flow in their working copy, as all are using the same defaults. The only change from running the
initcommand is two
[gitflow]sections in your
.git/configfile looking like this:
It’s no more magic than that. Consult the built-in help with:
If you’ve got the
bash-completionpackage installed, you can use tab-completion for git-flow commands and arguments. Highly recommended.
Working on a feature
Start work on a feature - say feature 77 from your issue-tracker:
This creates a new regular local git branch called
feature/77-speedup-yak-shavingbased on the current
developand places you on it. Want to share your work in progress with others? Use:
This pushes the branch, and sets up your local branch to track the remote branch in one easy step. The regular
git pull --rebasework as you would expect here. There is nothing special about branches created by git-flow, they are just conveniently named and managed.
All done with a feature? Rebase your feature on the current
developbranch, then merge it in:
With these two steps, you end up on
developwith your feature merged in. Run your test-suite, and push to a central repository as appropriate.
Releasing to production version and hotfixes
Production releases are handled quite nicely in git-flow:
This creates a new branch called
release/2011_year_of_the_yak, based on the current
developbranch. Here you can fix any HISTORY or VERSION file, commit, and the finish up the release with:
By finishing the release, you now get a tag called
2011_year_of_the_yak, and the temporary release-branch is removed. You end up back at the
A hotfix is simply a feature branch which is based on the latest production release, and which is automatically merged to both
master. Extremely handy.
Rebase considered harmful?
Some developers prefer to only ever merge, but I’m a big fan of rebasing local work before publishing. I’ll leave you with this:
If you treat rebasing as a way to clean up and work with commits before you push them, and if you only rebase commits that have never been available publicly, then you’ll be fine. If you rebase commits that have already been pushed publicly, and people may have based work on those commits, then you may be in for some frustrating trouble.
From Pro Git, chapter 3
subscribe via RSS