its easy. watch this https://www.youtube.com/watch?v=2q8cW9mQtOM . its a 3 part video so make sure to watch all. basically u need to write some simple bash commands in post commit hooks of ur remote repo
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?
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.
@matrixdevuk Thats the amazingly simple answer I was looking for. Thank you.
No problem. :)
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. :)
Webhooks are preferred for this type of stuff. I know most git repo providers supply it.
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...
@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.
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.
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?
@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 :
- Your git remote (
master) is set to use anHTTP/Surl instead of agit://address, in which case it always falls back to a user/pass authentication. From within the server, rungit remote -vto check it out. - You haven't setup an SSH key in your server - this involves pasting a ssh public key from your servers
~/.sshdirectory 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
@fideloper Thanks. My git master was indeed set to https instead of ssh. Everything is working fine now. Thanks everyone for your help!
Please or to participate in this conversation.