Automatic Deployment with Git to VPS

Catalogue
  1. 1. Server Setup
  2. 2. Creating Our Repository
  3. 3. Hooks
  4. 4. Local Machine

How to set up automatic deployment with git to VPS for my html homepage-

Server Setup

My workspace:

server live directory: /var/www/peiyingchi.com/html

server repository: /var/repo/site.git

Creating Our Repository

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

1
2
3
4
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.

So let’s go to hooks folder:

1
cd hooks

Now, create the file post-receive by typing:

1
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:

1
2
#!/bin/sh
git --work-tree=/var/www/peiyingchi.com/html --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:

1
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/peiyingchi.com/html

Local Machine

The following will need to be done on our local machine (not server).

Here, I have already created a project git repo.
Then we need to configure the remote path of our repository. Tell Git to add a remote called live:

1
2
cd /my/workspace
git remote add live ssh://user@mydomain.com/var/repo/site.git

Then, to push your work to your server, use the following command

1
git push live master

For me, I also have a GitHub repo for my files, it still works the same. This means that if I do git push, the local repo will be pushed to GitHub by default. The 2 remote repos (one at my cloud server, on at GitHub) do not have any connections.