让gitlab-ci运行在docker上,在主机上使用shell执行器



我们有一个gitlab ci runner,它托管在服务器a下运行的docker容器上。现在我们想将gitlab cirunner配置为一个容器,以便在主机上执行命令。

我们尝试将跑步者注册为";外壳;executor使用下面的命令,但它仍然试图只访问gitlab ci runner容器中的shell,而不是主机服务器A shell。

sudo gitlab runner register
--非交互式
--url"https://gitlab.com/">
-注册令牌"xXXXXXXXXXXXXxx">
--执行者"外壳
-描述"gitlab runner">
-运行未标记
-锁定"false";

提前感谢!

一个简单的解决方案是让容器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 *ntStrictHostKeyChecking nonn" >> ~/.ssh/config

example-stage:
stage: example  
script: 
- ssh $SERVER "your shell command here"

其中$SSH_PRIVATE_KEY$SERVER应该是SSH密钥和user@ipaddress的环境变量

注意,这假设了以下使用SSH密钥代替if password的最佳实践,并且还假设了基于Debian的主机,如果不是Debian,则您的before_script将有所不同,即对于基于Alpine的发行版,用apk add替换apt-get

最新更新