12

I need during a Gitlab-CI build to authenticate with ssh-agent from an alpine image.

I am looking for a sh one liner equivalent of this bash command (picked from the gitlab documentation):

ssh-add <(echo "$SSH_PRIVATE_KEY")

I have tried :

echo $SSH_PRIVATE_KEY | ssh-add -
Enter passphrase for (stdin): ERROR: Job failed: exit code 1

printf '%s\n' "$SSH_PRIVATE_KEY" | ssh-add
ERROR: Job failed: exit code 1
Dimitri Kopriwa
  • 8,478
  • 12
  • 70
  • 140

1 Answers1

26

You have to quote the variable in your first command:

echo "$SSH_PRIVATE_KEY" | ssh-add -
     ^----------------^

Or specify - as the filename in your second command:

printf '%s\n' "$SSH_PRIVATE_KEY" | ssh-add -
                                      -----^
that other guy
  • 101,688
  • 11
  • 135
  • 166
  • 2
    Perhaps you mean "and", rather than "or"? (The latter isn't a substitute for the former, after all). – Charles Duffy May 26 '17 at 23:54
  • Yes, the correct answer should be `echo "$SSH_PRIVATE_KEY" || ssh-add -` – henryJack Aug 24 '17 at 16:20
  • 5
    I think `|` in this context (bash oneliner) is a pipe, not a logical AND or OR. There's a different issue with that solution. ssh-add will open a dialog and ask for passphrase and wait for input. I've seen solutions to that using expect. – riemann Sep 06 '17 at 11:27
  • 1
    hmm, I am getting `-: No such file or directory` in that example (running inside a docker container). – DragonTux May 21 '18 at 17:33
  • Make sure you start an SSH agent first with `eval $(ssh-agent)`. Other solutions here: https://stackoverflow.com/questions/17846529/could-not-open-a-connection-to-your-authentication-agent – Josh M. Jan 14 '19 at 04:17
  • @JoshM. right - worth mention that this works only on bash - but your link contains other solutions too. – Tobias Gaertner Jan 29 '19 at 08:03