Runner配置 #

一、配置文件结构 #

全局配置 #

toml
concurrent = 4
check_interval = 0
log_level = "info"

[[runners]]
  name = "my-runner"
  url = "https://gitlab.example.com"
  token = "YOUR_TOKEN"
  executor = "docker"

配置层级 #

text
全局配置
├── concurrent
├── check_interval
├── log_level
│
└── [[runners]]
    ├── name
    ├── url
    ├── token
    ├── executor
    │
    ├── [runners.docker]
    │   ├── image
    │   ├── privileged
    │   └── volumes
    │
    ├── [runners.kubernetes]
    │   ├── namespace
    │   └── image
    │
    └── [runners.cache]
        ├── Type
        └── Path

二、全局配置 #

concurrent #

同时运行的作业数量:

toml
concurrent = 4

check_interval #

检查新作业的间隔(秒):

toml
check_interval = 3

log_level #

日志级别:

toml
log_level = "debug"

可选值:debuginfowarnerrorfatalpanic

listen_address #

Prometheus监控地址:

toml
listen_address = ":9252"

三、Runner基础配置 #

基本配置 #

toml
[[runners]]
  name = "my-runner"
  url = "https://gitlab.example.com"
  token = "YOUR_TOKEN"
  executor = "docker"
  limit = 2
  output_limit = 4096

配置项说明 #

配置项 说明
name Runner名称
url GitLab URL
token Runner令牌
executor 执行器类型
limit 最大并发作业数
output_limit 日志输出限制(KB)

环境变量 #

toml
[[runners]]
  environment = ["ENV=value", "FOO=bar"]

禁用Runner #

toml
[[runners]]
  active = false

四、Shell Executor配置 #

基本配置 #

toml
[[runners]]
  name = "shell-runner"
  executor = "shell"
  shell = "bash"

环境配置 #

toml
[[runners]]
  executor = "shell"
  environment = [
    "PATH=/usr/local/bin:/usr/bin:/bin",
    "NODE_ENV=production"
  ]

五、Docker Executor配置 #

基本配置 #

toml
[[runners]]
  name = "docker-runner"
  executor = "docker"
  [runners.docker]
    image = "node:18"
    privileged = false
    disable_cache = false

镜像配置 #

toml
[[runners]]
  [runners.docker]
    image = "node:18"
    allowed_images = ["node:*", "python:*", "golang:*"]
    allowed_services = ["postgres:*", "redis:*", "mysql:*"]
    pull_policy = ["if-not-present"]

拉取策略 #

策略 说明
always 总是拉取镜像
if-not-present 本地不存在时拉取
never 从不拉取

权限配置 #

toml
[[runners]]
  [runners.docker]
    privileged = true
    cap_add = ["SYS_ADMIN"]
    devices = ["/dev/kvm"]

卷挂载 #

toml
[[runners]]
  [runners.docker]
    volumes = [
      "/var/run/docker.sock:/var/run/docker.sock",
      "/cache:/cache",
      "/builds:/builds"
    ]

缓存配置 #

toml
[[runners]]
  [runners.docker]
    disable_cache = false
    cache_dir = "/cache"

网络配置 #

toml
[[runners]]
  [runners.docker]
    network_mode = "host"
    links = ["mysql:mysql"]

资源限制 #

toml
[[runners]]
  [runners.docker]
    memory = "2g"
    cpus = "2"
    memory_swap = "4g"

安全配置 #

toml
[[runners]]
  [runners.docker]
    security_opt = ["no-new-privileges"]
    tls_verify = true
    tls_cert_path = "/certs"

六、Kubernetes Executor配置 #

基本配置 #

toml
[[runners]]
  name = "kubernetes-runner"
  executor = "kubernetes"
  [runners.kubernetes]
    namespace = "gitlab-runner"
    image = "node:18"

Pod配置 #

toml
[[runners]]
  [runners.kubernetes]
    namespace = "gitlab-runner"
    image = "node:18"
    cpu_limit = "1"
    memory_limit = "2Gi"
    service_cpu_limit = "500m"
    service_memory_limit = "1Gi"

节点选择 #

toml
[[runners]]
  [runners.kubernetes]
    node_selector = {
      "kubernetes.io/os": "linux",
      "node-role.kubernetes.io/ci": "true"
    }

污点容忍 #

toml
[[runners]]
  [[runners.kubernetes.node_tolerations]]
    key = "dedicated"
    operator = "Equal"
    value = "ci"
    effect = "NoSchedule"

服务账号 #

toml
[[runners]]
  [runners.kubernetes]
    service_account = "gitlab-runner"

卷挂载 #

toml
[[runners]]
  [[runners.kubernetes.volumes.host_path]]
    name = "docker-sock"
    mount_path = "/var/run/docker.sock"
    host_path = "/var/run/docker.sock"

ConfigMap挂载 #

toml
[[runners]]
  [[runners.kubernetes.volumes.config_map]]
    name = "config"
    mount_path = "/config"

七、缓存配置 #

S3缓存 #

toml
[[runners]]
  [runners.cache]
    Type = "s3"
    Path = "runner-cache"
    Shared = true
    [runners.cache.s3]
      ServerAddress = "s3.amazonaws.com"
      BucketName = "gitlab-runner-cache"
      BucketLocation = "us-east-1"
      AccessKey = "YOUR_ACCESS_KEY"
      SecretKey = "YOUR_SECRET_KEY"

GCS缓存 #

toml
[[runners]]
  [runners.cache]
    Type = "gcs"
    Path = "runner-cache"
    [runners.cache.gcs]
      BucketName = "gitlab-runner-cache"
      CredentialsFile = "/path/to/credentials.json"

Azure缓存 #

toml
[[runners]]
  [runners.cache]
    Type = "azure"
    Path = "runner-cache"
    [runners.cache.azure]
      ContainerName = "gitlab-runner-cache"
      AccountName = "storage-account"
      AccountKey = "storage-key"

八、自定义构建目录 #

配置构建目录 #

toml
[[runners]]
  builds_dir = "/opt/builds"
  cache_dir = "/opt/cache"

Docker卷挂载 #

toml
[[runners]]
  builds_dir = "/opt/builds"
  [runners.docker]
    volumes = [
      "/opt/builds:/opt/builds",
      "/opt/cache:/opt/cache"
    ]

九、Runner监控 #

Prometheus监控 #

toml
listen_address = ":9252"

[[runners]]
  name = "monitored-runner"
  metrics_server = ":9252"

监控指标 #

text
# 作业执行次数
gitlab_runner_jobs_total

# 当前运行作业数
gitlab_runner_jobs_running_total

# 作业执行时间
gitlab_runner_job_duration_seconds

# Runner状态
gitlab_runner_up

十、高级配置 #

预构建脚本 #

toml
[[runners]]
  pre_build_script = "echo 'Pre-build script'"
  post_build_script = "echo 'Post-build script'"

自定义环境变量 #

toml
[[runners]]
  environment = [
    "CUSTOM_VAR=value",
    "BUILD_ENV=production"
  ]

请求并发 #

toml
[[runners]]
  request_concurrency = 4

输出限制 #

toml
[[runners]]
  output_limit = 8192

十一、配置示例 #

Docker Runner完整配置 #

toml
concurrent = 4
check_interval = 3
log_level = "info"

[[runners]]
  name = "docker-runner"
  url = "https://gitlab.example.com"
  token = "YOUR_TOKEN"
  executor = "docker"
  limit = 2
  environment = ["DOCKER_DRIVER=overlay2"]
  
  [runners.docker]
    image = "node:18"
    privileged = true
    disable_cache = false
    pull_policy = ["if-not-present"]
    allowed_images = ["node:*", "python:*", "golang:*"]
    allowed_services = ["postgres:*", "redis:*", "mysql:*"]
    volumes = [
      "/var/run/docker.sock:/var/run/docker.sock",
      "/cache:/cache"
    ]
    
  [runners.cache]
    Type = "s3"
    Path = "runner-cache"
    Shared = true
    [runners.cache.s3]
      ServerAddress = "s3.amazonaws.com"
      BucketName = "gitlab-runner-cache"
      BucketLocation = "us-east-1"

Kubernetes Runner完整配置 #

toml
concurrent = 10
check_interval = 3

[[runners]]
  name = "kubernetes-runner"
  url = "https://gitlab.example.com"
  token = "YOUR_TOKEN"
  executor = "kubernetes"
  
  [runners.kubernetes]
    namespace = "gitlab-runner"
    image = "node:18"
    cpu_limit = "1"
    memory_limit = "2Gi"
    service_account = "gitlab-runner"
    
    node_selector = {
      "kubernetes.io/os": "linux"
    }
    
  [runners.cache]
    Type = "s3"
    Path = "runner-cache"
    [runners.cache.s3]
      ServerAddress = "s3.amazonaws.com"
      BucketName = "gitlab-runner-cache"

下一步 #

现在你已经掌握了Runner配置,接下来让我们学习 Runner高级

最后更新:2026-03-28