Gitlab Runner 实现内网自动部署

最近出差支持项目上线,我们的部署流程是Gitlab CI打包Docker镜像上传到云镜像服务,现场部署由于内网无法访问公网,没办法直接使用脚本实现自动部署(Runner没办法连接~)。目前的情况是,客户提供了一台可访问外网的机器(不提供也可以使用本机),并且在同一个网段,也算是放开一个口子,可以基于这一台机器去实现不同操作,以下便是提供的一种解决思路,减少工作量。

Ubuntu安装Gitlab Runner

添加Gitlab Runner仓库

首先,添加Gitlab Runner的官方仓库,运行一下命令添加:

1
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash

image-20230511103118473

安装Gitlab Runner

使用apt包管理器安装Gitlab Runner

1
sudo apt-get install gitlab-runner

image-20230511103303074

检查Gitlab Runner状态

1
sudo gitlab-runner status

查看Runner运行状态

1
2
3
root@runner:/home/jonty# sudo gitlab-runner status
Runtime platform arch=amd64 os=linux pid=2966404 revision=436955cb version=15.11.0
gitlab-runner: Service is running

image-20230511103329891

成功在Ubuntu安装Gitlab Runner

卸载Gitlab Runner

停止服务:

1
gitlab-runner stop

卸载服务:

1
gitlab-runner uninstall

注册Gitlab Runner

将Runner注册到Gitlab项目中

1、获取Gitlab项目注册令牌:

  • 仓库RUL地址
  • 仓库注册令牌

image-20230511102811943

2、 注册Gitlab Runner

1
sudo gitlab-runner register

按照提示输入Gitlab 示例的URL、项目的注册令牌以及Runner描述信息、Tag

这里executor选择shell

image-20230511103545979

3、激活Gitlab Runner

1
sudo gitlab-runner verify

image-20230511103918535

在项目Runner中可以看到已经注册完成:

image-20230511103934445

4、Gitlab Runner后台运行

使用“&”将在后台启动,即使关闭 SSH 会话,它仍然存在,只有在服务器重新启动或关闭时才会停止

1
sudo gitlab-runner run&

定义Job

.gitlab-ci.yml中定义Job

script执行runner宿主机/bin/bash /images_194/deploy_194.sh脚本,实现曲线救国

1
2
3
4
5
6
7
8
9
10
11
12
13
deploy_dev:
stage: deploy
dependencies:
- build_host_dev
only:
refs:
- dev-1.5
tags:
- dev
retry:
max: 2
script:
- /bin/bash /images_194/deploy_194.sh

最后贴出连接局域网另一台机器部署的脚本:

  • 拉取公网镜像,重新打Tag,推送到内网Registry
  • ssh连接到内网机器,执行部署脚本
  • 完成部署
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash

imgAppNames=('server' 'vue')

function AppFile(){

WMS_VERSION=dev
REGISTRY_URL=ccr.ccs.tencentyun.com/xxx
LocalRegistryUrl=192.168.2.89:8232

echo $WMS_VERSION
echo $REGISTRY_URL
echo $LocalRegistryUrl

echo "正在处理应用镜像"
PullImage
TagImage
PushImage
}

function PullImage(){
for imgName in ${imgAppNames[@]}
do
targetImgName="$REGISTRY_URL/${imgName}:$WMS_VERSION"

echo "正在拉取: $targetImgName"
docker pull $targetImgName
done
echo "拉取完成"
}

function TagImage(){
for imgName in ${imgAppNames[@]}
do
targetImgName="$REGISTRY_URL/${imgName}:$WMS_VERSION"
localImgName="$LocalRegistryUrl/${imgName}:$WMS_VERSION"

echo "正在打标签: $targetImgName"
docker tag $targetImgName $localImgName
done
echo "打标签完成"
}

function PushImage(){
for imgName in ${imgAppNames[@]}
do
localImgName="$LocalRegistryUrl/${imgName}:$WMS_VERSION"

echo "正在推送: $localImgName"
docker push $localImgName
done
echo "推送完成"
}

AppFile

# 配置参数
REMOTE_IP="192.168.2.194"
REMOTE_USER="wms"
REMOTE_PASS="wmstest@123"
ROOT_PASS="wmstest@123"


# 连接到远程服务器
sshpass -p "${REMOTE_PASS}" ssh -o StrictHostKeyChecking=no -l "${REMOTE_USER}" "${REMOTE_IP}" <<- EOF

# 切换到 root 用户
echo "${ROOT_PASS}" | sudo -S su

# 切换到指定目录
cd /home/wms/wms_app/07_app

echo "移除当前服务:test_app"
sudo docker stack rm test_app

sleep 20s

echo "启动服务中..."
# 执行 run.sh
sudo /bin/bash ./run.sh

echo "服务启动完成..."

sudo docker service ls -f name=test_app

# 退出 root 用户
exit

# 退出远程服务器
EOF

问题

如果Runner在执行服务器脚本时报错,如下:

1
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.40/auth: dial unix /var/run/docker.sock: connect: permission denied

解决方案:

授予运行作业的用户访问 Docker 资源的权限

1
usermod -a -G docker gitlab-runner

参考:How to fix Gitlab Runner ‘dial unix /var/run/docker.sock: connect: permission denied’ | TechOverflow