Github Actions未用rbenv指定Ruby版本,仍使用系统2.6.x版本问题
在MacStadium自托管Github Actions runner上执行fastlane时,明明已经用rbenv配置了Ruby 2.7.x版本,但Actions始终调用系统默认的Ruby 2.6.10,导致依赖安装报错:
Warning: the running version of Bundler (2.2.31) is older than the version that created the lockfile (2.4.22). We suggest you to upgrade to the version that created the lockfile by running
gem install bundler:2.4.22.
Ignoring digest-crc-0.6.4 because its extensions are not built. Try: gem pristine digest-crc --version 0.6.4
Ignoring unf_ext-0.0.8.2 because its extensions are not built. Try: gem pristine unf_ext --version 0.0.8.2
Fetching gem metadata from https://rubygems.org/.......
drb-2.2.1 requires ruby version >= 2.7.0, which is incompatible with the current
version, ruby 2.6.10p210
本地终端验证rbenv配置完全正常:
which ruby返回/Users/administrator/.rbenv/shims/rubyruby -v返回ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [arm64-darwin23]bundle env确认Bundler和Ruby版本匹配2.7.4- PATH环境变量已把
/Users/administrator/.rbenv/shims放在最前面
当前工作流及脚本:
- 工作流
deploy-ios使用自托管runner,执行yarn clean:ios脚本 clean:ios脚本包含cd ios && bundle update && bundle install && bundle exec pod install逻辑
1. 工作流中显式初始化rbenv
Github Actions的runner环境不会自动加载用户shell的rbenv配置,得手动在工作流里初始化:
在Use Node.js步骤后添加这段:
- name: 初始化rbenv run: | eval "$(rbenv init -)" rbenv global 2.7.4
或者直接在执行脚本前加载rbenv:
- name: 清理并安装Pod依赖 run: | eval "$(rbenv init -)" rbenv local 2.7.4 yarn clean:ios
2. 用登录shell执行脚本
自托管runner默认可能用非登录shell,不会读取~/.bash_profile或~/.zshrc里的rbenv配置。可以指定用登录shell执行脚本:
- name: 清理并安装Pod依赖 shell: bash -l {0} run: yarn clean:ios
bash -l会触发登录shell,自动加载你已经配置好的rbenv初始化脚本(前提是你已经在~/.bash_profile里加了eval "$(rbenv init -)")。
3. 直接指定bundle的绝对路径
如果上面的方法都不行,干脆在脚本里直接用rbenv管理的bundle路径,绕过shell的路径解析:
修改package.json里的clean:ios脚本:
"clean:ios": "yarn cache clean && yarn install && cd ios && rm -rf Pods && rm -rf Build && /Users/administrator/.rbenv/shims/bundle update && /Users/administrator/.rbenv/shims/bundle install && /Users/administrator/.rbenv/shims/bundle exec pod install && cd .."
4. 检查并重置PATH变量
确认Github Actions没覆盖PATH。可以先在工作流里加个步骤打印PATH,看看rbenv shims路径是不是还在最前面:
- name: 打印PATH run: echo $PATH
如果PATH被修改了,手动重置:
- name: 设置正确的PATH run: echo "/Users/administrator/.rbenv/shims:$PATH" >> $GITHUB_PATH
内容的提问来源于stack exchange,提问作者Olivier

