A simple sample to continuous deploy with git hooks

Objective: To create an environment of continuous deploy using the git hooks to developers.

What’s git?

Git is a open source, distributed version control and a management of source code system, focused in productivity. The Git was originally projected and developed by Linus Torvals to develop Kernel Linux, which was accepted by larger part of developers community, that was bored with svn subversion.

The best way of learning git is through its documentation: https://git-scm.com/

If you are not using git, start now.

I prefer to use github.com, because it is wonderful, and used by a larger part of community such as took Phil Sturgeon, in CakeFest2106 in Amsterdam in Building Extraordinary Packages.

The context to this tutorial.

  • Hosting server with git and ssh access;
  • Client terminal;

In the local machine you can create a folder, I used, continuousDelivery.

# To create it in the terminal, type:

$ mkdir continuousDelivery

$ cd continuousDelivery

# to start your local repository, type:

$ git init

# Well done, your git repository is created.

Here you can create your project PHP, NodeJS, …

In this case I used a simple html file.

# Creating the first file

$ touch index.html

# use your prefer text editor to edit this file. I used vim text editor.

$ vim index.html

Inside the index.html put some content. Below my sample.

<head>My first page</head>
<h1>My first file index.html</h1>

If you used vim, type :wq to save and close.

Now you can look your changes. Type git status in your terminal and look that the git showed your untracked files, such as:

Now you need to add your files and to do your commit. Type:

$ git add index.html

If you type git status again, you can see that the state has changed. Like this:

Look that now, you can commit your file, the state is: Changes to be committed.

Then, type: git commit -m “My first commit of index.html”

Ok, our local git repository is updated.Now we can send it to remote repository.

To do it, we need a remote repository. Let’s start.

Go to your hosting server with ssh connection, by terminal, like this:

$ ssh username@yourhost.com.br -p 22

# the -p 22 specify the port to ssh

# type your password.

Usually, you could put it outside your public_html folder. Then, beside of public_html create a folder:

$ mkdir gitLive

# Inside this folder, initialize your remote repository with the command git init — bare:

$ cd gitLive

$ git init — bare

Now if you type ls, in your terminal, you can see some folders, used by git to organize the version control repository.

Like this:

HEAD, config, hooks, objects, branches, description, info, refs

You may think: How can I automate my delivery process with it?

Look folder hooks, inside this folder we have samples of files that will be processed when we do a git push in this repository.

We need to create a file called post-received.

$ touch post-receive

$ vi post-receive

Inside this file we can put the content below:

GIT_WORK_TREE=/path/to/your/public_html git checkout -f

Now you need to give execute permission to this file.

$chmod +x post-receive

Ok, now we conclude our job on the hosting server.

Come back to local repository.

Here we will set our remote repository.

$ git remote add live ssh://username@yourhost.com.br:22/path/to/your/gitLive

In the above command, we said that my repository live will be my remote repository to be delivered.

The :22 specified the port to ssh in your hosting server.

The /path/to/your/gitLive is exactly the folder that we created our remote repository.

Now you can push your repository to your website. When we do it, remember that in our server, the file post-receive will be executed, and this repository will be copied to public_html, and our website is up.

Inside your local folder you can type the command below to push your repository on line.

# pushing repository

$ git push live master

Well done!
 Now, you can always work with git, because it is the safest way to your workflow!

Learn more about git hooks, because it is very easy and very helpful to you.