Skip to content

Instantly share code, notes, and snippets.

@kheengz
Created February 11, 2019 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kheengz/4f90ef6173a262e202bc664b7f0ed8dc to your computer and use it in GitHub Desktop.
Save kheengz/4f90ef6173a262e202bc664b7f0ed8dc to your computer and use it in GitHub Desktop.
How To Set Up Automatic Deployment with Git

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

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

Your server repository: /var/repos/website.git

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

Creating Our Repository

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

cd /var
mkdir repos && cd repos
mkdir website.git && cd website.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
WORKSPACE=/var/www/domain.com

git --work-tree=$WORKSPACE --git-dir=/repos/website.git checkout -f

cd  $WORKSPACE

# Below you can execute other scripts like npm install e.t.c
# echo running npm
# npm install
# echo finished npm install

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 server, just type:

exit

And create your local repo:

cd /User/username/workspace
mkdir project && cd project
git init

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/repos/website.git* [new branch]      master -> master

Here we tell Git to push to the ‘live’ remote on the ‘master’ branch.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment