【问题标题】:Unable to SSH into my server using Gitlab CI Docker executor无法使用 Gitlab CI Docker 执行程序通过 SSH 连接到我的服务器
【发布时间】:2020-07-16 09:11:29
【问题描述】:

我无法使用 Gitlab CI SSH 进入我的服务器。我已经尝试了 Stack Overflow 中所有可能的解决方案,但仍然无法解决它。 :(

这是我用来参考的链接:https://docs.gitlab.com/ee/ci/examples/deployment/composer-npm-deploy.html

我的 gitlab 运行器在一个 VM 下运行,而我的部署服务器在另一个 VM 中运行。它们都由 VMWare ESXI 管理。 Gitlab runner 正在使用 Docker。

我尝试过的事情:

  1. 在我的部署服务器上禁用 UFW 防火墙。
  2. 将我的部署服务器 ssh 公钥添加到 Gitlab 密钥/
  3. 将我的私钥添加到 Gitlab 变量中。

以下是我使用的脚本/yaml 文件:


image: node:12.18.2

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'

cache:
  key: "$CI_COMMIT_REF_NAME"
  paths:
    - node_modules/

stages:
#  - setup
#  - test
#  - build
  - deploy

#setup:
#  stage: setup
#  script:
#    - npm install

#test:
#  stage: test
#  script:
#    - echo Testing...
#    - env CI=true npm test
#
#build:
#  stage: build
#  script:
#    - echo Building...
#    - npm run build
#  only:
#    - master

deploy:
  stage: deploy
  artifacts:
    paths:
      - build/
  script:
    - ssh -A scim@192.168.100.201
#    - ssh -A scim@192.168.100.201 "mkdir /home/scim/Desktop/build_tmp"
#    - scp -r build/* scim@192.168.100.201:/home/scim/Desktop/build_tmp
#    - ssh scim@192.168.100.201 "mv /home/scim/Desktop/build /home/scim/Desktop/build_old && mv /home/scim/Desktop/build_tmp /home/scim/Desktop/build"
#    - ssh server_user@server_host "rm -rf /home/scim/Desktop/build_old"
  only:
    - master

这是它在 Gitlab UI 上产生的错误消息。

Running with gitlab-runner 13.1.0 (6214287e)
  on docker-auto-scale 72989761
Preparing the "docker+machine" executor
00:39
Using Docker executor with image node:12.18.2 ...
Pulling docker image node:12.18.2 ...
Using docker image sha256:1fa6026dd8bbe97cf9d38fbf7e83b3f157aac1e28cad349a143c8920705771d6 for node:12.18.2 ...
Preparing environment
00:05
Running on runner-72989761-project-19942034-concurrent-0 via runner-72989761-srm-1594818580-ad6e18fc...
Getting source from Git repository
00:02
$ eval "$CI_PRE_CLONE_SCRIPT"
Fetching changes with git depth set to 50...
Initialized empty Git repository in /builds/SaiMun92/SCIM_Webapp_Frontend/.git/
Created fresh repository.
Checking out 8edfe553 as master...
Skipping Git submodules setup
Restoring cache
00:15
Checking cache for master...
Downloading cache.zip from https://storage.googleapis.com/gitlab-com-runners-cache/project/19942034/master 
Successfully extracted cache
Executing "step_script" stage of the job script
00:33
$ which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )
/usr/bin/ssh-agent
$ eval $(ssh-agent -s)
Agent pid 13
$ ssh-add <(echo "$SSH_PRIVATE_KEY")
Identity added: /dev/fd/63 (saimun.lee@tauexpress.com)
$ mkdir -p ~/.ssh
$ [[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
$ ssh scim@192.168.100.201
Pseudo-terminal will not be allocated because stdin is not a terminal.
ssh: connect to host 192.168.100.201 port 22: Connection timed out
ERROR: Job failed: exit code 1

【问题讨论】:

    标签: linux git docker ssh gitlab


    【解决方案1】:

    这是一个网络问题,您必须从主机而不是在运行器中进行调试

    1. 访问执行 gitlab runner 的主机
    2. 检查此主机是否可以访问其他主机上的 ssh 端口
    telnet 192.168.100.201 22
    

    或

    nc -zv 192.168.100.201 22
    

    在192.168.100.201 主机中检查ssh 是否正在运行以及它是否正在侦听端口22

    ps aux | grep sshd
    netstat -plant | grep :22
    

    可选 - 从 localhost 测试 ssh 端口(在目标主机上)

    telnet localhost 22
    

    最后一个问题,主机是否在同一个虚拟机网络上?

    【讨论】:

    • 这台主机可以到达另一台服务器的ssh端口。目标主机上的 telnet localhost 22 表示它已连接到 localhost。
    猜你喜欢
    • 2022-07-11
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    • 2021-08-28
    • 1970-01-01
    • 2023-03-08
    • 2020-08-21
    • 1970-01-01
    相关资源
    最近更新 更多