2
votes

I have configured my CI pipeline to remote via SSH into a server and update the git repository there with the latest changes. However it fails. Because when the CI runs the git pull command, the response is : "could not read Username for 'https://gitlab.com' "

My question is: Is there a way to run git pull which includes username and password in one single command?

I know the best way is to add the SSH key into gitlab variables to avoid asking for username and password, but that didn't work for me either. so my only option would be to provide username and password.

My gitlab-ci.yml file is like below:

deploy_staging:
  stage: deploy
  before_script:
   - mkdir -p ~/.ssh
   - echo -e "$DEPLOY_KEY" > ~/.ssh/id_rsa
   - chmod 600 ~/.ssh/id_rsa
   - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
  script:
    - ssh [email protected] "
      cd my_Project &&
      git pull
      "
3
Try ssh-add ~/.ssh/id_rsa; ssh -A [email protected] "cd proj && git pull". Check - developer.github.com/v3/guides/using-ssh-agent-forwardingHarish Ved
@HarishVed, I am now getting "Could not open a connection to your authentication agent." error.Benjamin
Check this - stackoverflow.com/a/31835965/827715 @BenjaminHarish Ved

3 Answers

1
votes

Your order is incorrect. You add the ssh deploy key to the environment executing your CI job when you should add it inside the example.com environment executing the pull command.

It's like preparing a room with a chair and table and then entering the next room and expect you can sit down on the chair from the other room.

You should follow the instructions for Deploy keys and add the public key from the [email protected] user to your project deploy keys.

This should be all to make the last bit work.

Note:

Your comment about: echo -e "$DEPLOY_KEY" > ~/.ssh/id_rsa; ssh [email protected]" cd myProject && git pull doesn't change the order, you still add the deploy_key to your current environment and then enter another one through ssh.

1
votes

I tried very hard by following every single step that is mentioned by gitlab official guide here as well as @Stefan's suggestion, and still I wasn't able to run the git pull command on the remote server.

But I could connect to the remote server via SSH.

So, I end up storing the gitlab credential on the server to avoid interrupting the CI execution.

 $ git config credential.helper store
 $ git push http://example.com/repo.git
 Username: <username>
 Password: <password>
0
votes

Simply use the below command to pull repo without credentials on GitLab CI

git pull ${CI_REPOSITORY_URL}

CI_REPOSITORY_URL -- is GitLab predefined environment url