iEFdev

Code, Computers & Random Junk

Deploy Your Website With Git

A small howto, to use Git to deploy your site, instead of FTP. It’s not that very difficult.

Intro

To set this up, you’ll need to have git installed on both your webhost and your local machine, and SSH access to your webhost.

I’ve seen a few different setups doing simular things, but with this setup we’ll create a bare git repository on the webhost, outside the public folder, and then add a hook to make it update the site.

Below there are some names, like webhost-username, yoursite.dev, etc… Change to match your own setup.


If you need a good webhost I can recommend the one I’m using: Oderland. The site is in Swedish, but should translate fine with Google translate. They accept customers outside Sweden, and if you have any questions - just send them an email and they’ll help you. All servers have git installed, and they also have PHP7. Actually, you can switch/select between PHP7 down to 5.2 - if you want/need (in “cPanel”). Most accounts/plans have a 2 week trial period if you just want to test.

Local setup

Git

Turn you local site/folder into a repository:

git init /srv/www/yoursite.dev/public_html

Add a .gitignore file… I have this for example:

.ht*
_*

blog/
cgi-bin/

*test*.php
info.php
index.html

The blog is Octopress and have its own deployment (using rake, rsync).

SSH

Set up SSH access to your webhost…

Creating the keys:

ssh-keygen -t ed25519 -o -a 100 -f ~/.ssh/id_yoursite_ed25519 -C "$(whoami)@$(hostname)-$(date "+%F")"

Add it:

ssh-add ~/.ssh/id_yoursite_ed25519

# On OS X you use -M to add it to the keychain.
ssh-add -M ~/.ssh/id_yoursite_ed25519

And setup a shortcut in your config file (~/.ssh/config):

Host Foo
    User webhost-username
    Hostname yoursite.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_yoursite_ed25519
    Port 22

And copy the public key to the webhost server:

ssh-copy-id -i ~/.ssh/id_yoursite_ed25519.pub Foo

Remote setup

Log in to your webhost:

ssh Foo

To not mess up the home folder - put the git repository in a “repos” directory. Since git init will create the directories on the fly, just run:

git init --bare repos/yoursite.git

Now, we need to add the “hook” post-receive:

# Using nano
nano repos/yoursite.git/hooks/post-receive

# or with cat
cat > repos/yoursite.git/hooks/post-receive

The script to add is:

1
2
3
4
5
6
7
#!/usr/bin/env sh
#
# Git hook to deploy yoursite.com
#

# path to your public folder on the webhost
git --work-tree=/home/webhost-username/public_html/ checkout -f

Save it and make it run:

chmod +x repos/yoursite.git/hooks/post-receive

Now you can logout…

exit

Usage

Back on the local machine. First thing to do is to add everything.

cd /srv/www/yoursite.dev/public_html
git add .
git commit -m "Initial commit"

Then add the remote… (I call it “live”)

git remote add live ssh://webhost-username@yoursite.com:/home/yoursite.com/repos/yoursite.git

Check your remotes:

git remote -v

And push it. First time you do that it might be good to use the -u handler.

git push -u live master

…and later on:

git push live master

# …or just
git push

It’s a really nice setup to work with. And by using the git repo outside the public folder, there’s no need for xtra stuff in the .htaccess file etc. +1


Happy hacking…

/Eric

Comments