【问题标题】:GitLab - Git pull command with username and password in CIGitLab - CI 中带有用户名和密码的 Git 拉取命令
【发布时间】:2018-07-05 17:23:38
【问题描述】:

我已将我的 CI 管道配置为通过 SSH 远程连接到服务器,并使用最新更改更新那里的 git 存储库。然而它失败了。因为当 CI 运行git pull 命令时,响应是:“could not read Username for 'https://gitlab.com'”

我的问题是:有没有办法运行 git pull,在一个命令中包含用户名和密码?

我知道最好的方法是将 SSH 密钥添加到 gitlab 变量中以避免询问用户名和密码,但这对我也不起作用。所以我唯一的选择是提供用户名和密码。

我的 gitlab-ci.yml 文件如下:

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 ubuntu@example.com "
      cd my_Project &&
      git pull
      "

【问题讨论】:

标签: gitlab gitlab-ci gitlab-ci-runner


【解决方案1】:

您的订单不正确。当您应该将 ssh 部署密钥添加到执行 pull 命令的 example.com 环境中时,您将其添加到执行 CI 作业的环境中。

这就像准备一个带椅子和桌子的房间,然后进入下一个房间并期望您可以从另一个房间坐在椅子上。

您应该按照Deploy keys 的说明,将来自ubuntu@example.com 用户的公钥添加到您的项目部署密钥中。

这应该是让最后一点工作的全部。

注意:

您对:echo -e "$DEPLOY_KEY" > ~/.ssh/id_rsa; ssh ubuntu@example.com" cd myProject && git pull 的评论并没有改变顺序,您仍然将 deploy_key 添加到当前环境中,然后通过 ssh 输入另一个。

【讨论】:

  • 我明白你的意思。我现在正在编辑“example.com”环境,例如:“ - echo -e "$DEPLOY_KEY" > ~/.ssh/id_rsa; ssh ubuntu@example.com" cd myProject && git pull " 仍然得到相同的错误说:“无法读取'gitlab.com'的用户名”,非常感谢您的帮助。
  • 令我困惑的是,如果命令错误,为什么我连服务器都可以连接成功?因为我可以很容易地在远程服务器上执行“mkdir”命令。
  • 编辑了答案
【解决方案2】:

我非常努力地按照 gitlab 官方指南here 以及@Stefan 的建议提到的每一步,但我仍然无法在远程服务器上运行git pull 命令。

但我可以通过 SSH 连接到远程服务器。

所以,我最终将 gitlab 凭据存储在服务器上,以避免中断 CI 执行。

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

【讨论】:

    【解决方案3】:

    只需使用以下命令在 GitLab CI 上无需凭据即可拉取 repo

    git pull ${CI_REPOSITORY_URL}

    CI_REPOSITORY_URL -- 是 GitLab 预定义的环境 url

    【讨论】:

      猜你喜欢
      • 2015-06-28
      • 1970-01-01
      • 2013-03-07
      • 2021-08-16
      • 2020-09-08
      • 1970-01-01
      • 2012-11-08
      • 2018-02-20
      • 2021-12-16
      相关资源
      最近更新 更多