在本地调试 GitLab CI Pipeline (WSL搭建GitLab Runner)

💡 为什么本地搭建

有时候我们对公司远端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

  1. 打开:CI/CD > Runners > Create a new runner
  2. 执行以下命令:
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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值