【问题标题】:How use ssh proxy to run deploy job in gitlab ci?如何使用 ssh 代理在 gitlab ci 中运行部署作业?
【发布时间】:2019-05-22 18:12:32
【问题描述】:
我在伊朗有一台服务器,我想使用 gitlab ci 打开一个到我的服务器的 ssh 隧道。
但多亏了谷歌云服务,gitlab 看不到伊朗 IP。
有没有办法使用伊朗的中间服务器打开从 gitlab 到我的代理服务器并从该代理服务器到我的伊朗服务器的代理隧道,然后使用 docker 从 gitlab registery 中提取图像?
假设伊朗服务器无法连接到 gitlab,那么 gitlab 也无法连接到伊朗服务器。
谢谢
【问题讨论】:
标签:
docker
ssh
deployment
proxy
gitlab-ci
【解决方案1】:
这样的代码我已经成功了
before_script:
- apt-get update -y
- apt-get install openssh-client curl -y
integration:
stage: integration
script:
- mkdir ~/.ssh/
- eval $(ssh-agent -s)
- echo "$SSH_KEY" | tr -d '\r' > ~/.ssh/id_rsa
- chmod 600 ~/.ssh/id_rsa
- ssh -fN -L 1029:localhost:1729 user@$HOST -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no 2>&1
- ssh -fN -L 9013:localhost:9713 user@$HOST -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no 2>&1
【解决方案2】:
这对我也有用
deploy:
environment:
name: production
url: http://example.com
image: ubuntu:latest
stage: deploy
only:
- master
before_script:
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
## Install rsync to create mirror between runner and host.
- apt-get install -y rsync
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- eval $(ssh-agent -s)
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
- echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
script:
- ssh-add <(echo "$SSH_PRIVATE_KEY" | base64 --decode)
- ssh -o StrictHostKeyChecking=no $SSH_USER@"$SSH_HOST" 'ls -la && ssh user@host "cd ~/api && docker-compose pull && docker-compose up -d"'
我还在这里描述了我用波斯语所做的一切:
https://virgol.io/@aminkt