【问题标题】:Unable to use Gitlab Ci to deploy two repositories无法使用 Gitlab Ci 部署两个存储库
【发布时间】:2021-01-25 09:56:05
【问题描述】:

我在 gitLab 中有两个存储库。一个有前端应用程序(Ionic、Cypress),另一个有后端应用程序(Django、Postgis)。要进行集成测试,我需要后端和前端。当我在前端提交时,我想使用 gitLab CI 在服务器中部署两个项目,然后从那里执行脚本来运行 cypress 测试。我的问题是部署两个存储库。它适用于第一个,但不适用于第二个。我是 gitlab CI 的新手。 我的 gitlab-ci.yml

variables:
  BACKEND: git@gitlab.com:ctavar01/advisor_grammar_es_tests.git

before_script:
  - apt-get update -qq
  - apt-get install -qq git
  # Setup SSH deploy keys
  - 'which ssh-agent || ( apt-get install -qq openssh-client )'
  - eval $(ssh-agent -s)
  - ssh-add <(echo "$SSH_PRIVATE_KEY")
  - mkdir -p ~/.ssh
  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

deploy_staging:
  type: deploy
  environment:
    name: staging
    url: myipaddress
  script:
    - ssh myuser@myipaddress "cd ./app && git checkout master && git pull origin master && exit"
    - ssh myuser@myipaddress "cd ./backend && git checkout master && git pull origin master && exit"
  only:
    - master

我在目标服务器的用户帐户中做了什么 使用两个密钥对:一个用于 repo 后端,另一个用于 repo 应用程序 id_rsa, id_rsa_app 我已将两个密钥添加到服务器中的用户代理 我的配置

# GitLab.com server
Host username1
    HostName gitlab.com
    RSAAuthentication yes
    IdentityFile ~/.ssh/id_rsa
Host username2
    HostName gitlab.com
    RSAAuthentication yes
    IdentityFile ~/.ssh/id_rsa_app

我已将两个公钥复制到相应的 gitlab repos deploy 密钥中 我已将私钥复制到相应的 gitlab repos 变量中(SSH_PRIVATE_KEY)

第一个脚本命令正确执行。第二次在 git pull origin master 中失败 我想我无法让脚本识别第二个键。我错过了什么?

不知道如何介绍所要求的细节。发布为已接受的答案,但事实并非如此。作业输出

Getting source from Git repository
00:03
$ eval "$CI_PRE_CLONE_SCRIPT"
Fetching changes with git depth set to 50...
Initialized empty Git repository in /builds/ctavar01/advisor_app/.git/
Created fresh repository.
Checking out f323f983 as master...
Skipping Git submodules setup
Executing "step_script" stage of the job script
00:06
$ apt-get update -qq
$ apt-get install -qq git
$ which ssh-agent || ( apt-get install -qq openssh-client )
/usr/bin/ssh-agent
$ eval $(ssh-agent -s)
Agent pid 267
$ ssh-add <(echo "$SSH_PRIVATE_KEY")
Identity added: /dev/fd/63 (/dev/fd/63)
$ mkdir -p ~/.ssh
$ [[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
$ ssh myuser@ myipaddress "cd /home/marketradar_e2e/app && git checkout master && git pull origin master && exit"
Warning: Permanently added 'myipaddress' (ECDSA) to the list of known hosts.
Permission denied, please try again.
Permission denied, please try again.
myuser@myipaddress: Permission denied (publickey,password).
Cleaning up file based variables
00:01
ERROR: Job failed: exit code 1

【问题讨论】:

  • 我们能看到ssh myuser@myipaddress "cd ./app &amp;&amp; git remote -v"ssh myuser@myipaddress "cd ./backend &amp;&amp; git remote -v"的结果吗?

标签: git ssh gitlab-ci cypress


【解决方案1】:

如果您有一个带有 Host 条目的 ~/.ssh/config 文件,那么要使用的 SSH URL 将是:

myuser@username1
# or
myuser@username2

只有使用Host 条目,您才能引用正确的私钥。

由于第一个条目使用 默认 ~/.ssh/id_rsa 私钥,因此它适用于默认 SSH URL。
但第二个只有在您使用正确的 SSH URL 时才有效,基于第二个 Host 条目。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 2021-10-21
    • 2016-08-02
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2019-10-09
    相关资源
    最近更新 更多