💡 为什么本地搭建
有时候我们对公司远端Gitlab Server的主分支(main)没有权限。在调试 CI/CD 时,我们需要编辑 .gitlab-ci.yml
,然后提交,再进行合并,才能查看 Pipeline 的执行结果。
GitLab Runner 有时候显示“等待中(Pending)”,这通常是因为资源有限,其他人也有并行的 Pipeline 任务。
在 Windows 上创建 GitLab 环境和 GitLab Runner 并不容易。
由于我们一般不使用 Linux 发行版作为主操作系统,WSL 是一个很好的解决方案。
🛠️ 使用 WSL 和本地 GitLab Runner 搭建 GitLab CI/CD
📦 前提条件
需要安装 WSL(例如:Ubuntu 22.04.5 LTS)。
安装所需的软件包:
sudo apt update
sudo apt install -y curl openssh-server ca-certificates tzdata perl
sudo apt install -y postfix
🔧 安装 GitLab CE
添加 GitLab 仓库
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
安装 GitLab CE
sudo EXTERNAL_URL="http://localhost" apt-get install gitlab-ce
启动并配置 GitLab
sudo gitlab-ctl reconfigure
sudo gitlab-ctl start
📌 默认 root
用户的密码保存在:/etc/gitlab/initial_root_password
🔑 可以在以下页面更改密码:http://localhost/-/user_settings/password/edit
→ 编辑个人资料 → 密码
这让你成为 GitLab 的 root 用户。
对于快速测试来说,不再会遇到权限问题,非常方便。

⚙️ 安装 GitLab Runner
添加 GitLab Runner 仓库
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
安装 GitLab Runner
sudo apt-get install gitlab-runner
🚀 注册 GitLab Runner
- 打开:
CI/CD > Runners > Create a new runner
- 执行以下命令:
gitlab-runner register --url http://localhost --token <yourToken>
✅ 配置已保存至:/etc/gitlab-runner/config.toml
验证 Runner 是否正常
sudo gitlab-runner verify
🌐 查看所有 Runner:
http://localhost/admin/runners

🌍 使用 IP 访问 GitLab
查找本机 IP 地址
hostname -I
示例输出:
172.24.208.12
修改 GitLab 配置文件
sudo vi /etc/gitlab/gitlab.rb
将以下行修改为你的 IP 地址:
external_url 'http://172.24.208.12'
重新配置 GitLab
sudo gitlab-ctl reconfigure
🔁 之后如果Pipeline任务失败可以手动点击 “Retry” 重新运行 Pipeline。