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://[email protected]/${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?
To maintain a mirror of a repository without forking it, you can run a special clone command, then mirror-push to the new repository. Before you can push the original repository to your new copy, or mirror, of the repository, you must create the new repository on GitHub.
Input host keys manually, and enter the host key into SSH host key. When mirroring the repository, GitLab confirms at least one of the stored host keys matches before connecting. This check can protect your mirror from malicious code injections, or your password from being stolen. Select an Authentication method.
In the Project drop-down list, select the Google Cloud project to which the mirrored repository belongs. If you don't have a project, you can click Create project to create a project. In the Git provider drop-down list, select GitHub. Select the checkbox to authorize Cloud Source Repositories to store your credentials.
Don’t use git push --mirror in repositories that weren’t cloned by --mirror as well. It’ll overwrite the remote repository with your local references (and your local branches). This is not what we want. Read the next section to discover what to do in these cases.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With