2

I apologise if any part of this is confusing; it's currently 11:21PM, and I've been trying to get this working since 1PM.

I'm cloning a private Github repository to my working directory using git clone git@github.com:username/repo.git /var/www. This works perfectly! No troubles.

I have setup a webhook on Github's side to notify my server of any new push events. This webhook is supposed to pull from the Github repo and update any modified files. It is not doing this.

Every time I call the webhook and run the commands I'm about to show you, it responds indicating that it is "up to date" with the "latest version", however it's indicating that the latest version is the version that it was initially cloned with.

I have run through every solution I can find, and there doesn't seem to be anything that works for my particular issue. My PHP webhook presently sports the following (as it's where I gave up):

git reset --hard HEAD
git pull
git status
git submodule sync
git submodule update
git submodule status

This was supposed to be a method to update multiple servers for me at once.. a way to connect everything. At the moment, the only thing that this whole thing is connecting, is my keyboard with my forehead.

Any help is greatly appreciated.

CheapApples12
  • 97
  • 1
  • 8
  • You should have a look at the answer [here](https://stackoverflow.com/a/9530204/5784831): `git reset --hard` is dangerous. I am not sure, but this may cause the behavior(?) – Christoph Mar 05 '18 at 16:43
  • I can confidently guarantee you that I have read every article you could possibly refer me to. I’m well aware of the implications of using that particular command, and I can assure you that it is not the culperate. Thank you for replying, nonetheless – CheapApples12 Mar 05 '18 at 17:09

2 Answers2

2

This was an SSH error. For anyone with a similar issue, please ensure that Git is looking for your id_rsa in the correct place :)

CheapApples12
  • 97
  • 1
  • 8
1

(I'm not entirely certain if I correctly diagnosed the issue you face, so this is a stab in the dark.)

You are using git over ssh to login to github (git@github.com:username/repo.git).
If you are doing this from a script which runs from a different user shell (e.g. webserver user), said script may not have access to your SSH-Agent, thus can't acces your private key and will fail the SSH login.
You will need to tell git how to find the SSH Agent which has your Github private key loaded and unlocked, using the environment variables SSH_AUTH_SOCK and SSH_AGENT_PID.

Note: If you want to "update multiple servers at once", meaning you want a server-side git repo mirror, consider using git mirror instead. This will not waste space for a work-dir checkout and automatically mirror all the remote refs, including (e.g.) github merge requests; though it will not see/care about the submodules (you'll have to manually create mirrors for them alongside the main repo).
If you want to modify the files and push them back, you'll probably need the workdir(s) of course.


Example Cron script to remote-update local git mirrors over ssh transport; using the keychain tool for ssh agent management.
(You'll have to adapt this concept to fit your php script, obviously.)

$ crontab -e
# m  h   dom mon dow   command
0    23   *   *   *    cd /my/mirrors/ && sh update-mirrors.sh

And the script contents:

$ cat /my/mirrors/update-mirrors.sh
#!/bin/sh

# Git mirrors are created with `git clone --mirror ...`
# to "convert" existing local repo into mirror, use
# $ git clone --mirror --no-hardlinks <local-git-path> <new-mirror.git>
# then update the origin remote to the upstream repo and run
# `git remote update`

# Crontab script needs ssh agent
# This sources a file generated by `keychain' with the following info:
#
#SSH_AUTH_SOCK=/tmp/ssh-TLf5KUaqLQgk/agent.15740; export SSH_AUTH_SOCK;
#SSH_AGENT_PID=15731; export SSH_AGENT_PID;
#
# You may do this some other way
#
. $HOME/.keychain/`/bin/hostname`-sh

d=`pwd`
cd $d

# Now update mirrors (our convention: end dirnames with '.git')
DIRS=`ls -dR *.git`

for f in $DIRS; do
        echo "$f"
        cd "$d"
        if [ -f "$f/HEAD" ]; then
                 cd "$f"
                 echo "* updating bare/mirror repo $f ..."
                 git remote update --prune
        elif [ -d "$f/.git" ]; then
                cd "$f"
                echo "updating clone $f ..."
                git remote update
                #git pull
        fi
done
nyov
  • 1,382
  • 7
  • 23
  • This answer is not applicable for this particular issue. It _was_ an SSH issue, just not of the degree in which you described. Thank you for replying, nonetheless. – CheapApples12 Mar 06 '18 at 13:26
  • Sorry for that then. But glad you found the problem. – nyov Mar 07 '18 at 15:52
  • Nothing to be sorry about, @nyov! We tried something, it didn't work, so we moved on! Thank you again for replying! :) – CheapApples12 Mar 08 '18 at 04:27