Gitlab - CI和CD构建的远程服务器认证失败

3
我在我的“.gitlab-ci.yml”中尝试远程连接到Ubuntu服务器以执行SSH命令时,出现了“输入/dev/fd/63的密码短语”的错误。
我创建了一个名为“STAGING_PRIVATE_KEY”的新变量,并将其值设置为我个人用于SSH到服务器的私钥,但是将相同的密钥提供给“.gitlab-ci.yml”无法进行身份验证。
以下是我的yml文件:

deploy_staging:
  stage: deploy
  before_script:
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
  - mkdir -p ~/.ssh
  - eval $(ssh-agent -s)
  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
  script:
    - ssh-add <(echo "$STAGING_PRIVATE_KEY" | base64 --decode)
    - cd test
    - git pull
    - echo "deployed to staging server"
  environment:
    name: staging
    url: MY SERVER

2个回答

3

我使用以下代码片段通过 .gitlab-ci.yml 作业进行ssh连接,STAGING_SSH_KEY被存储为一个变量,可以在设置 -> CI/CD -> 变量中找到。

variables:
    GIT_SSL_NO_VERIFY: "true"

image: someimage:latest #replace with any valid image which has ssh installed

before_script:
  - mkdir -p ~/.ssh
  - echo -e "$STAGING_SSH_KEY" > ~/.ssh/id_rsa
  - chmod 600 ~/.ssh/id_rsa
  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

stages:
 - deploy

deploy_STAGING_job:
  stage: deploy
  script:
   - echo "ssh into the below random IP"
   - ssh myuser@10.200.200.200"
     echo "Login using ssh to remote instance"
     "

0

由于openssh已经与Git for Windows捆绑在一起,因此请尝试使用一个opeenssh密钥(使用ssh-keygen生成),暂时不要设置密码(以避免需要ssh-agent)

在AWS端注册您的openssh公钥(默认为id_rsa.pub)。
例如,如“将您自己的公钥导入Amazon EC2”中所述。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接