Search code examples
gitlabgitlab-cigitlab-ci-runner

GitLab - Git pull command with username and password in CI


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' "

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
      "

Solution

  • 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.