本篇目录
- 1. 问题背景
- 2. 环境准备
- 2.1 云上开通windows 2022 英文版机器
- 2.1.1 安装 git
- 2.1.2 安装 runner
- 2.1.3 装docker
- 2.1.4 注册runner并使用docker执行器
 
 
- 3. 项目信息
- 3.1 编写window bat脚本
- 3.2 项目.gitlab-ci.yml文件
 
- 4. 测试结论
- 4.1 运行流水线
 
- 5. troubleshooting
- 问题1:Job failed: invalid volume specification: "/cache"
- 问题2:Windows does not support privileged mode (docker.go:652:0s)
- 问题3:extra_hosts配置生效问题
 
 
该实践来自于客户的一个真实需求

1. 问题背景
客户在 gitlab-runner 是使用的 windows 服务器上的 docker,客户表示在配置了extra_hosts后,发现并没有生效,无法实现某些需求。
2. 环境准备
2.1 云上开通windows 2022 英文版机器
2.1.1 安装 git
参考:安装git
2.1.2 安装 runner
参考:安装runner
2.1.3 装docker
参考: windows-docker
2.1.4 注册runner并使用docker执行器
runner的配置文件
concurrent = 1
check_interval = 0
connection_max_age = "15m0s"
shutdown_timeout = 0
[session_server]
  session_timeout = 1800
[[runners]]
  name = "windows"
  url = "https://jihulab.com"
  id = 31472
  token = "glrt-xxxxxxxxxx"
  token_obtained_at = 2024-08-06T10:34:29Z
  token_expires_at = 0001-01-01T00:00:00Z
  executor = "docker"
  [runners.custom_build_dir]
  [runners.cache]
    MaxUploadedArchiveSize = 0
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]
  [runners.docker]
    # extra_hosts = ["www.baidu.com:10.100.10.10"] # 经过反复多次的测试,发现该配置在windows下的docker执行器中不生效
    tls_verify = false
    image = "ruby:2.7"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["C:\\cache"] # 需要将["/cache"] 改成["C:\\cache"]
    shm_size = 0
    network_mtu = 0
3. 项目信息
3.1 编写window bat脚本
cat test.bat
@echo off
setlocal
rem 检查管理员权限
openfiles >nul 2>&1
if %errorlevel% neq 0 (
    echo 提升到管理员权限...
    powershell start-process -filepath '%0' -verb runas
    exit /b
)
rem 定义要添加的条目
set "hostEntry=192.168.10.100 www.baidu.com"
rem 检查条目是否已经存在
findstr /c:"%hostEntry%" %windir%\System32\drivers\etc\hosts >nul 2>&1
if %errorLevel% neq 0 (
    rem 添加条目到hosts文件
    echo %hostEntry% >> %windir%\System32\drivers\etc\hosts
    echo 条目已添加到hosts文件。
) else (
    echo 条目已存在于hosts文件中。
)
endlocal
3.2 项目.gitlab-ci.yml文件
cat .gitlab-ci.yml
default:
  tags:
    - windows
build:
  stage: build
  image: mcr.microsoft.com/dotnet/framework/sdk:4.8.1-windowsservercore-ltsc2022
  script:
    - ./test.bat
    - ping www.baidu.com
4. 测试结论
4.1 运行流水线
说明写入hosts后,解析生效
 
 修改脚本(解析改成192.168.10.100和10.100.10.10),再次运行,如下两次结论:
 192.168.10.100 无法解析
 
 10.100.10.10 可以解析
 
5. troubleshooting
问题1:Job failed: invalid volume specification: “/cache”

 原因:这个是由于默认runner注册的时候,volume的路径采用的是linux的路径方式,因为需要修改为windows的路径方式,将["/cache"]修改为["c:\\cache"]
问题2:Windows does not support privileged mode (docker.go:652:0s)

 原因:这个是由于windows下runner的docker执行器不支持特权模式,因此将配置文件修改为privileged = true 为 privileged = false。
问题3:extra_hosts配置生效问题
extra_hosts本身是 docker 执行器的有效参数,但是通过在windows 上的 docker 和 linux 上的 docker 执行器上对比,linux 上是可以是生效的,但是 windows 上是不可以的,所以这里需要特殊说明下。


















