1

I need to use jenkins job to synchronize two repositories. So I have created a job, added two git repositories using Multiple SCMs plugin.

And added shell commands, example:

cd first_project
sha=`cat last_commit_merged`
cd ..
cd second_project
new_sha=`git rev-parse HEAD`
git diff-index "$sha" --binary > my.patch
cd ..
cd first_project
if [ "$sha" != "$new_sha" ]
then
  git apply ../second_project/my.patch
  echo "$new_sha" > last_commit_merged
  git add .
  git commit -m "New sync, date `date +'%Y-%m-%d %H:%M:%S'`";
  git push origin HEAD
fi

So when job starts, it successfully clones both repositories in folders: first_project and second_project, I could create my.patch, apply it and commit, but command: git push origin HEAD does not work and I could not figure out how to setup correctly git in shell to make it working, the error is:

+ git push origin HEAD
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Build step 'Execute shell' marked build as failure
Finished: FAILURE

If anyone could help that would be much appreciated

Thanks

  • 1
    i think the message is pretty clear `Permission denied (publickey)`. git does not find the correct public key. – Jens Mar 29 '17 at 08:56
  • consider reading this http://stackoverflow.com/questions/2643502/git-permission-denied-publickey – Disc-Addict Mar 29 '17 at 08:58
  • @Jens, yes, but it is not clear to me how to configure git in shell for Jenkins job. git clone works when Job starts, but then in the shell you could not use git commands like clone, pull, push. – Alexey Lukin Mar 29 '17 at 09:01

1 Answers1

1

The solution is to create id_rsa file manually in shell like this:

cat <<EOF >/root/.ssh/id_rsa
-----BEGIN RSA PRIVATE KEY-----
content of the file
-----END RSA PRIVATE KEY-----
EOF
chmod 400 ~/.ssh/id_rsa