Git实验室 - Git 在 CI 中使用用户名和密码拉取命令
GitLab - Git pull command with username and password in CI
我已将我的 CI 管道配置为通过 SSH 远程连接到服务器并使用最新更改更新那里的 git 存储库。但是它失败了。因为当 CI 运行 执行 git pull
命令时,响应是:"could not read Username for 'https://gitlab.com' "
我的问题是:有没有一种方法可以 运行 git 在一条命令中提取用户名和密码?
我知道最好的方法是将 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
"
您的订单有误。当您应该将 ssh 部署密钥添加到执行 CI 作业的环境中时,您应该将其添加到执行 pull
命令的 example.com
环境中。
这就像准备一个房间,里面有一把椅子和table,然后进入下一个房间,期望你可以坐在另一个房间的椅子上。
您应该按照 Deploy keys 的说明进行操作,并将 ubuntu@example.com
用户的 public 密钥添加到您的项目部署密钥中。
这应该是让最后一点起作用的全部。
注:
您的评论:echo -e "$DEPLOY_KEY" > ~/.ssh/id_rsa; ssh ubuntu@example.com" cd myProject && git pull
并没有改变顺序,您仍然将 deploy_key 添加到您当前的环境,然后通过 ssh 进入另一个。
我非常努力地按照 gitlab 官方指南 here 以及@Stefan 的建议提到的每一步进行操作,但我仍然无法 运行 [=11] =] 远程服务器上的命令。
但我可以通过 SSH 连接到远程服务器。
所以,我最终将 gitlab 凭据存储在服务器上以避免中断 CI 执行。
$ git config credential.helper store
$ git push http://example.com/repo.git
Username: <username>
Password: <password>
只需使用以下命令在 GitLab 上无需凭据即可拉取 repo CI
git pull ${CI_REPOSITORY_URL}
CI_REPOSITORY_URL -- 是GitLab预定义环境url
我已将我的 CI 管道配置为通过 SSH 远程连接到服务器并使用最新更改更新那里的 git 存储库。但是它失败了。因为当 CI 运行 执行 git pull
命令时,响应是:"could not read Username for 'https://gitlab.com' "
我的问题是:有没有一种方法可以 运行 git 在一条命令中提取用户名和密码?
我知道最好的方法是将 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
"
您的订单有误。当您应该将 ssh 部署密钥添加到执行 CI 作业的环境中时,您应该将其添加到执行 pull
命令的 example.com
环境中。
这就像准备一个房间,里面有一把椅子和table,然后进入下一个房间,期望你可以坐在另一个房间的椅子上。
您应该按照 Deploy keys 的说明进行操作,并将 ubuntu@example.com
用户的 public 密钥添加到您的项目部署密钥中。
这应该是让最后一点起作用的全部。
注:
您的评论:echo -e "$DEPLOY_KEY" > ~/.ssh/id_rsa; ssh ubuntu@example.com" cd myProject && git pull
并没有改变顺序,您仍然将 deploy_key 添加到您当前的环境,然后通过 ssh 进入另一个。
我非常努力地按照 gitlab 官方指南 here 以及@Stefan 的建议提到的每一步进行操作,但我仍然无法 运行 [=11] =] 远程服务器上的命令。
但我可以通过 SSH 连接到远程服务器。
所以,我最终将 gitlab 凭据存储在服务器上以避免中断 CI 执行。
$ git config credential.helper store
$ git push http://example.com/repo.git
Username: <username>
Password: <password>
只需使用以下命令在 GitLab 上无需凭据即可拉取 repo CI
git pull ${CI_REPOSITORY_URL}
CI_REPOSITORY_URL -- 是GitLab预定义环境url