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! =)
Yes, you can create scripts that work after some Git action using Git Hooks. You can automate many things with these tools! Take a look at this ebook in English. This other link also has a tutorial in English.
– Erick Mendonça
This is done using the hook
post-receive
, in that script the commands ofdeploy
. Related answer: http://answall.com/a/63874/13561– Pedro Sanção