Using Git with Dandelion for staging and production deployments.
I’ve been recently setting up my local development environment using a combination of a few pieces of software and web services to get this going. In upcoming blog posts I’ll be going over each of these in detail, for now here is my list:
- DesktopServer
Local development server that uses XAMPP and some magic. I’ve heard it described as “awesome” and “awesome in a bottle”, from the same person in the same sentence. - git
The man page describes git as “the stupid content tracker” - Tower
git for people that like a graphical user interface - github.com
the remote repository where git will push my code to - Dandelion
The secret sauce to pushing my ready for staging or ready for production code to the server(s)
Dandelion
So the part I’m going to focus on today is Dandelion which is a script that Scott Nelson on github created and describes as the “Incremental Git repository deployment”. What Dandelion does is something I don’t want to have to manage myself… anymore. You see I use to keep track of what files I changed and then only “deploy” the files I thought I changed up to production or staging if I had a staging server in place. What Dandelion does is it leverages git’s recent commit status to deploy only the changed files up to the webserver.. It’s pretty awesome.
The code is written in Ruby and is pretty simple to install and get running:
- I followed these steps on how to get Ruby and RubyGems installed on my MacBook Pro running Mountain Lion. It requires that you install Xcode and the Command Line Tools to get it going. http://www.zlu.me/blog/2012/02/21/install-native-ruby-gem-in-mountain-lion-preview/
- Once Ruby and RubyGems are installed you need to load up Terminal and run the following:
$ sudo gem install dandelion
- You’ll also need to run the following as well:
$ sudo gem install net-sftp
- From there we can test that dandelion is working:
$ dandelion Invalid command: Usage: dandelion [options]
[] -v, --version Display the current version -h, --help Display this screen --repo=[REPO] Use the given repository --config=[CONFIG] Use the given configuration file Available commands: status deploy - Now that we have Dandelion installed we just need to configure our production server so we can deploy our local get repo and it’s committed changes to production.
- Create a new file in the root of your website directory. Mine live here: /Users/jason/Documents/Websites/ so I go into: /Users/jason/Documents/Websites/www.jasontucker.dev which is the directory that I use with DesktopServer for my jasontucker.blog site and create a new text file in there called dandelion.yml and input the following:
# Required scheme: sftp host: jasontucker.blog username: myusernamehere password: mysuperlongCraz7Pazsw0rDh3re!2938474 # Optional path: jasontucker.blog exclude: - .gitignore - dandelion.yml
- Instead of repeating what the github readme says I’ll referrer you to here: https://github.com/scttnlsn/dandelion#readme
- Optionally you can create additional file for your staging server if you like. If you do name it staging.yml and this one producton.yml and I can show you how to deploy to each by adding in a switch.
Tricking Dandelion into thinking we’ve already deployed (time saver)
Lets assume that recently downloaded a copy of your whole website to your local and haven’t done any changes at all yet. One thing that fustrated me when I used dandelion for the first time was that it wanted me to deploy EVERYTHING since it hasn’t taken note as to what hasn’t been deployed yet. What it does is it uses the SHA hash from git to figure out what it has deployed to the production site. So to get around pushing all those changes to the server I cheated and got the current SHA hash and tricked dandelion into thinking that has already been deployed for the first time. Here is how:
Execute Dandelion from the terminal while your present working directory is your website root folder, mine being: /Users/jason/Documents/Websites/www.jasontucker.dev
$ dandelion status
Connecting to sftp://myusernamehere@jasontucker.blog/jasontucker.blog
Remote revision: ---
Local HEAD revision: 7e8116749a78890e599561b0d674311a512f839a
Notice how it’s reporting there is no remote revision, let’s trick it into thinking the current version is there (which it is for the most part). Create a new file on the remote server called .revision and put in that string of text from the output of ‘dandelion status’ we ran earlier.
7e8116749a78890e599561b0d674311a512f839a
then upload it to the webroot of the remote webserver, in my case it was ‘jasontucker.blog’ Now run dandelion status and you should see Remote revision and Local HEAD the same SHA hash. Now you can run dandelion deploy and it will deploy all of your changed files in that commit. We fancy now huh?
How to deploy to Production and Staging.
Dandelion has a great switch called “config” that allows you to specify which .yml (in our case production.yml and staging.yml) that you want to deploy, it’s syntax is simple, just change the name of the yml you want to deploy to.
$ dandelion –config=staging.yml deploy
$ dandelion –config=production.yml deploy
Let me know how things go in the comments below.