I am trying to push a file to GH repo from pipeline and failing at ssh-add $private_key / ssh-add <(cat git-private-key | base64 --decode)
git config --global user.name "CI Bot"
git config --global user.email "localhost"
eval $(ssh-agent -s)
echo -e $private_key | base64 -w0 > git-private-key
ssh-add <(cat git-private-key | base64 --decode)
I tried other solutions and things like eval ssh-agent, update libcrypto, chmod 600 file permission, and passing key in plane and base64 format, but I am getting the same error.
Pls find the below ss trying multiple ways enter image description here
3 Answers 3
I know it's an old question, but it took me quite some time to figure it out, so it might help the one or another.
For me the problem was that I was trying to set-up deployment on staging but I have not yet protected the staging branch the deployment ran on. But the variable with the private key was protected by default, which means, that it won't be exposed to non-protected branches.
So the solution would be to protect your branch to make the ssh key working. Or (not recommended) unprotect your variable.
Comments
After some debugging I have it fixed by correcting the way it is being referenced in ssh-add.
I have used sed to update the $private_key before ssh-add
echo -e $private_key | sed 's/--- /---\n/g;s/ ----/\n----/g' | sed '2s/ /\n/g' > git-private-key
This makes sure that private_key will have a correct format earlier
----beginkey---- KEY SECRET DATA IN MULTIPLE LINES ----endkey----
After sed command
----beginkey----
KEY SECRET DATA
----endkey----
Comments
I faced this error because I had made the private_key variable protected in gitlab. So, on removing the protected tick allowed me to continue
$private_keyenvvar, just put it in a temporary file,ssh-addthat, and go. (Also: please don't screenshot text if you can copy and paste.)plane and base64but both did not solve it. The reason using base64 is got a solution from someone which is a fix for him so tried. For the screenshot I tried to put as much text but picture at some point gives more visibility I'll keep this in mind going forward. - Thanks