gitlab-runner那些坑

本文档记录了在部署GitLab Runner过程中遇到的问题及解决方案,包括安装、用户权限设置、注册Runner、配置.gitlab-ci.yml文件等。在遇到日志大小限制、Runner状态异常等问题时,通过修改配置文件和重启服务可以解决。同时,给出了如何移除、重置和重新注册Runner的方法。

gitlab-runner那些坑

最近部署gitlab-runner时候遇到了一些坑,随手记录下来

  1. 参考文献

gitlab之gitlab-ci自动部署
gitlab-ci配置

1.在本地安装runner

> curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.deb.sh | sudo bash
> sudo apt-get update
> sudo apt-get install gitlab-ci-multi-runner

2.重置git-runner用户权限

  • 一般设置为管理员权限,不然无法磁盘读写权限和系统使用权限;
> sudo gitlab-ci-multi-runner uninstall
> sudo gitlab-ci-multi-runner install -u (用户名)

3.注册一个可用的runner

在这里插入图片描述

> sudo gitlab-ci-multi-runner register

WARNING: Running in user-mode.                     
WARNING: The user-mode requires you to manually start builds processing: 
WARNING: $ gitlab-runner run                       
WARNING: Use sudo for system-mode:                 
WARNING: $ sudo gitlab-runner...                   
                                                   
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/): # <font color=#FF0000>填写Gitlab服务器地址 </font>
例如http://gitlab.example.com/
Please enter the gitlab-ci token for this runner:	# <font color=#FF0000>填写项目CI/CD中的令牌 </font>
d6jxxxxxxxxxxxxx
Please enter the gitlab-ci description for this runner: 	# <font color=#FF0000>填写runner名称</font>

runner_xxx

Please enter the gitlab-ci tags for this runner (comma separated):	# <font color=#FF0000>填写Runner的标签,用于和服务器进行匹配,和填多个,用","分割</font>
release      #标签用于后面分支运行时候使用的runner
Whether to run untagged builds [true/false]:	# <font color=#FF0000>是否允许无标签运行,默认false,建议不做修改 </font>

[false]: 

Whether to lock Runner to current project [true/false]:	# <font color=#FF0000>在运行时是否锁定项目,建议填true </font>

[false]: true

Registering runner... succeeded                     runner=d6jAc4iw
Please enter the executor: virtualbox, docker-ssh+machine, kubernetes, docker-ssh, ssh, shell, docker+machine, docker, parallels:	# <font color=#FF0000>填写Runner的执行方式 </font>
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 

查看git-runner状态和信息

> sudo gitlab-ci-multi-runner list #获取Runner列表
Listing configured runners                          ConfigFile=/etc/gitlab-runner/config.toml # 配置文件路径
runner_xxx                                                 Executor=shell Token=hx9xyRsYxxxxxxURL=http:XXXXXXX
> sudo gitlab-ci-multi-runner status #获取Service运行状态
gitlab-runner: Service is running. 

> sudo gedit /etc/systemd/system/gitlab-runner.service
... "--working-directory" "/media/lxx/SSD2T/work_idh/git_runner" ...
> systemctl daemon-reload # 重启加载
> systemctl restart gitlab-runner # 重启服务

仓库配置文件.gitlab-ci.yml

  • 在本地仓库的根目录下创建,项目git push时候就会触发
  • 在code根目录中,创建.gitlab-ci.yml文件
stages:
  - build     #构建一个项目

build:
  stage: build
  only:    #仅仅在此分支触发
    - xxxxx         #分支名字
  script:
    - make all     #运行脚本
  tags:
    - release     #runner的标签

问题

Job’s log exceeded limit of 4194304 bytes.

  1. 出现该问题主要是因为gitlab runner默认日志大小为4096,修改相关配置即可
> sudo gedit /etc/gitlab-runner/config.toml
 
concurrent = 1
check_interval = 0
 
[[runners]]
  name = "ci"
  url = "http://git.***.org/"
  token = "6f2xxxxxxxxxxxx"
  executor = "shell"
  builds_dir = "/mdata/builds"
  # 此行为新增行,默认配置为4096
  output_limit = 81920
  [runners.cache]
 
> sudo gitlab-ci-multi-runner restart #重启gitlab runner

  1. runner灰色下线无法找到runner标签,重启runner
 > sudo gitlab-runner restart

  1. runner一直等待中,一般是找不到可以使用匹配得上的runner
  • 解决
    1移除runner
    2.重置令牌
    3.删除/etc/gitlab-runner/config.toml配置
    也可以查看下home目录下有没有该配置,有的话也可以删除

    	> gedit ~/.gitlab-runner/config.toml
    
    	> sudo rm /etc/gitlab-runner/config.toml
    

    4.重新注册一个runner即可

       	> sudo gitlab-ci-multi-runner register
    
<think>嗯,用户遇到了一个典型的Windows CI/CD环境配置问题,报错显示GitLab Runner无法找到pwsh(PowerShell Core)可执行文件。这个问题其实挺常见的,尤其是当用户从传统PowerShell迁移到跨平台的PowerShell Core时。 用户提供的引用信息很有价值。从引用[1]来看,用户之前解决过类似PATH缺失的问题,说明对系统环境变量有一定了解;引用[2][3]显示用户正在配置Node.js和GitLab CI/CD,且可能踩过文档版本的;引用[4]的流程图则表明用户在部署多环境应用。 深层需求可能是:用户正在Windows Server上搭建.NET应用的CI/CD流水线,需要确保GitLab Runner能正确识别PowerShell Core作为默认shell。考虑到pwsh是.NET Core开发的重要工具,用户可能还在使用ASP.NET Core或Azure相关技术栈。 解决方案需要分三层处理: 第一层是基础路径修复,就像处理引用[1]的powershell.exe问题那样,但这次要定位pwsh.exe的安装位置。默认路径通常在C:\Program Files\PowerShell\7\,这个路径必须加入系统PATH。 第二层是GitLab Runner的配置适配。根据引用[3]的教训,必须严格参照英文文档。需要在config.toml里明确shell选项为"pwsh",而不是默认的"powershell"。这里有个细节:如果Runner以服务方式运行,需要重新注册服务才能加载新的环境变量。 第三层防御性措施很重要,在.gitlab-ci.yml里显式指定shell路径能避免环境差异。考虑到用户的多环境部署需求(引用[4]),建议在before_script里用Get-Command pwsh做预检查更稳妥。 突然想到一个容易忽略的点:32位/64位路径混淆。如果服务器是64位系统但装了32位pwsh,路径会是C:\Program Files (x86)\,而GitLab Runner默认按64位进程运行就可能找不到。最好在配置前用[Environment]::Is64BitProcess确认运行环境。 用户上次的问题显示ta已经具备CI/CD流程设计能力(引用[4]的流程图),所以提供方案时可以适当引入高级技巧,比如用$PROFILE检测PowerShell模块加载状态,这对未来调试其他问题也有帮助。 </think>以下是解决 Windows CI/CD 报错 `exec: "pwsh": executable file not found in %PATH%` 的完整方案,结合引用内容和系统配置逻辑: ### 原因分析 1. **PowerShell Core 未安装** `pwsh` 是 PowerShell Core(跨平台版本)的可执行命令,传统 Windows PowerShell 可执行文件是 `powershell.exe`。报错表明系统未识别 `pwsh` 命令。 2. **PATH 环境变量缺失** 即使安装了 PowerShell Core,若其安装路径未加入系统的 `PATH` 环境变量,GitLab Runner 仍无法定位可执行文件[^1]。 3. **GitLab Runner Shell 配置错误** Runner 的 `config.toml` 中可能错误配置了 Shell 类型(如设为 `pwsh` 但未正确安装)[^4]。 --- ### 解决步骤 #### 1. **安装 PowerShell Core** - 访问 [PowerShell 官网](https://aka.ms/powershell-release?tag=stable) 下载 **MSI 安装包** - 安装时勾选 **`Add to PATH`**(关键步骤!) ![](https://docs.microsoft.com/en-us/powershell/media/install-powershell-windows/mswin-msi-install-options.png?view=powershell-7.2) *(图示:确保勾选 "Add PowerShell to PATH")* #### 2. **验证安装** - 打开 **CMD** 或 **Git Bash**,执行: ```bash pwsh --version ``` 若显示版本号(如 `PowerShell 7.4.1`),则安装成功。 #### 3. **手动添加 PATH(若安装时遗漏)** - 按 `Win + R` 输入 `sysdm.cpl` → 高级 → 环境变量 - 在 **系统变量** 中找到 `Path` → 编辑 → 新增以下路径: ``` C:\Program Files\PowerShell\7 # 默认安装路径 ``` - **重启服务器或 Runner 服务** 使配置生效[^1][^4]。 #### 4. **配置 GitLab Runner** - 编辑 Runner 配置文件 `config.toml`(通常在 `C:\GitLab-Runner\`) - 确认 `shell` 配置与实际相符: ```toml [[runners]] name = "windows-runner" executor = "shell" shell = "pwsh" # 或改为 "powershell" 使用传统 PowerShell ``` - **重启 Runner 服务**: ```bash gitlab-runner restart ``` #### 5. **在 CI 脚本中显式指定路径(备用方案)** 在 `.gitlab-ci.yml` 中直接调用绝对路径: ```yaml job_name: script: - 'C:\Program Files\PowerShell\7\pwsh.exe -Command "Your-Script.ps1"' ``` #### 6. **验证 Runner Shell 环境** ```yaml test_path: script: - pwsh -c "Write-Host 'PS Core Available!'" - echo %PATH% # 检查 PATH 是否包含 PowerShell 路径 ``` --- ### 关键预防措施 1. **文档一致性** 严格遵循 [GitLab Runner Shell 配置英文文档](https://docs.gitlab.com/runner/shells/#powershell-core)(引用[3]的经验) 2. **Runner 服务权限** 若 Runner 以 **Windows 服务**运行,需在 **服务属性** → **登录** 选项卡中勾选 **`允许服务与桌面交互`**,确保加载用户环境变量[^4]。 3. **镜像预配置** 若使用虚拟机/容器,在镜像中预装 PowerShell Core 并固化 PATH。 > **引用说明**:PATH 配置和 Runner 服务交互问题关联引用[1][4];安装验证步骤参考引用[2];文档一致性强调引用[3]。 --- ### 常见问题排查表 | 现象 | 解决方案 | |-----------------------|-----------------------------| | 安装后仍报错 | 重启服务器或注销重新登录 | | 服务模式下 PATH 无效 | 为 Runner 服务账户手动添加 PATH | | 权限不足 | 以管理员身份安装 PowerShell | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值