Is it possible to deploy a website using git push
? I have a hunch it has something to do with using git hooks to perform a git reset --hard
on the
We use capistrano for managing deploy. We build capistrano to deploy on a staging server, and then running a rsync with all of ours server.
cap deploy
cap deploy:start_rsync (when the staging is ok)
With capistrano, we can made easy rollback in case of bug
cap deploy:rollback
cap deploy:start_rsync
I am using the following solution by toroid.org, which has a simpler hook script.
on the server:
$ mkdir website.git && cd website.git
$ git init --bare
Initialized empty Git repository in /home/ams/website.git/
and install the hook on the server:
$ mkdir /var/www/www.example.org
$ cat > hooks/post-receive
#!/bin/sh
GIT_WORK_TREE=/var/www/www.example.org git checkout -f
GIT_WORK_TREE=/var/www/www git clean -f -d # clean directory from removed files
$ chmod +x hooks/post-receive
on your client:
$ mkdir website && cd website
$ git init
Initialized empty Git repository in /home/ams/website/.git/
$ echo 'Hello, world!' > index.html
$ git add index.html
$ git commit -q -m "The humble beginnings of my web site."
$ git remote add web ssh://server.example.org/home/ams/website.git
$ git push web +master:refs/heads/master
then to publish, just type
$ git push web
There is a full description on the website: http://toroid.org/ams/git-website-howto
After many false starts and dead ends, I'm finally able to deploy website code with just "git push remote" thanks to this article.
The author's post-update script is only one line long and his solution doesn't require .htaccess configuration to hide the Git repo as some others do.
A couple of stumbling blocks if you're deploying this on an Amazon EC2 instance;
1) If you use sudo to create the bare destination repository, you have to change the owner of the repo to ec2-user or the push will fail. (Try "chown ec2-user:ec2-user repo.")
2) The push will fail if you don't pre-configure the location of your amazon-private-key.pem, either in /etc/ssh/ssh_config as an IdentityFile parameter or in ~/.ssh/config using the "[Host] - HostName - IdentityFile - User" layout described here...
...HOWEVER if Host is configured in ~/.ssh/config and different than HostName the Git push will fail. (That's probably a Git bug)
dont install git on a server or copy the .git folder there. to update a server from a git clone you can use following command:
git ls-files -z | rsync --files-from - --copy-links -av0 . user@server.com:/var/www/project
you might have to delete files which got removed from the project.
this copies all the checked in files. rsync uses ssh which is installed on a server anyways.
the less software you have installed on a server the more secure he is and the easier it is to manage it's configuration and document it. there is also no need to keep a complete git clone on the server. it only makes it more complex to secure everything properly.
In our scenario we're storing the code on github/bitbucket and want to deploy to live servers. In this case the following combination works for us (that is a remix of the highly upvoted answers here):
.git
directory to your web servergit remote add live ssh://user@host:port/folder
git config receive.denyCurrentBranch ignore
On remote: nano .git/hooks/post-receive
and add this content:
#!/bin/sh
GIT_WORK_TREE=/var/www/vhosts/example.org git checkout -f
On remote: chmod +x .git/hooks/post-receive
git push live
If your .git
folder is within document root make sure you hide it from the outside by adding to .htaccess
(source):
RedirectMatch 404 /\..*$
Sounds like you should have two copies on your server. A bare copy, that you can push/pull from, which your would push your changes when you're done, and then you would clone this into you web directory and set up a cronjob to update git pull from your web directory every day or so.