2

I have a remote ubuntu server that stores the live version of my code from GitHub. Every time I push from my local computer with git, I want the remote server to automatically pull the code. Other guides usually talk about adding a PHP file to a web directory though my server doesn't have a direct HTTP URL or a www directory since it's not used for a website.

I've also tried creating a post-receive hook but to no result, I don't know if I'm doing it wrong or not.

My post-receive file in .git/hooks:

#!/bin/bash
git --work-tree=/home/folder/anotherFolder/repo --git-dir=/home/folder/anotherFolder/repo checkout -f

I'd like to note that I'm also not the only one that will be pushing to the repository, so it should support commits from different devices.

Skully
  • 2,882
  • 3
  • 20
  • 31
  • Is the remote server running a linux variant? Do you have administrative privileges on it? – Jason Warta Mar 20 '18 at 21:47
  • So you want to push to Github and have this secondary server do a pull automatically? Your options are a hook in Github (probably hard if the secondary server can't be reached via Github), or a cronjob that runs on the secondary server. – Stephen Newell Mar 20 '18 at 21:57
  • The remote server can reach GitHub fine, it's just setting up the hook itself which I'm having trouble with. I don't really want to use a cronjob since it seems inefficient and inaccurate - the pull should occur as soon as the code is pushed. – Skully Mar 20 '18 at 22:02
  • Probably your best bet is going to be checking out the webhooks api: https://developer.github.com/webhooks/ . You can add a webhook to the repo under settings. All you would need for this is some service running on a static port on your server, accessible to the world, listening for that one POST request from the api. – Jason Warta Mar 20 '18 at 22:14

1 Answers1

1

If setting up a webhook listener is not practical for you (like for instance adnanh/webhook), you can check if can push directly from your own local repo to two repos: the GitHub one and the Ubuntu one.
Then and only then your post-receive hook would be activated.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250