在持续集成CI/CD的搭建过程中GitLab Runner 是不可或缺的线下执行机。本文将结合最新版本v18.x的实战注册日志手把手带你完成从安装到注册成功的全过程并针对注册过程中的提示进行深度解析。Step 1下载与安装 GitLab Runner首先我们需要从官方下载适合自己系统架构的安装包。下载你可以访问官方GitLab Runner下载列表 获取最新版本的 RPM/DEB 包。对于大多数 Linux 企业服务器如 CentOS / Rocky Linux选择 amd64 架构的 rpm 包即可。命令行安装下载完成后在终端执行以下命令进行安装以 yum 为例# 安装主程序sudoyum localinstall-ygitlab-runner_x86_64.rpmsudoyum localinstall-ygitlab-runner-helper-images.rpmStep 2注册 Runner 交互式配置安装完成后我们需要运行注册命令让这台服务器与你的 GitLab 实例建立连接。执行以下命令开启注册流程sudogitlab-runner register完整注册日志与参数解析下面是实际注册过程中的标准交互日志我们逐一拆解每个参数该如何填写见图中2token见图中红框处# 1. 输入你的 GitLab 访问地址公网或内网 IPEnter the GitLab instance URL(forexample,https://gitlab.com/): http://10.16/# 2. 输入在 GitLab 后台获取的 Registration TokenEnter the registration token: zm7Lxxx# 3. 为这个 Runner 填写一个易懂的描述建议带上主机名或IPEnter a descriptionforthe runner:[host-10-19-195-108]: token_opt# 4. 设置标签Tags后续在 .gitlab-ci.yml 中可以通过 tags 指定在这个 Runner 上运行Enter tagsforthe runner(comma-separated): token# 5. 填写维护备注可选直接回车留空即可Enter optional maintenance noteforthe runner:# 【核心提示】这里会触发一段 Deprecated 警告提示未来的版本会全面改用 authentication tokens。# 现阶段看到 succeeded 证明已经成功连接到 GitLab 后端WARNING: Supportforregistration tokens and runner parameters in theregistercommand has been deprecated in GitLab Runner 15.6 and will be replaced with supportforauthentication tokens.Registering runner...succeeded correlation_idc734e07d...runnerzm7Lps5Xj# 6. 选择执行器Executor这里我们选择最基础直接的 shellEnter an executor: kubernetes,ssh,shell,docker...shell# 提示注册成功配置已自动保存。Runner registered successfully.Feel free tostartit,butifits running already the config should be automatically reloaded!Configuration(with the authentication token)was saved in/etc/gitlab-runner/config.tomlStep 3核心配置参数拆解在上述注册交互中有三个关键点需要特别注意关于 Deprecated 警告 ⚠️现象 注册时出现 WARNING: Support for registration tokens… has been deprecated…解析 很多同学看到这段红字以为注册失败了。其实不然只要下方出现了 Registering runner… succeeded就代表当前注册已经完全成功。这是 GitLab 官方提示在未来的大版本中会废弃旧版的 Registration Token 认证方式改用更安全的 Authentication Token 流程。现阶段18.x版本依然兼容不用担心。执行器Executor为什么选 Shell在最后一步中我们输入了 shell。Shell 执行器意味着 CI/CD 任务里的脚本比如 npm run build 或 python test.py会直接在当前这台宿主机上执行。优点配置简单可以直接利用宿主机上已经装好的工具链省去了容器间挂载的麻烦。注意点构建过程中产生的临时文件会污染宿主机环境需要注意权限管理。Step 4验证与启动注册完成后Runner 的所有配置都被加密转换并保存在了 /etc/gitlab-runner/config.toml 文件中。此时你可以通过以下命令检查 Runner 的运行状态# 查看服务状态sudogitlab-runner status现在回到你的 GitLab 后台Settings - CI/CD - Runners刷新页面你就能看到一个绿色的、带有 token 标签的物理 Runner 已经在静静等待接收它的第一个流水线任务了参考官方指南