GitLab CI|CD 配置笔记
在持续集成(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完整注册日志与参数解析
下面是实际注册过程中的标准交互日志,我们逐一拆解每个参数该如何填写:
- 见图中2
- token见图中红框处
# 1. 输入你的 GitLab 访问地址(公网或内网 IP)Enter the GitLab instance URL(forexample,https://gitlab.com/): http://10.16/# 2. 输入在 GitLab 后台获取的 Registration TokenEnter the registration token: zm7Lxxx# 3. 为这个 Runner 填写一个易懂的描述(建议带上主机名或IP)Enter 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 the'register'command has been deprecated in GitLab Runner 15.6 and will be replaced with supportforauthentication tokens.Registering runner...succeeded correlation_id=c734e07d...runner=zm7Lps5Xj# 6. 选择执行器(Executor),这里我们选择最基础直接的 shellEnter an executor: kubernetes,ssh,shell,docker...shell# 提示:注册成功!配置已自动保存。Runner registered successfully.Feel free tostartit,butifit's running already the config should be automatically reloaded!Configuration(with the authentication token)was saved in"/etc/gitlab-runner/config.toml"Step 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 已经在静静等待接收它的第一个流水线任务了!
参考:
官方指南
