1

In gitlab I build my job with a maven image, then copy the jar to the ssh server -> it works fine.

For a php project, I try to use alpine image. But I get rejected with 'Host key verification failed'.

The server and the key are the same.

Not working:

image: alpine:latest
stages:
  - deploy
deploy:
  before_script:
    - apk add --update openssh-client bash
    - eval $(ssh-agent -s)
    - bash -c 'ssh-add <(echo "$SSH_PRIVATE_KEY")'
  stage: deploy
  script:
    - ssh root@devsb01 "ls"

Working:

  image: maven:3.6.0-jdk-10-slim
   stages:
      - deploy
   deploy:
      before_script:
        - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
        - eval $(ssh-agent -s)
        - ssh-add <(echo "$SSH_PRIVATE_KEY")
        - '[[ -f /.dockerenv ]] && mkdir -p ~/.ssh && echo "$KNOWN_HOST" > ~/.ssh/known_hosts'
        - mkdir -p ~/.ssh
        - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
      stage: deploy
      script:
        - ssh root@devsb01 "ls"

I think this has to do with the way the ssh key is add.

Tyvain
  • 2,640
  • 6
  • 36
  • 70
  • Got the exact same problem with an apline image when trying to connect in ssh to my vpn. Did you find any solution ? – Pochwar Apr 18 '19 at 23:27

1 Answers1

1

try adding theses two lines:

- mkdir ~/.ssh
- ssh-keyscan -t rsa devsb01 >> ~/.ssh/known_hosts

It works for me!

Your file will look like that:

image: alpine:latest
stages:
  - deploy
deploy:
  before_script:
    - apk add --update openssh-client bash
    - eval $(ssh-agent -s)
    - bash -c 'ssh-add <(echo "$SSH_PRIVATE_KEY")'
    - mkdir ~/.ssh
    - ssh-keyscan -t rsa devsb01 >> ~/.ssh/known_hosts
  stage: deploy
  script:
    - ssh root@devsb01 "ls"
Pochwar
  • 646
  • 5
  • 15