Search code examples
gitgithubssh-agent

mirroring a self-hosted git repository to github.com (auth failures)


I am trying to have my personal server be my primary git remote and automatically mirror that to github. I found this article which gets it mostly working with a post-receive script that does git push --mirror (essentially).

My approach is different in that I would like to avoid having to create and secure a deploy key and then configure it on every repository.

My post-receive script works correctly with most of the variants below as marked in the comments except when I do the full nohup + stdio redirection + backgrounding as in the blog article above, the authentication stops working.

GITHUB_USERNAME=focusaurus
BARE_PATH=$(pwd -P)
REPO_NAME=$(basename "${BARE_PATH}")
REPO_URL="ssh://git@github.com/${GITHUB_USERNAME}/${REPO_NAME}"
echo "About to mirror to ${REPO_URL}"

#hmm, this works
#git push --mirror "${REPO_URL}"

#this works, too
#nohup git push --mirror "${REPO_URL}"

#and this also works OK
nohup git push --mirror "${REPO_URL}" &

#but this fails with
#Permission denied (publickey).
#fatal: The remote end hung up unexpectedly
#Somehow ssh agent forwarding must get screwed up? Help me, Internet.
#nohup git push --mirror "${REPO_URL}" &>>/tmp/mirror_to_github.log &

#this is the one used in the blog post above but it also fails
# nohup git push --mirror "${REPO_URL}" &>/dev/null & 

I have ssh agent forwarding which I believe is how the working versions work. So my question is why do those last 2 variations fail with authentication errors?


Solution

  • Maybe you can try to set the verbose flag on ssh to figure out what is going wrong.

    You can use the GIT_SSH environment variable to substitute the command that git will use to open the ssh connection. From the man page:

       GIT_SSH
           If this environment variable is set then git fetch and git push
           will use this command instead of ssh when they need to connect to a
           remote system. The $GIT_SSH command will be given exactly two
           arguments: the username@host (or just host) from the URL and the
           shell command to execute on that remote system.
    
           To pass options to the program that you want to list in GIT_SSH you
           will need to wrap the program and options into a shell script, then
           set GIT_SSH to refer to the shell script.
    

    So a script in /tmp/verb-ssh that looks like:

    #!/bin/bash
    /usr/bin/ssh -vvv "$@"
    

    and then setting the environment variable GIT_SSH=/tmp/verb-ssh should provide some useful debugging information.