Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zachleigh's avatar

Auto deploy with git

Id love to be able to auto deploy from my git production branch so that when I push to production from my home machine, it automatically makes its way onto my server. Whats the best way to do this?

0 likes
12 replies
matrixdevuk's avatar

Have a cron with this:

*/5 * * * * cd /path/to/web/root; git pull; composer install;

This will pull every 5 minutes, feel free to change 5 to anything you want.

phildawson's avatar

Something like Capistrano is worth looking at if you want a script that can ssh on then update.

If you store your code on Github / Bitbucket then they have easy hooks where you can add a url to your server https://yourserver.com/push?token=secret. Then your server can run your own script to update when that url is called. If you store your .git locally then you can make this url call to your server from your .git/hooks folder.

Then the server can run something like @matrixdevuk mentioned above:

cd /path/to/web/root
git pull origin master
composer install --no-interaction --no-dev --prefer-dist
php artisan migrate --force

If you have forge/envoyer theres a quick deploy button which does it for you. :)

1 like
bashy's avatar

Webhooks are preferred for this type of stuff. I know most git repo providers supply it.

1 like
zachleigh's avatar

Ive been trying to set up something like what @phildawson suggested but am having a hard time getting my ssh keys set up. I have an ssh key in my home folder on my server, but cant get it to work in the web root. I feel like Im missing something really obvious...

martinbean's avatar

@zachleigh Depending on where your remote repository is hosted, I imagine you’d be able to set up a post-push web hook that hits a URL, which in turn calls a deployment script if the branch pushed to was the master branch.

1 like
mikevrind's avatar

I also would suggest to use a webhook and not a cronjob (checking for updates every 5 minutes seems a bit hardcore). I'm using the Remote package which is now being maintained by the Laravel collective ( http://laravelcollective.com/docs/5.0/ssh ) to run different tasks (git pull, composer install, php artisan migrate) when a webhook is triggered.

This route is only accessible from a private Gitlab server and validates the post request.

1 like
zachleigh's avatar

Thanks for the help everyone. Im almost there. The only problem now is ssh keys. I cant get them working in my web root so pulling the git repo in requires a password, which obviously isnt going to work. How can I access the ssh keys in my home folder in my web root? Is this bad practice?

fideloper's avatar

@zachleigh So, assuming running git pull origin master isn't working - that will attempt to use whatever SSH keys are in the home directory of the user running that command. (~/.ssh/). It sounds likely that :

  1. Your git remote (master) is set to use an HTTP/S url instead of a git:// address, in which case it always falls back to a user/pass authentication. From within the server, run git remote -v to check it out.
  2. You haven't setup an SSH key in your server - this involves pasting a ssh public key from your servers ~/.ssh directory into a Github repository so that the server has access to the git repository. The process of creating an SSH key and getting the public key to another server (or Github in this case) is in my free SFH video on "logging in with ssh".

Hope that helps

1 like
zachleigh's avatar

@fideloper Thanks. My git master was indeed set to https instead of ssh. Everything is working fine now. Thanks everyone for your help!

1 like

Please or to participate in this conversation.