// Tutorial //

How To Set Up Automatic Deployment with Git with a VPS

Published on August 8, 2013
Default avatar
By Caio Vaccaro
Developer and author at DigitalOcean.
How To Set Up Automatic Deployment with Git with a VPS

Introduction

For an introduction to Git and how to install, please refer to the introduction tutorial.

This article will teach you how to use Git when you want to deploy your application. While there are many ways to use Git to deploy our application, this tutorial will focus on the one that is most straightforward. I assume you already know how to create and use a repository on your local machine. If not, please refer to this tutorial.

When you use Git, the workflow generally is toward version control only. You have a local repository where you work and a remote repository where you keep everything in sync and can work with a team and different machines. But you can also use Git to move your application to production.

Server Setup

Our fictitious workspace:

Your server live directory: /var/www/domain.com

Your server repository: /var/repo/site.git

What should we do if we want to push to site.git and at the same time make all the content available at /var/www/domain.com?

Creating Our Repository

Login to your VPS from command line and type the following:

cd /var
mkdir repo && cd repo
mkdir site.git && cd site.git
git init --bare

--bare means that our folder will have no source files, just the version control.

Hooks

Git repositories have a folder called 'hooks'. This folder contains some sample files for possible actions that you can hook and perform custom actions set by you.

Git documentation define three possible server hooks: 'pre-receive', 'post-receive' and 'update'. 'Pre-receive' is executed as soon as the server receives a 'push', 'update' is similar but it executes once for each branch, and 'post-receive' is executed when a 'push' is completely finished and it's the one we are interested in.

In our repository if you type:

ls

You will see a few files and folders, including the 'hooks' folder. So let's go to 'hooks' folder:

cd hooks

Now, create the file 'post-receive' by typing:

cat > post-receive

When you execute this command, you will have a blank line indicating that everything you type will be saved to this file. So let's type:

#!/bin/sh
git --work-tree=/var/www/domain.com --git-dir=/var/repo/site.git checkout -f

When you finish typing, press 'control-d' to save. In order to execute the file, we need to set the proper permissions using:

chmod +x post-receive

You can see on the documentation that 'git-dir' is the path to the repository. With 'work-tree', you can define a different path to where your files will actually be transferred to.

The 'post-receive' file will be looked into every time a push is completed and it's saying that your files need to be in /var/www/domain.com.

Local Machine

Let's create our local repository. You should change the path and name to whichever you choose. If you are on a VPS, just type:

exit

And create your repo:

cd /my/workspace
mkdir project && cd project
git init

Then we need to configure the remote path of our repository. Tell Git to add a remote called 'live':

git remote add live ssh://user@mydomain.com/var/repo/site.git

Here we should give the repository link and not the live folder.

Let's assume that we have some great work ready in this folder. We should do the usual steps of adding the files and commit with a message:

git add .
git commit -m "My project is ready"

Just to remember, the dot after 'git add' means you are adding all files to stage. After 'git commit' we have '-m' which means we will type a message. To complete, we just 'push' everything to the server. We use the 'live' alias that we used when setting the remote.

git push live master
Counting objects: 7, done.Delta compression using up to 4 threads.Compressing objects: 100% (7/7), done.Writing objects: 100% (7/7), 10.56 KiB, done.Total 7 (delta 0), reused 0 (delta 0)To ssh://user@mydomain.com/var/repo/site.git* [new branch]      master -> master

Here we tell Git to push to the 'live' remote on the 'master' branch. To understand more about branches and how to use it you can read this tutorial.

Beta

What if you don't want to deploy everything in one step? Maybe you want to test it first and have a beta directory.

One of the ways to do that is create another repository. Let's log in again in our VPS and create our directory:

cd /var/www/
mkdir beta

To create our repository:

cd /var/repo
mkdir beta.git && cd beta.git
git init --bare

Again we should create the 'post-receive' file because we want to see our project in the beta directory:

cd hooks
cat > post-receive

Type the content of the file:

#!/bin/sh
git --work-tree=/var/www/beta --git-dir=/var/repo/beta.git checkout -f

When you finish typing, press 'control-d' to save. In order to execute the file, we need to set the proper permissions using:

chmod +x post-receive

Let's go back to our local repository:

exit
cd /my/workspace/project

So now we can set another remote pointing to our beta repository:

git remote add beta ssh://user@mydomain.com/var/repo/beta.git

With this, we can have a two step process. First we push to beta and check, and if everything is fine we push to live:

git add .
git commit -m "New version"
git push beta master

And later:

git push live master

Going Live From the Server

Maybe you have a team working in the same project and you want that others can also decide that it's time to go live. To do this, we can link the beta and live repository on the server. Log in to your VPS and type:

cd /var/repo/beta.git
git remote add live ../site.git

So now you can push from beta to live on the server:

cd /var/repo/beta.git
git push live master

Congratulations! Your VPS is now set to automatically deploy with Git!

Submitted by: Caio Vaccaro

If you’ve enjoyed this tutorial and our broader community, consider checking out our DigitalOcean products which can also help you achieve your development goals.

Learn more here


About the authors
Default avatar
Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?
10 Comments

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

At the step “cat > post-receive” I got “-bash: post-receive: Permission denied” Same with "sudo cat > post-receive. Instead I did “sudo nano post-receive” and typed the next step in. Not surprisingly, when I did “git push live master” locally, I got:

error: insufficient permission for adding an object to repository database ./objects

fatal: failed to write object error: unpack failed: unpack-objects abnormal exit

Suggestions? Thanks.

Received the following error… I have followed every step of the tutorial…

fatal: ‘/var/repo/site.git’ does not appear to be a git repository fatal: Could not read from remote repository.

note that if /var/repo and /var/www/domain.com are owned by root but you’re ssh-ing in as user foo, and sudo requires a password that the git hooks will fail due to lack of permissions. the easy (someone else can decide if this is problematic or not) fix is to simply change the ownership of /var/www and /var/repo to your user and group: sudo chown -R foo:foo /var/www and sudo chown -R foo:foo /var/repo

Solution found, Reference from: http://stackoverflow.com/questions/10391522/git-does-not-appear-to-be-a-git-repository

when adding the git remote. I changed ssh://user@domain/var/repo/site.git to ssh://user@domain/root/var/repo/site.git

It works now.

I am author of two possible alternatives to the same problem.

First one is the open-source incoming webhook server that can execute commands, you can find it at https://github.com/adnanh/webhook and the bitbucket hook example at https://github.com/adnanh/webhook/wiki/Hook-Examples

Second one is Hookdoo and it follows a little bit different approach. However, it is a paid Software-as-a-Service solution, but it requires you to do zero setup on the server. When you register for the first time, you will receive a month of free trial so you can try it out, and we can help you set it up. You can check it out at https://www.hookdoo.com/

Need some help here… after I do the

git push live master

and saw * [new branch] master -> master

I went back to my server via ssh, and nothing happened, none of the files were added. Why?

In case this post-receive hooks don’t work for you :

git --work-tree=/var/www/domain.com --git-dir=/var/repo/site.git checkout -f

** try this :**

GIT_WORK_TREE=/var/www/domain.com git checkout -f

Hi,

Was able to fix the issue with

  • remote: fatal: This operation must be run in a work tree by creating the folder in the /var/www - e.g. mkdir /var/www/domain.com before you can actually do the git --work-tree=~/var/www/domain.com --git-dir=~/var/repo/site.git checkout -f

Hope this helps.

Hello and thanks for this nice tutorial!

I found a problem anyway, when I hit “git push” on my local machine the remote repo is updated correctly, anyway the working tree directory isn’t created and I got this message:

  • remote: fatal: This operation must be run in a work tree

Which for what I know about git is legit. So how did you make it works with bare repos? Am I missing something? Thank you!

@maxcnunes: It should be <code>~/var</code>, I’ve updated the article.