GitLab CI CD 与 ssh-add 的身份验证问题

GitLab CI CD 与 ssh-add 的身份验证问题

我正在使用 gitlab ci cd,但不幸的是,当我访问我的部署服务器并提示ssh-agent并使用 ssh-add 添加我的私钥后,它只显示了错误Error connecting to agent: No such file or directory。不知怎的,ssh-add没有被传播或ssh-agent没有被启动。我已经在 shell 脚本上配置了所有步骤。下面给出。

#!/bin/bash

# Get server list 
set -f
string=$DEPLOY_SERVER_IP
array=(${string//,/ })

# Iterate servers for deploy and pull last commit 
for i in "${!array[@]}"; do
  echo "Deploy project on server ${array[i]}"
  ssh bitnami@${array[i]} "eval $(ssh-agent -s)\ssh-add relation-fe && cd htdocs/relation-fe/ && git pull"
done

gitlab-ci.yml 文件包含以下内容

build-job:
  tags:
    - ci
  stage: build
  script:
    - echo "Hello, $GITLAB_USER_LOGIN!"

test-job1:
  tags:
    - ci
  stage: test
  script:
    - echo "This job tests something"

test-job2:
  tags:
    - ci
  stage: test
  script:
    - echo "This job tests something, but takes more time than test-job1."
    - echo "After the echo commands complete, it runs the sleep command for 20 seconds"
    - echo "which simulates a test that runs 20 seconds longer than test-job1"
    - sleep 20

deploy-prod:
  tags:
    - ci
  stage: deploy
  only:
    - master
  before_script:
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    - eval $(ssh-agent -s)
    - mkdir -p ~/.ssh
    - echo -e "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
  script:
    - bash .gitlab-deploy-prod.sh

我的部署构建日志上的输出。

Preparing the "shell" executor
Using Shell executor...
Preparing environment
Running on ip-172-26-14-215...
Getting source from Git repository
Fetching changes with git depth set to 50...
Reinitialized existing Git repository in /home/gitlab-runner/builds/8x1yWQpE/0/root/relation-fe/.git/
Checking out b7d66a8d as master...
Skipping Git submodules setup
Executing "step_script" stage of the job script
$ which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )
/usr/bin/ssh-agent
$ eval $(ssh-agent -s)
Agent pid 26162
$ mkdir -p ~/.ssh
$ echo -e "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
$ chmod 600 ~/.ssh/id_rsa
$ [[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
$ bash .gitlab-deploy-prod.sh
Deploy project on server 1.2.3.4
Agent pid 26167
Error connecting to agent: No such file or directory
Cleaning up file based variables
ERROR: Job failed: exit status 1

有人可以帮我解决这个问题吗?我现在被困在这里几天了。我认为主要问题在这一行
ssh bitnami@${array[i]} "eval $(ssh-agent -s)\ssh-add relation-fe && cd htdocs/relation-fe/ && git pull"
不知何故我在这里做错了什么!有人能帮我解决这个问题吗?

相关内容