GIT deployments on MediaTemple

GIT deployments are a technique for moving source code from your local development environment to your remote environments while maintaing a complete history of your changes. Many web developers have moved to this style of deployment because it: lowers the risk of forgetting to transfer a file when deploying gives you a way to back-out changes…

Published in

,

git-deployments-mediatempleGIT deployments are a technique for moving source code from your local development environment to your remote environments while maintaing a complete history of your changes. Many web developers have moved to this style of deployment because it:

  • lowers the risk of forgetting to transfer a file when deploying
  • gives you a way to back-out changes if your deployment breaks something
  • makes it easier to collaborate with other developers, or work from multiple computers i.e. your laptop and the desktop at work

Hub and prime: The web-focused GIT workflow

hub-prime GIT deployment workflow that works[/caption]

Joe Maller’s web-focused GIT workflow is the workflow I choose to use. Why?

  • It is relatively simple
  • It can be applied to a live website without any down-time
  • It worked

Please go and read the original article because it provides a great explanation on why he uses two repositories set up the way they are.

By following the instructions in the original article and using MediaTemple’s Knowledge base for MediaTemple specific info I got this workflow working. This is a run down for those wanting to do the same.

Connect via SSH

MediaTemple are kind enough to provide GIT pre-installed on their Grid service, but you will need to enable SSH connections before moving on.

When connecting via SSH, the username is your primary domain. So in my case, that’s prevision.com.au.

The following command connected me via SSH:

ssh prevision.com.au@prevision.com.au

The SSH password is the server administrator password, which is the same password you use to connect via FTP. Not your account centre password.

Create Prime repository

Once connected via SSH, the only changes to Joe Maller’s instructions on setting up the Prime repository is the file path to the public html directory.

On MediaTemple’s Grid the public html directory is: ~/domains/yourdomain.com/html

So the commands to set-up your Prime repository become:

$ cd ~/domains/yourdomain.com/html
$ git init
$ git add .
$ git commit -m"initial import of pre-existing web files"

Create Hub repository outside the public html directory

I created a directory called “repos” at the root level of my server and and created the Hub repositories there.

To do this use the following commands instead of those from Joe Maller’s article

$ cd ~; mkdir repos; cd repos
$ mkdir site_hub.git; cd site_hub.git
$ git --bare init
Initialized empty Git repository in ~/repos/site_hub.git

Add remote to Prime

Due to the MediaTemple specific file paths, the command you’ll need to add a remote repository to your Prime repositories becomes:

$ cd ~/domains/yourdomain.com/html
$ git remote add hub ~/repos/site_hub.git
$ git remote show hub
* remote hub
URL: /domains/repos/site_hub.git
$ git push hub master

Create hooks

Hooks are the linch-pin of GIT deployments and it’s no different with this workflow. Again the only thing needing to change from the original article is the file paths, so the commands become:

#!/bin/sh
echo
echo "**** Pulling changes into Prime [Hub's post-update hook]"
echo
cd ~/domains/yourdomain.com/html || exit
unset GIT_DIR
git pull hub master
git-update-server-info

The post-commit hook on in the Prime repository is the same.

Add Hub as a remote on your local repository

The final step is linking your local repository to the Hub repository by adding it as a remote.

The following commands are on your local computer:

$ mkdir yourdomain
$ git init
$ git remote add hub ssh://yourdomain.com@yourdomain.com/~/repos/site_hub.git
$ git pull hub master

If you already have code in a repository locally you can use the following commands to add hub as a remote, and deal with any merge conflicts which arise:

$ cd /path/to/your/repository
$ git remote add hub ssh://yourdomain.com@yourdomain.com/~/repos/site_hub.git
$ git pull hub master

Pushing changes to Hub will deploy them to your live site

Now when you’re ready to deploy changes to your live site, commit them locally then push to Hub. This will trigger the post-update hook on Prime which pulls the changes to your public html directory making the live.

The commands for this are something like:

$ git add .
$ git commit -m "Your descriptive commit message"
$ git push hub master

Comments

19 responses to “GIT deployments on MediaTemple”

  1. Leian Avatar

    How would you integrate this with a GIT app like SourceTree or Github For Mac?

    1. Andrew Fleming Avatar
      Andrew Fleming

      You will need to set up the Hub and Prime repositories, and their hooks, via SSH from your command line. Once they are in place though, there is nothing to stop you using Source Tree or GitHub For Mac to interact with your local repository and push changes to the remote repository (Hub).

      1. Leian Avatar

        Thank you for your quick response.

        exec git-update-server-info

        I get the following error

        -bash: exec: git-update-server-info: not found

        1. Andrew Fleming Avatar
          Andrew Fleming

          I’ve not run into that error Leian. If you’re pushing to your remote repository via SSH you should be able to remove it from your hooks. That command is needed if you access your repo via http or https. Read this SO article for a bit more detail.

  2. Justin Avatar

    Any ideas how to link a bitbucket repository to this?

    1. Andrew Fleming Avatar

      I haven not written any scripts which link to any hosted repositories like BitBucket, but I have had success using Dploy before. It’s a deployment app for people who want to host their repos on BitBuckt/GitHub. It’s by the guys who built Beanstalk, which is for those who want their repos and deployment all in the same place.

  3. Blake Avatar
    Blake

    I can’t seem to get this to work. All seemed great till I got to the add hub as remote on local repository.

    git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree push -v –tags –set-upstream hub master:master
    Pushing to ssh:mydomain.com@mydomain.com/~/repos/site_hub.git
    ssh: Could not resolve hostname ssh: nodename nor servname provided, or not known
    fatal: Could not read from remote repository.

    Please make sure you have the correct access rights
    and the repository exists.
    Completed with errors, see above

    1. Andrew Fleming Avatar

      See what happens when you use ssh:// (with the double forward slash) rather than what you have.

  4. Brendan Foster Avatar

    Hey Andrew,

    Will this work for multiple sites on the same server?

    Also thanks very much for this walkthrough.

    1. Andrew Fleming Avatar

      I will work for multiple sites on the same server if each site is it’s own GIT repository. You could then simply repeat the steps above for each site.

      I have 10+ sites on a single Media Temple Grid service all set up this way. Works great.

  5. Brandon Avatar

    Great article still getting use 2 years later. This is just what I needed, however I can’t get the hook post-update to work. Once I “git push hub master” the commit writes to the hub repo that works fine, but it sends an error,
    Any ideas? I used your exact code switching the path to the prime repo. My only thought is somehow it can’t find the prime repo to pull from.
    I am kind of a novice so any help is appreciated.

    BTW… When I type a colon in this comment box your site automatically translates it to a single vertically centered dot. For instance above after the ssh that dot was entered as a colon. Not sure if it is your font or if you have a script that renders it that way?

    1. Brandon Avatar

      Sorry for the second comment for some reason part of my comment was left out. The error says:

      error: cannot run hooks/post-update: No such file or directory To ssh://…/~/repos/site_hub.git

    2. Andrew Fleming Avatar

      Thank for the comments Brandon. I would suggest connecting to your server via SSH again and checking what hooks are in your Hub repo.

      cd ~/repos/site_hub.git/hooks

      Git creates sample hooks during git init. Check that the hooks in the directory above have been renamed to remove the .sample suffix.

  6. Michael Avatar
    Michael

    Where do you add the hooks? When you’re SSH’ed into your MT servers or on Github? Sorry I’m new to this, so I’m following the instructions closely – I just don’t know where or how to add these hooks.

    1. Andrew Fleming Avatar

      The hooks are added to the GIT repositories on your MT servers.

      There are 2 hooks in use with this workflow;

      1. post-update hook in the Hub repo ~/domains/repos/site_hub.git
      2. post-commit hook in the Prime repo ~/domains/yourdomain.com/html

      The hooks are all stored in the hooks subdirectory of the Git directory. You can read a full description of GIT hooks here; https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks

      I’ve provided example of how I updated the hooks from Joe Maller’s web-focused GIT workflow (http://joemaller.com/990/a-web-focused-git-workflow/), so the full description of the hooks can be found in that article.

  7. Leo Perkins Avatar
    Leo Perkins

    Thanks Andrew, when I run on Media Temple.

    “exec git-update-server-info”
    I get the following error
    “-bash: exec: git-update-server-info: not found”

    Just running “git update-server-info” seemed to work.

    1. Andrew Fleming Avatar

      Thanks Leo. I’ll update the post.

  8. Drew Avatar
    Drew

    Being new to git, I don’t fully understand how to add the hook commands. Is that something that is done through CLI or should I just add that script to the existing post-update.sample file and delete the .sample?

    1. Andrew Fleming Avatar

      Hey Drew. Yes, you can update the existing post-update.sample file and then delete the .sample suffix to enable it.

Leave a Reply to Leian Cancel reply

Your email address will not be published. Required fields are marked *