如何解决使gitlab-ci Runner在docker上运行并在主机上使用shell executor
我们有一个gitlab-ci运行器,该运行器托管在服务器A下运行的docker容器上。现在,我们希望将gitlab-ci运行器配置为容器,以在主机上执行命令。
我们尝试使用以下命令将运行程序注册为“ shell”执行程序,但仍尝试仅在gitlab-ci运行程序容器(而不是主机服务器A Shell)内访问该外壳程序。
sudo gitlab-runner注册
-非互动
--url“ https://gitlab.com/”
--registration-token“ xXXXXXXXXXXXXXXx”
--executor“ shell”
--description“ gitlab-runner”
--run-unagged
-锁定了“假”
谢谢!
解决方法
一个简单的解决方案是将容器SSH从运行程序中放入主机,并在运行程序访问主机后即运行命令,即
before_script:
- set -e
- '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
- touch ~/.ssh/config
- echo -e "Host *\n\tStrictHostKeyChecking no\n\n" >> ~/.ssh/config
example-stage:
stage: example
script:
- ssh $SERVER "your shell command here"
其中$SSH_PRIVATE_KEY
和$SERVER
应该是您的SSH密钥和user@ipaddress
的环境变量
注意,这假设遵循以下最佳做法,即使用SSH密钥代替密码(如果使用密码),并且假设基于Debian的主机,如果您的before_script
不是Debian,则将有所不同,即用{替换apt-get
{1}}用于基于高山的发行版