Search code examples
amazon-ec2sshgitlabgitlab-cissh-keys

Host key verification problem with gitlab CI


Running a gitlab CI pipeline I'm trying to easy-deploy the repository's code on a EC2 instance.

I generated my ssh-keys for gitlab in my PC to clone and push my code. Then I moved the public and private keys also in the ec2. This just o allow to make the git clone "git.repo.git" in the ec2 instance.

I think this should be the problem, but I can't find a solution, I get this error

Cloning into 'repo-name'... Host key verification failed. fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.

This is my gitlb ci file: (really easy, just for tests)

stages:        
  - deploy

deploy-job:      
  stage: deploy
  script:
    - cat $SSH_KEY > cred.pem && chmod 400 cred.pem
    - ssh -o StrictHostKeyChecking=no -i cred.pem ubuntu@id-amazon.com git clone git@gitlab.com:repo/test.git

Is there a way to pass these credentials correctly?


Solution

  • You are using your keys to connect to id-amazon.com, user ubuntu.

    But they would not be used by the git clone executed on that server, which would explain why the clone fails.

    Although it fails first because the gitlab.com fingerprint is not found in ~ubuntu/.ssh/knwon_hosts.

    See "Using SSH keys with GitLab CI/CD" and its example SSH project.
    Double-check the result of ssh-keyscan gitlab.com with GitLab SSH known_hosts entries.


    As explained by the OP Alex Sander in the comments

    I think a mix of permissions problems, if the folder with the .ssh for the aws-key has 'too much permissions' I saw that it gives problems (I read that is because the ssh key has to be not 'viewable', inside the ubuntu folder I created another folder with chmod 777 in which I which I cloned the directory.

    And for the .ssh files all he permissions in the aws docs.
    To solve these problems I changed in a strange way the commands ran in the GitLab job but it was just this permission problem I think.