Auto Deploy after Git Push

Asked

Viewed 1,179 times

13

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

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

1 answer

9


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

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

Then you have to have two folders on your server:

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

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 option --bare, so that your repository has no physical files, only 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 the vi, press the key i to edit the file and write the following content:

#!/bin/sh

meuprojeto=/var/www/meuprojeto.com
git_repo=/var/git/meuprojeto.git

# 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 &

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

After leaving vi, grant permission to execute this file:

chmod +x post-receive

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

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


Configuring your local environment:

Now go to your local repository on your machine. Inside your project folder, add your remote repository:

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

Substitute xxx.xxx by the IP or the domain of your server.

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

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

Tips:

1) Working with Several Remote Repositories

You can use the same technique to add several remote repositories. It is good practice for you to have a test, approval 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 remote addresses by default, like this:

git push -u testes_meuprojeto master

and the next time you write git push no arguments, it will rise to your test repository as 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 more deeply, take a look at this site about git Hooks or see the official documentation.

Some examples of what can be done:

  • Check code patterns;
  • E-mail the team notifying the deploy;
  • Run unit tests before running build;
  • A rollback script if there is a problem after the build.

Anyway, I hope I helped! =)

  • Hello, Rafael. Very nice answer. I now understand how the Openshift deployment generation routine is implemented. A single gap in your answer is the question of automatic deploy. By this I mean the process of publishing an application on an application server like Jboss. I think you have confused it with automatic publishing.

  • Ops, I didn’t realize that the question was specific to Java. This first command I passed just copies the files. However, this post-commit file is just a Bash script, you can write arbitrary commands on it, like running a build on ant, for example.

  • You can, for example, set up the post-commit to run unit tests and publish only if it passes. But this is subject to another post. ;)

  • Raphael, I think your answer answers well the question "How to implement a process similar to this one?" and therefore deserves to be approved. In order to be more complete it would be interesting to emphasize, to make more clear, the question of the necessity of the script with the use of the Hooks.

  • Okay, I’ll edit it out.

  • 1

    Done! I’ve added a link to the git Hooks documentation too if you want to dig deeper into the subject.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.