1. GitLab CI简介与版本说明
GitLab CI是GitLab内置的持续集成和持续交付(CI/CD)工具,通过GitLab Runner执行构建任务。GitLab Runner是独立的应用程序,可以在不同操作系统上运行。更多学习教程www.fgedu.net.cn
GitLab Runner最新版本:
GitLab Runner 17.9.1(2026年3月发布)
GitLab Runner 17.8.3(2026年2月发布)
GitLab Runner 17.7.2(2026年1月发布)
GitLab Runner 17.6.3(2025年12月发布)
GitLab Runner类型:
Shared Runners:共享Runner,所有项目都可以使用
Group Runners:组Runner,特定组内的项目可以使用
Specific Runners:专用Runner,仅特定项目可以使用
2. GitLab Runner下载地址
GitLab Runner支持多种操作系统和架构,可以从官方下载页面获取对应版本。学习交流加群风哥微信: itpux-com
官方下载地址:
GitLab Runner官方下载页面:https://docs.gitlab.com/runner/install/
GitLab Runner二进制文件:https://gitlab.com/gitlab-org/gitlab-runner/-/releases
GitLab Runner源码仓库:https://gitlab.com/gitlab-org/gitlab-runner
$ curl -s “https://gitlab.com/api/v4/projects/gitlab-org%2Fgitlab-runner/releases” | head -50
输出示例如下:
[
{
“name”: “v17.9.1”,
“tag_name”: “v17.9.1”,
“description”: “GitLab Runner 17.9.1 release”,
“created_at”: “2026-03-15T10:00:00.000Z”,
“released_at”: “2026-03-15T10:00:00.000Z”,
“assets”: {
“links”: [
{
“name”: “gitlab-runner-linux-amd64”,
“url”: “https://gitlab-runner-downloads.s3.amazonaws.com/v17.9.1/binaries/gitlab-runner-linux-amd64”
}
]
}
}
]
各平台下载链接:
Linux x86-64:https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
Linux ARM64:https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-arm64
macOS:https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-darwin-amd64
Windows:https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-windows-amd64.exe
3. Linux系统安装GitLab Runner
在Linux系统上安装GitLab Runner有多种方式,包括使用包管理器、二进制文件安装等。风哥提示:生产环境建议使用包管理器安装,便于后续升级维护。
步骤1:使用yum安装(CentOS/RHEL/Oracle Linux)
# curl -L “https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh” | sudo bash
输出示例如下:
Detected operating system as ol/8.
Checking for curl…
Detected curl…
Downloading repository file: https://packages.gitlab.com/install/repositories/runner/gitlab-runner/config_file.repo?os=ol&dist=8&source=script
done.
Installing pygpgme to verify GPG signatures…
Importing GPG key 0x51312f3f:
Userid : “GitLab B.V. (package repository signing key)
Fingerprint: F640 3F65 44A3 8863 DAA0 B6E0 3F01 618A 5131 2F3F
From : https://packages.gitlab.com/gpg.key
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : pygpgme-0.3-21.el8.x86_64 1/1
Verifying : pygpgme-0.3-21.el8.x86_64 1/1
Installed:
pygpgme-0.3-21.el8.x86_64
Complete!
The repository is setup! You can now install packages.
# 安装GitLab Runner
# yum install -y gitlab-runner
输出示例如下:
Last metadata expiration check: 0:00:15 ago on Sat Apr 4 10:00:00 2026.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
gitlab-runner x86_64 17.9.1-1 runner_gitlab-runner 45 M
Transaction Summary
================================================================================
Install 1 Package
Total download size: 45 M
Installed size: 120 M
Downloading Packages:
gitlab-runner-17.9.1-1.x86_64.rpm 12 MB/s | 45 MB 00:03
——————————————————————————–
Total 12 MB/s | 45 MB 00:03
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Running scriptlet: gitlab-runner-17.9.1-1.x86_64 1/1
Installing : gitlab-runner-17.9.1-1.x86_64 1/1
Running scriptlet: gitlab-runner-17.9.1-1.x86_64 1/1
Created symlink /etc/systemd/system/multi-user.target.wants/gitlab-runner.service → /usr/lib/systemd/system/gitlab-runner.service.
Verifying : gitlab-runner-17.9.1-1.x86_64 1/1
Installed:
gitlab-runner-17.9.1-1.x86_64
Complete!
步骤2:使用apt安装(Debian/Ubuntu)
# curl -L “https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh” | sudo bash
输出示例如下:
Detected operating system as Ubuntu/22.04.
Checking for curl…
Detected curl…
Downloading repository file: https://packages.gitlab.com/install/repositories/runner/gitlab-runner/config_file.list?os=Ubuntu&dist=22.04&source=script
done.
Installing pygpgme to verify GPG signatures…
…
The repository is setup! You can now install packages.
# 安装GitLab Runner
# apt-get install -y gitlab-runner
输出示例如下:
Reading package lists… Done
Building dependency tree… Done
Reading state information… Done
The following NEW packages will be installed:
gitlab-runner
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 45.0 MB of archives.
After this operation, 120 MB of additional disk space will be used.
Get:1 https://packages.gitlab.com/runner/gitlab-runner/ubuntu jammy/main amd64 gitlab-runner amd64 17.9.1 [45.0 MB]
Fetched 45.0 MB in 5s (9000 kB/s)
Selecting previously unselected package gitlab-runner.
(Reading database … 150000 files and directories currently installed.)
Preparing to unpack …/gitlab-runner_17.9.1_amd64.deb …
Unpacking gitlab-runner (17.9.1) …
Setting up gitlab-runner (17.9.1) …
GitLab Runner: creating gitlab-runner user…
GitLab Runner: installing service…
Created symlink /etc/systemd/system/multi-user.target.wants/gitlab-runner.service → /lib/systemd/system/gitlab-runner.service.
Processing triggers for man-db (2.10.2-1) …
步骤3:二进制文件安装
# curl -L –output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
输出示例如下:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 45.0M 100 45.0M 0 0 12.5M 0 0:00:03 0:00:03 –:–:– 12.5M
# 添加执行权限
# chmod +x /usr/local/bin/gitlab-runner
# 创建gitlab-runner用户
# useradd –comment ‘GitLab Runner’ –create-home gitlab-runner –shell /bin/bash
# 创建Systemd服务
# cat > /etc/systemd/system/gitlab-runner.service << 'EOF'
[Unit]
Description=GitLab Runner
After=syslog.target network.target
[Service]
User=gitlab-runner
Group=gitlab-runner
ExecStart=/usr/local/bin/gitlab-runner run
WorkingDirectory=/home/gitlab-runner
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
# 启动服务
# systemctl daemon-reload
# systemctl start gitlab-runner
# systemctl enable gitlab-runner
输出示例如下:
Created symlink /etc/systemd/system/multi-user.target.wants/gitlab-runner.service → /etc/systemd/system/gitlab-runner.service.
# 验证安装
# gitlab-runner --version
输出示例如下:
Version: 17.9.1
Git revision: abc123def
Git branch: 17-9-stable
GO version: go1.21.8
Built: 2026-03-15T10:00:00+0000
OS/Arch: linux/amd64
4. Docker方式部署GitLab Runner
使用Docker部署GitLab Runner是生产环境常用的方式,便于管理和升级。学习交流加群风哥QQ113257174
步骤1:拉取GitLab Runner镜像
# docker pull gitlab/gitlab-runner:latest
输出示例如下:
latest: Pulling from gitlab/gitlab-runner
Digest: sha256:abc123def456…
Status: Downloaded newer image for gitlab/gitlab-runner:latest
docker.io/gitlab/gitlab-runner:latest
# 拉取指定版本镜像
# docker pull gitlab/gitlab-runner:v17.9.1
输出示例如下:
v17.9.1: Pulling from gitlab/gitlab-runner
7a9868e23051: Pull complete
…
Digest: sha256:abc123def456…
Status: Downloaded newer image for gitlab/gitlab-runner:v17.9.1
docker.io/gitlab/gitlab-runner:v17.9.1
# 查看本地镜像
# docker images | grep gitlab-runner
输出示例如下:
gitlab/gitlab-runner latest abc123def456 2 days ago 500MB
gitlab/gitlab-runner v17.9.1 abc123def456 2 days ago 500MB
步骤2:创建GitLab Runner容器
# mkdir -p /fgedudb/gitlab-runner/config
# 启动GitLab Runner容器
# docker run -d –name gitlab-runner –restart always \
-v /fgedudb/gitlab-runner/config:/etc/gitlab-runner \
-v /var/run/docker.sock:/var/run/docker.sock \
gitlab/gitlab-runner:latest
输出示例如下:
Unable to find image ‘gitlab/gitlab-runner:latest’ locally
latest: Pulling from gitlab/gitlab-runner
…
Digest: sha256:abc123def456…
Status: Downloaded newer image for gitlab/gitlab-runner:latest
f8e7d8a9c0b1a2e3f4a5b6c7d8e9f0a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7
# 查看容器状态
# docker ps | grep gitlab-runner
输出示例如下:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f8e7d8a9c0b1 gitlab/gitlab-runner:latest “/usr/bin/dumb-init …” 10 seconds ago Up 9 seconds gitlab-runner
步骤3:使用Docker Compose部署
# cat > /fgedudb/gitlab-runner/docker-compose.yml << 'EOF' version: '3.8' services: gitlab-runner: image: gitlab/gitlab-runner:v17.9.1 container_name: gitlab-runner restart: always volumes: - ./config:/etc/gitlab-runner - /var/run/docker.sock:/var/run/docker.sock environment: - TZ=Asia/Shanghai logging: driver: "json-file" options: max-size: "100m" max-file: "3" EOF # 启动服务 # cd /fgedudb/gitlab-runner # docker-compose up -d 输出示例如下: Creating network "gitlab-runner_default" with the default driver Creating gitlab-runner ... done # 查看服务状态 # docker-compose ps 输出示例如下: Name Command State Ports ------------------------------------------------------------ gitlab-runner /usr/bin/dumb-init /entryp... Up
5. GitLab Runner注册配置
GitLab Runner需要注册到GitLab实例才能接收构建任务。更多学习教程公众号风哥教程itpux_com
步骤1:获取注册Token
# 项目级别:Settings -> CI/CD -> Runners -> New project runner
# 组级别:Settings -> CI/CD -> Runners -> New group runner
# 实例级别:Admin Area -> Settings -> CI/CD -> Runners
# Token格式示例
GR1348941xxxxxxxxxxxx
# 或使用API获取Token
$ curl –request POST “https://gitlab.fgedu.net.cn/api/v4/runners” \
–header “PRIVATE-TOKEN: your_personal_access_token” \
–data “runner_type=project_type” \
–data “project_id=123” \
–data “paused=false” \
–data “locked=false” \
–data “run_untagged=true” \
–data “tag_list=docker,build,deploy”
输出示例如下:
{
“id”: 12345,
“token”: “GR1348941xxxxxxxxxxxx”,
“token_expires_at”: null
}
步骤2:注册Runner
# gitlab-runner register
输出示例如下:
Runtime platform arch=amd64 os=linux pid=12345 revision=abc123def version=17.9.1
Running in system-mode.
Enter the GitLab instance URL (for example, https://gitlab.com/):
https://gitlab.fgedu.net.cn
Enter the registration token:
GR1348941xxxxxxxxxxxx
Enter a description for the runner:
[fgedu-server]: fgedu-docker-runner
Enter tags for the runner (comma-separated):
docker,build,deploy
Enter optional maintenance note for the runner:
WARNING: Support for registration tokens and runner parameters in the ‘register’ command is deprecated
The ‘register’ command is deprecated and will be removed in GitLab Runner 18.0
Use ‘gitlab-runner register –help’ for more information.
Registering runner… succeeded runner=GR1348941
Runner registered successfully. Feel free to start it, but if it’s running already the config should be automatically reloaded!
Configuration (with the authentication token) was saved to “/etc/gitlab-runner/config.toml”
# 非交互式注册
# gitlab-runner register \
–non-interactive \
–url “https://gitlab.fgedu.net.cn” \
–token “GR1348941xxxxxxxxxxxx” \
–executor “docker” \
–docker-image alpine:latest \
–description “fgedu-docker-runner” \
–tag-list “docker,build,deploy” \
–run-untagged=”true”
输出示例如下:
Runtime platform arch=amd64 os=linux pid=12346 revision=abc123def version=17.9.1
Running in system-mode.
Registering runner… succeeded runner=GR1348941
Runner registered successfully. Feel free to start it, but if it’s running already the config should be automatically reloaded!
Configuration (with the authentication token) was saved to “/etc/gitlab-runner/config.toml”
步骤3:验证Runner状态
# gitlab-runner list
输出示例如下:
Listing configured runners ConfigFile=/etc/gitlab-runner/config.toml
fgedu-docker-runner Executor=docker Token=GR1348941 URL=https://gitlab.fgedu.net.cn
# 验证Runner配置
# gitlab-runner verify
输出示例如下:
Runtime platform arch=amd64 os=linux pid=12347 revision=abc123def version=17.9.1
Running in system-mode.
Verifying runner… is alive runner=GR1348941
# 查看Runner详细信息
# gitlab-runner status
输出示例如下:
Runtime platform arch=amd64 os=linux pid=12348 revision=abc123def version=17.9.1
Service is running
6. GitLab CI Pipeline实战
GitLab CI使用.gitlab-ci.yml文件定义Pipeline,支持多阶段构建和部署。风哥提示:Pipeline配置文件应提交到代码仓库根目录。
步骤1:创建.gitlab-ci.yml文件
$ cat > .gitlab-ci.yml << 'EOF' stages: - build - test - docker - deploy variables: MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository" DOCKER_REGISTRY: "192.168.1.51:5000" IMAGE_NAME: "${DOCKER_REGISTRY}/fgedu-app" IMAGE_TAG: "${CI_COMMIT_SHORT_SHA}" cache: paths: - .m2/repository/ build: stage: build image: maven:3.9.6-eclipse-temurin-17 script: - echo "Building application..." - mvn clean package -DskipTests artifacts: paths: - target/*.jar expire_in: 1 hour test: stage: test image: maven:3.9.6-eclipse-temurin-17 script: - echo "Running tests..." - mvn test artifacts: reports: junit: - target/surefire-reports/TEST-*.xml expire_in: 1 week docker-build: stage: docker image: docker:latest services: - docker:dind script: - echo "Building Docker image..." - docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${DOCKER_REGISTRY} - docker build -t ${IMAGE_NAME}:${IMAGE_TAG} . - docker tag ${IMAGE_NAME}:${IMAGE_TAG} ${IMAGE_NAME}:latest - docker push ${IMAGE_NAME}:${IMAGE_TAG} - docker push ${IMAGE_NAME}:latest only: - main deploy: stage: deploy image: bitnami/kubectl:latest script: - echo "Deploying to Kubernetes..." - kubectl set image deployment/fgedu-app fgedu-app=${IMAGE_NAME}:${IMAGE_TAG} -n default only: - main when: manual EOF # 提交到Git仓库 $ git add .gitlab-ci.yml $ git commit -m "Add GitLab CI Pipeline" 输出示例如下: [main abc123d] Add GitLab CI Pipeline 1 file changed, 60 insertions(+) create mode 100644 .gitlab-ci.yml $ git push origin main 输出示例如下: Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 8 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 1.20 KiB | 1.20 MiB/s, done. Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 To https://gitlab.fgedu.net.cn/fgedu/fgedu-app.git def456a..abc123d main -> main
步骤2:查看Pipeline运行状态
$ glab pipeline list
输出示例如下:
Showing pipeline 123 of 123 on fgedu/fgedu-app (main)
#123: Add GitLab CI Pipeline – success
Status: passed
Source: push
Ref: main
SHA: abc123def456…
Created: 2026-04-04T10:00:00+08:00
Duration: 5m30s
# 查看Pipeline详细信息
$ glab pipeline view 123
输出示例如下:
Pipeline #123
Status: passed
Duration: 5m30s
Created: 2026-04-04T10:00:00+08:00
Jobs:
build (passed) – 2m15s
test (passed) – 1m45s
docker-build (passed) – 1m00s
deploy (manual) – pending
7. GitLab Runner生产环境配置
生产环境需要对GitLab Runner进行优化配置,确保稳定性和性能。from:www.itpux.com
步骤1:配置Runner并发和限制
# vi /etc/gitlab-runner/config.toml
# 配置示例
concurrent = 10
check_interval = 0
connection_max_age = “15m”
shutdown_timeout = 0
[session_server]
session_timeout = 1800
[[runners]]
name = “fgedu-docker-runner”
url = “https://gitlab.fgedu.net.cn”
id = 12345
token = “GR1348941xxxxxxxxxxxx”
executor = “docker”
limit = 5
[runners.docker]
image = “alpine:latest”
privileged = true
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
volumes = [“/cache”, “/var/run/docker.sock:/var/run/docker.sock”]
shm_size = 0
pull_policy = [“if-not-present”]
allowed_images = [“alpine:*”, “maven:*”, “docker:*”]
[runners.cache]
Type = “s3”
Path = “runner”
Shared = true
[runners.cache.s3]
ServerAddress = “s3.fgedu.net.cn”
BucketName = “gitlab-runner-cache”
AccessKey = “your-access-key”
SecretKey = “your-secret-key”
Insecure = false
步骤2:配置Runner监控
# vi /etc/gitlab-runner/config.toml
# 添加监控配置
listen_address = “:9252″
# 重启Runner服务
# systemctl restart gitlab-runner
# 验证监控端点
# curl http://localhost:9252/metrics | head -20
输出示例如下:
# HELP gitlab_runner_api_request_statuses_total The total number of api requests, partitioned by runner, status and endpoint.
# TYPE gitlab_runner_api_request_statuses_total counter
gitlab_runner_api_request_statuses_total{endpoint=”jobs”,runner=”fgedu-docker-runner”,status=”201″} 150
gitlab_runner_api_request_statuses_total{endpoint=”jobs”,runner=”fgedu-docker-runner”,status=”403″} 0
# HELP gitlab_runner_autoscaling_machine_creation_duration_seconds Duration of machine creation during autoscaling
# TYPE gitlab_runner_autoscaling_machine_creation_duration_seconds summary
gitlab_runner_autoscaling_machine_creation_duration_seconds{runner=”fgedu-docker-runner”} 0
# HELP gitlab_runner_autoscaling_machine_states The current number of machines per state in this provider.
# TYPE gitlab_runner_autoscaling_machine_states gauge
gitlab_runner_autoscaling_machine_states{runner=”fgedu-docker-runner”,state=”acquired”} 0
gitlab_runner_autoscaling_machine_states{runner=”fgedu-docker-runner”,state=”creating”} 0
gitlab_runner_autoscaling_machine_states{runner=”fgedu-docker-runner”,state=”idle”} 2
gitlab_runner_autoscaling_machine_states{runner=”fgedu-docker-runner”,state=”used”} 3
步骤3:配置日志和审计
# vi /etc/systemd/system/gitlab-runner.service
[Unit]
Description=GitLab Runner
After=syslog.target network.target
[Service]
User=gitlab-runner
Group=gitlab-runner
ExecStart=/usr/bin/gitlab-runner run –working-directory /home/gitlab-runner –config /etc/gitlab-runner/config.toml
WorkingDirectory=/home/gitlab-runner
Restart=always
RestartSec=5
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=gitlab-runner
[Install]
WantedBy=multi-user.target
# 重新加载配置
# systemctl daemon-reload
# systemctl restart gitlab-runner
# 查看日志
# journalctl -u gitlab-runner -f
输出示例如下:
Apr 04 10:00:00 fgedu-server gitlab-runner[12345]: Runtime platform arch=amd64 os=linux pid=12345 revision=abc123def version=17.9.1
Apr 04 10:00:00 fgedu-server gitlab-runner[12345]: Starting multi-runner from /etc/gitlab-runner/config.toml…
Apr 04 10:00:00 fgedu-server gitlab-runner[12345]: Configuration loaded builds=0
Apr 04 10:00:00 fgedu-server gitlab-runner[12345]: Listen address for metrics server :9252
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
