通过 Gitlab CICD SSH 到 EC2 实例并运行命令

问题描述 投票:0回答:1

我目前正在使用 gitlab CICD ssh 到我们正在运行的 Gitlab EC2 实例之一来运行 rake 命令。我有以下代码用于测试目的,似乎虽然成功建立了 SSH 连接,但后续命令(例如 pwd 和 ls -l /etc/gitlab)并未执行。此问题会导致管道挂起而未完成所需命令的执行。

image: buildtime_base_v2:3.25.1

stages:

  - test

test:

  stage: test

  script:

    - |

      echo "$SSH_KEY_VARIABLE" > ssh_key.pem

      chmod 600 ssh_key.pem

      ssh -i "ssh_key.pem" -t -t -o StrictHostKeyChecking=no centos@<ip>

      pwd

      ls -l /etc/gitlab

git gitlab gitlab-ci gitlab-ci-runner gitlab-omnibus
1个回答
0
投票

此 YAML 并未告诉 SSH 远程运行任何命令;它说“通过 SSH 连接到盒子,然后当你完成后在本地运行这些其他命令”。您需要将远程系统命令放入 SSH 命令中:

ssh -i /path/to/key <other options here> user@host "pwd && ls -l /etc/gitlab/"
。这将使用指定的参数通过 SSH 连接到
host
;在
pwd
上运行
host
;然后,如果
pwd
成功退出,则在
ls -l /etc/gitlab/
上运行
host

如果远程运行的命令变得难以使用,请将它们放入远程主机上的脚本中:

ssh -i /path/to/key <other options here> user@host "/opt/deployment_script.sh"
。这将使用指定的参数通过 SSH 连接到
host
,然后在
/opt/deployment_script.sh
上运行
host

© www.soinside.com 2019 - 2024. All rights reserved.