ssh - Gitlab CI - 在 Bash 中设置 SSH key

标签 ssh gitlab-ci

我在 SSH keys when using the Docker executor 上关注了 gitlab 的文档。设置到我的远程服务器的连接,它按预期工作。

before_script:
  - which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )
  - 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'

但是,我想将这些命令放在一个单独的脚本中,如下所示:
before_script:
  - bash ./scripts/ssh-config.sh

ssh-config.sh
#!/bin/bash
which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )
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

尝试连接远程服务器时,出现以下错误:
$ bash scripts/ssh-config.sh
/usr/bin/ssh-agent
Agent pid 15
Identity added: /dev/fd/63 (/dev/fd/63)
$ ssh op@example.com "touch test"
Warning: Permanently added 'example.com' (ECDSA) to the list of known hosts.    
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,password).

该脚本似乎已正确执行,并输出了与前一种方法相同的日志。有任何想法吗?

最佳答案

运行 ssh-add 时,请使用 source 或 .以便脚本在同一个 shell 中运行,如果您没有当前 shell 中的 ssh-agent 将没有新 key 。因此,在您的情况下,您将执行以下操作。

before_script:
  - . ./scripts/ssh-config.sh

或者
before_script:
  - source ./scripts/ssh-config.sh

改编自措辞不佳的类似问题的答案。 Here是原件。

注意:不需要bash因为你已经在使用 #!/bin/bash在你的脚本中

关于ssh - Gitlab CI - 在 Bash 中设置 SSH key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46638308/

相关文章:

gitlab-ci - 添加 .gitlab-ci.yml 文件不会激活 CI

node.js - 如何使用 gitlab runners 在 ec2 上自动部署?

python - 在 python 中通过 SSH 运行远程 mysqldump

java - "The Transport Protocol thread failed"– "The socket is EOF"使用 Java 与 J2SSH 连接

mysql - 使用 SSH 的 Laravel MySql 数据库连接

gitlab - 从 `only/except` : How to run a gitlab-ci job only for tags excluding a pattern with `rules` in . gitlab-ci.yml 移植?

java - Ganymed-ssh - 当 execCommand 需要更多时间时如何设置超时关闭 session

hadoop - 在本地PC和SSH根目录上安装HUE或Spark,以访问我公司的Cloudera?

gitlab - 在 gitlab ci 中获取分支名称

gitlab - Gitlab 的 "pages"作业在内部是如何工作的?