etckeeper pushing to github

I set up etckeeper and added the file /etc/etckeeper/commit.d/60github-push in order to push the commit to github.

[orschiro@thinkpad etc]$ sudo cat /etc/etckeeper/commit.d/60github-push 
#!/bin/sh 
set -e
if [ "$VCS" = git ] && [ -d .git ]; then   
  cd /etc/   
  git push origin master 
fi

However, pushing to github fails as etckeeper tries to push as root. Should the use of sudo not preserve my user account settings for git, including my ~/.ssh keys?

[orschiro@thinkpad etc]$ sudo etckeeper commit "test"
[master de5971c] test
 Author: orschiro <orschiro@thinkpad.(none)>
 3 files changed, 2 insertions(+), 1 deletion(-)
 rename etckeeper/{ => commit.d}/60github-push (100%)
 create mode 100644 test
no such identity: /root/.ssh/id_rsa: No such file or directory
no such identity: /root/.ssh/id_dsa: No such file or directory
no such identity: /root/.ssh/id_ecdsa: No such file or directory
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

To preserve the current ssh keys for when you're in root, use sudo -E. That way there's no need to add anything to the root ssh config


If anyone has an issue with git still trying to use id_rsa instead of the key specified in /root/.ssh/config, here's my fix for it.

The following are my test configuration files before fixing them:

/root/.ssh/config:

Host bitbucket
    HostName bitbucket.org
    User git
    IdentityFile /root/.ssh/bitbucket.pub

[repo]/.git/config:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = [email protected]:trae32566/test.git
    fetch = +refs/heads/*:refs/remotes/origin/*

There are two problems with this:

  1. SSH seems to require you to use the "Host" variable in place of [user]@[address|domain]
  2. The configuration file seems to need the private key.

To fix the first problem I edited line 7 in [repo]/.git/config from:

url = [email protected]:trae32566/test.git

to:

url = bitbucket:trae32566/test.git

To fix the second problem I edited line 4 in /root/.ssh/config from:

IdentityFile /root/.ssh/bitbucket.pub

to:

IdentityFile /root/.ssh/bitbucket

source: http://www.cyberciti.biz/faq/create-ssh-config-file-on-linux-unix/


One thing you can do is specify a key to use for one repo and set it as a remote in your git repository.

Meaning you can put this in root's ~/.ssh/config:

Host gitupstream
        HostName example.org
        User git
        IdentityFile /home/<user>/.ssh/id_rsa.pub

Assuming you git remote add gitupstream [email protected]:/myrepo in this case then do git push origin gitupstream.