'Error loading key "(stdin)": invalid format Gitlab CI

This is piece of my yaml file about ssh for gitlab ci:

eval $(ssh-agent -s)
echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
mkdir -p ~/.ssh
chmod 700 ~/.ssh
'[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

When I attempted to set SSH_PRIVATE_KEY variable to protected state, it occurs error:

Error loading key "(stdin)": invalid format

and I don't know why it's okay when I set SSH_PRIVATE_KEY variable to unprotected state



Solution 1:[1]

For me it also failed because the SSH_PRIVATE_KEY was marked as "Protected" but the tag I was using was not.

So either make the branch or tag you are using also protected or remove the "Protected" setting from the SSH_PRIVATE_KEY variable.

See: https://gitlab.com/help/ci/variables/README#create-a-custom-variable-in-the-ui

Protect variable (Optional): If selected, the variable will only be available in pipelines that run on protected branches or tags.

Solution 2:[2]

this is because you set SSH_PRIVATE_KEY in a malformed state. my suggestion is to use base64 encoded value in the variable

cat ~/.ssh/id_ras | base64 -w0

add this value to your gitlab ci variable then in your gitlab.ci.yml

ssh-add <(echo "$SSH_PRIVATE_KEY" | base64 -d)

Solution 3:[3]

I had the same issue and the problem was that I saved the key on GITLAB CI as file. changed it variable, problem sloved.

Solution 4:[4]

This issue could also happen because you have created 4096 bit keys. For some reason this fails in pipeline.

Make sure to create 2048 ones or use file as variable type.

Solution 5:[5]

Settings > CI/CD > Variables

  1. Make sure that your SSH_PRIVATE_KEY is file type.
  2. Check if you are using TAGS, if not... check if the file is marked as Protected, uncheck.
  3. If you copy paste from CAT, make sure that there is a blank line after your last line. Just press enter after:

-----END ... PRIVATE KEY-----

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 theshemul
Solution 3 Raimens
Solution 4 Sardorbek Imomaliev
Solution 5 Havyx