gitlab ci不能运行超过2条ssh命令

时间:2018-12-06 18:56:28

标签: docker gitlab-ci

我正在尝试使用ci从gitlab的docker部署我的应用程序。我已经按照以下方式配置了gitlab-ci(它的简化过程是,通过docker步骤完成docker build和push to registy,但是出于可读性考虑,我将其跳过)

image: docker:stable

services:
  - postgres:latest
  - docker:dind

stages:
  - test
  - build
  - deploy

variables:
  DOCKER_HOST: tcp://docker:2375/
  DOCKER_DRIVER: overlay2

run_develop_container:
  stage: deploy
  image: kroniak/ssh-client
  before_script:
    - which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
    - mkdir -p ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    - ssh-keyscan -H $DEVELOP_SERVER >> ~/.ssh/known_hosts
    - chmod 700 ~/.ssh
  script:
    - scp docker-compose-develop.yml $SSH_DEPLOY_USER@$DEVELOP_SERVER:~/docker-compose.yml
    - ssh -o StrictHostKeyChecking=no $SSH_DEPLOY_USER@$DEVELOP_SERVER "docker login -u $DOCKER_USER_ID -p $DOCKER_USER_PASSWORD"
    - ssh -o StrictHostKeyChecking=no $SSH_DEPLOY_USER@$DEVELOP_SERVER "docker-compose -p app-name down --remove-orphans"
    - ssh -o StrictHostKeyChecking=no $SSH_DEPLOY_USER@$DEVELOP_SERVER "docker-compose -p app-name pull && docker-compose -p app-name up -d"
  only:
    - setup_deploy_process

管道的输出如下所示

... some common things ...
$ which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )
/usr/bin/ssh-agent
$ eval $(ssh-agent -s)
Agent pid 12
$ echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
Identity added: (stdin) ((stdin))
$ mkdir -p ~/.ssh
$ [[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
$ ssh-keyscan -H $DEVELOP_SERVER >> ~/.ssh/known_hosts
# some_data_with_develop_server_ip
$ chmod 700 ~/.ssh
$ scp docker-compose-develop.yml $SSH_DEPLOY_USER@$DEVELOP_SERVER:~/docker-compose.yml
$ ssh -o StrictHostKeyChecking=no $SSH_DEPLOY_USER@$DEVELOP_SERVER "docker login -u $DOCKER_USER_ID -p $DOCKER_USER_PASSWORD"
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Login Succeeded
WARNING! Your password will be stored unencrypted in /home/runner/.docker/config.json.
Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store

$ ssh -o StrictHostKeyChecking=no $SSH_DEPLOY_USER@$DEVELOP_SERVER "docker-compose -p app-name down --remove-orphans"
ssh: connect to host 159.89.24.62 port 22: Connection refused
ERROR: Job failed: exit code 1

现在,对我来说ssh似乎配置正确,因为脚本能够连接到scp docker-compose并登录到注册表。

作为开发服务器,我有一个来自DigitalOcean的一键式Docker Drop,是否有可能无法处理此流量?也许我应该等一会儿再调用下一个ssh命令?我完全迷失了,因为这是一个非常基本的配置,而且我找不到其他任何配置,因此我非常感谢您提供任何提示,谢谢!

1 个答案:

答案 0 :(得分:1)

也许在DO限制ssh的某处有一个配置可以防止多个连续的连接? (可以通过向-v命令传递至少一个ssh标志来对此进行调查...)

无论如何,一种解决方案可能是将ssh命令合并为一个。

例如,您可以尝试这样的事情吗?

- ssh -o StrictHostKeyChecking=no …@… "docker login … && docker-compose …"

否则,假设您的仓库中有一个bash脚本,则也可以使用管道来远程运行此脚本,例如:

- cat some_script.sh | ssh -o StrictHostKeyChecking=no …@… "bash --login"