Automatic Deploy after Git Push


I use OpenShift daily to create and host applications in the clouds. One interesting thing I notice is the deploy process of the applications hosted on this platform. Deploy is preceded by the git push command. A single, simple command, with no complexity.

Is there a hidden script that performs the necessary commands to perform the deploy process or is it a plugin attached to Maven? How to implement a process similar to this?

asked by anonymous 25.08.2015 / 16:45

1 answer


Assuming you have root access to your server, this is quite easy to do:

Basically, you have to create a Hook of type post-receive that will copy all the contents of your push to a directory that you determine and execute your build.

So you have to have two folders on your server:

  • Repository: /var/git/meuprojeto.git
  • Your Project: /var/www/

Creating the Repository:

mkdir /var/git && mkdir /var/git/meuprojeto.git
cd /var/git/meuprojeto.git
git init --bare

You need to start your repository with the --bare option, so that your repository does not have physical files, just version control. That is, it is ONLY a repository.

Git repositories have a hooks folder. You can view it with ls and then enter it to create your post-receive:

cd hooks && vi post-receive

Inside vi, press the i key to edit the file and write the following content:



# Isso vai copiar o conteúdo do push para o seu projeto
git --work-tree=$meuprojeto --git-dir=$git_repo checkout -f

# Esse arquivo é só um bash script. Adicione aqui comandos 
# para executar o seu build, usando as suas ferramentas de
# costume (maven, ant, junit, etc.)

# Exemplo:
cd $meuprojeto
pkill -f meuprojeto-SNAPSHOT
mvn clean package
nohup java -jar target/meuprojeto-SNAPSHOT.jar server meuprojeto.yml > /var/log/java/meuprojeto.log 2>&1 &

Press Esc to exit editing mode and :wq to save the file.

After exiting vi, grant execution permission to this file:

chmod +x post-receive

Ready. This will copy the files and run the build of your project (and most everything you write to run).


NOTE: This above bash is just an example. Should work, but to run in production, I recommend you add more commands in the   script or other hook types (see tips at the end of the answer).

Configuring your local environment:

Now go to your local repository on your machine. Within the folder of your project, add your remote repository:

git remote add meuprojeto ssh://[email protected]/var/git/meuprojeto.git

Replace with the IP or domain of your server.

Ready! You can now push and take advantage of automatic deploy:

git add .
git commit -m "Testando deploy automático"
git push meuprojeto master


1) Working with Several remote repositories

You can use the same technique to add multiple remote repositories. It is good practice to have a test, homologation and production server. So your pushs would look like this:

git push testes_meuprojeto master
git push homologacao_meuprojeto master

You can also configure git to use one of these default remote addresses as follows:

git push -u testes_meuprojeto master

And the next few times you type git push with no arguments, it will rise to your test repository by default.

2) More about git hooks

There are other types of hooks in git besides the post-receive I mentioned above, which can be used for several important tasks. To get to know them further, take a look at this site on git hooks or view the official documentation .

Some examples of what can be done:

  • Check for code patterns;
  • Send e-mail pro team by notifying deploy;
  • Run unit tests before running the build;
  • A rollback script if you encounter any problems after the build.

At last, I hope I have helped! =)

03.09.2015 / 05:14