GitLab CI CDとssh-add間の認証の問題

GitLab CI CDとssh-add間の認証の問題

私はgitlab ci cdを使用していますが、残念ながら配布サーバーにアクセスしてssh-agentssh-addを使用して秘密鍵を追加するように求められた場合、エラーのみが表示されますError connecting to agent: No such file or directory。どういうわけかssh-add伝播されない、またはssh-agent開始されません。シェルスクリプトですべての手順を設定しました。以下に示す。

#!/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"
どういうわけかここで何か間違っているようです!誰でもこの問題を解決するのに役立ちますか?

ベストアンサー1

おすすめ記事