移动开发中的 Ruby:Fastlane、CI/CD 和自动化

作者: IT Sectr 发布日期: 2026-02-11 阅读时间: 8 分钟

Ruby — 一种动态解释型语言,语法优雅,以 Ruby on Rails 框架闻名。在移动开发中,Ruby 主要通过 Fastlane 使用 — 用于自动化构建、测试和部署 iOS 和 Android 应用的标准工具。根据 Fastlane 官方文档,该工具在全球超过 100,000 个项目中使用。

要点

  • Ruby — 一种动态语言,在移动开发中通过 Fastlane 用于构建自动化
  • Fastlane — iOS 和 Android 开发中 CI/CD 的事实标准,用 Ruby 编写
  • Fastfile — 使用类似 Ruby 的 DSL 编写的配置文件,描述构建过程
  • Match 和 Deliver — 用于管理证书和在 App Store 发布的 Fastlane actions
  • Ruby Gems — 包管理器,允许通过插件扩展 Fastlane

什么是 Ruby?

Ruby — 一种开源动态解释型语言,由松本行弘于 1995 年创建。Ruby 的基本原则 — «最优简单性»(最小意外原则)。该语言以闭包、纯面向对象方法(一切都是对象,包括数字)和强大的元编程系统而闻名。

Ruby 在 YARV(Yet Another Ruby VM)解释器上运行,从 3.1 版本开始支持 JIT 编译。当前版本 Ruby 3.4(2025)包含装饰器、改进的 Prism 解析器以及通过 RBS 的实验性类型支持。RubyGems — 包管理器 — 包含超过 200,000 个 gems。

Ruby on Rails — Ruby 中最著名的 Web 框架,但在移动开发中 Ruby 的使用方式不同。主要领域 — Fastlane:用于自动化移动应用程序生命周期所有阶段的 Ruby 脚本和 gems 生态系统:从证书签名到在商店发布。

Fastlane 的 Ruby 语法基础

使用 Fastlane 只需要对 Ruby 有基本的了解:方法、do-end 块、符号 (:symbol) 和哈希语法 { key: value }。与 Python 不同,Ruby 使用 end 来关闭块,而不是缩进。方法可以不带括号调用 — 这在 Fastlane DSL 中被积极使用。

ruby
# Fastlane 中使用的基本 Ruby 语法
def download_assets(version, platform: "ios")
  paths = {
    ios: "ios/Assets.car",
    android: "android/app/src/main/assets"
  }
  url = "https://cdn.example.com/assets/#{version}/#{paths[platform.to_sym]}"
  puts "Downloading from: #{url}"

  unless File.exist?("temp/assets")
    Dir.mkdir("temp/assets")
  end
  result = system("curl -o temp/assets.zip #{url}")
  raise "Download failed" unless result
end

download_assets("1.2.3", platform: "android")

在示例中,download_assets 方法接受参数 version 和可选的命名参数 platform(默认为 ios)。通过 #{} 进行的字符串插值将值直接嵌入到字符串中。unless — if 的反义,当条件为假时执行代码。

Fastlane:在 Ruby 中自动化移动构建

Fastlane — 一个开源工具,用 Ruby 编写。通过 gem install fastlane 安装后,它提供一组命令来自动化移动开发人员最常规的任务:创建截图、代码签名、在 TestFlight 和 Google Play Console 中发布。

Fastlane 由工具组成:sigh(证书管理)、match(团队中证书同步)、gym(iOS 构建)、gradle(Android 构建)、deliver(iOS 发布)、supply(Android 发布)、snapshot(截图)、scan(测试)。所有工具都可以从一个 Fastfile 访问。

在大型项目中,Fastlane 在一个管道中处理 50 多个操作:版本递增、从 CDN 下载资源、构建、签名、上传到 App Store Connect、发送通知到 Slack。整个管道通过一个命令执行:fastlane release。

Fastfile:构建管道的配置

Fastfile — 使用类似 Ruby 的 DSL 编写的文件,描述一系列操作(lane)。每个 lane 是一个独立的任务:beta(用于测试的构建)、release(发布)、screenshots(生成截图)。Lane 可以调用其他 lane 并传递参数。

ruby
# Fastfile — iOS 应用程序构建管道的配置
fastlane_version "2.220.0"

default_platform :ios

platform :ios do
  desc "TestFlight 构建的编译和部署"
  lane :beta do |options|
    increment_build_number(
      build_number: options[:build]
    )
    match(type: :appstore)
    gym(
      scheme: "MyApp",
      export_method: "app-store",
      configuration: "Release"
    )
    upload_to_testflight(
      app_identifier: "com.example.myapp"
    )
    slack(
      message: "构建 #{options[:build]} 已上传到 TestFlight"
    )
  end
end

Beta lane 按顺序执行:递增构建编号(increment_build_number)、同步证书(match)、编译存档(gym)、上传到 TestFlight(upload_to_testflight)和发送通知到 Slack。参数 :build 作为命名参数传递给 lane。

Fastfile 中的 Android 配置

Fastlane 在一个 Fastfile 中支持两个平台。对于 Android,在 platform :android 内部使用带有 gradle、supply 和 google_play_track_version_codes 操作的 lane。

ruby
platform :android do
  desc "APK 构建和在 Google Play Internal Testing 中发布"
  lane :beta do
    gradle(
      task: "assemble",
      build_type: "Release",
      flavor: "Production"
    )
    supply(
      track: "internal",
      apk_paths: ["app/build/outputs/apk/release/app-release.apk"]
    )
  end
end

iOS 和 Android 的关键 Fastlane actions

Fastlane 包含超过 200 个内置操作。以下是移动开发中最重要的操作,按类别分组。

操作平台用途
matchiOS通过 Git 同步证书和配置文件
gymiOS编译 .app 和 .ipa 存档
scaniOS在模拟器上运行 UI 测试
snapshotiOS在不同设备上自动生成截图
deliveriOS将构建、截图和元数据上传到 App Store Connect
pilotiOS管理 TestFlight:上传构建、添加测试人员
gradleAndroid运行 Gradle 任务:assemble、test、bundle
supplyAndroid将 APK/AAB 和元数据上传到 Google Play Console
screengrabAndroid为 Google Play 自动截图
firebase_app_distribution两者将构建上传到 Firebase App Distribution

Match:安全的证书管理

match — 一个 Fastlane gem,用于在开发人员之间同步 iOS 证书和配置文件。所有机密都存储在加密的 Git 仓库或 S3 中。Match 在证书缺失时自动生成证书,并在过期时更新它们。

ruby
# 为新项目初始化 match
$ fastlane match init

# 创建和同步 App Store 证书
$ fastlane match appstore

# 为团队创建 Development 证书
$ fastlane match development

# 过期时更新所有证书
$ fastlane match --force

Match 使用只有开发人员知道的密码短语存储私钥。每个开发人员克隆证书仓库并运行 match 以安装当前配置文件。Apple 证书的有效期为 1 年,match 会自动续期。

Fastlane 与 CI/CD 系统的集成

Fastlane 与任何 CI/CD 平台集成:GitHub Actions、GitLab CI、CircleCI、Bitrise、Jenkins 和 Xcode Cloud(通过 xcode-build)。Fastlane 在 CI 上通过 fastlane <lane_name> 启动,并在任何步骤失败时返回错误代码。

GitHub Actions 的配置包括安装 Ruby、fastlane 并通过 bundle exec fastlane 启动 lane。iOS 构建需要 macOS runner。Android 可以使用 ubuntu-latest,这更便宜。

yaml
# .github/workflows/deploy.yml — 使用 Fastlane 的 CI/CD
name: Deploy Mobile App
on:
  push:
    branches: [main]

jobs:
  ios-deploy:
    runs-on: macos-14
    steps:
      - uses: actions/checkout@v4
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: "3.3"
      - run: bundle install
      - run: bundle exec fastlane beta
      - env:
          MATCH_PASSWORD: ${{ secrets.MATCH_PASSWORD }}

工作流在推送到 main 时启动。macOS runner 从 Fastfile 执行 bundle exec fastlane beta。MATCH_PASSWORD 通过 GitHub Secrets 传递以解密证书。在 lane 完成后,Slack 操作发送通知。

Ruby Gems 和 Fastlane 插件

RubyGems — Ruby 的包管理器。Fastlane 通过插件扩展 — 添加新操作的 gems。fastlane-plugin-versioning 插件管理版本,fastlane-plugin-firebase_app_distribution 将构建上传到 Firebase。GitHub 上有超过 500 个 Fastlane 插件。

项目根目录中的 Gemfile 固定所有依赖项的版本以确保构建的可重现性。bundle install 根据 Gemfile.lock 安装 gems。移动开发的主要 gems:fastlane、cocoapods(iOS 依赖项)、xcodeproj(处理 .xcodeproj)。

ruby
# 移动项目的 Gemfile
source "https://rubygems.org"

gem "fastlane", "~> 2.220"

plugins_path = File.join(File.dirname(__FILE__), 'fastlane', 'Pluginfile')
eval_gemfile(plugins_path) if File.exist?(plugins_path)

Fastlane 与替代方案:方法比较

Fastlane — 不是唯一的自动化工具。让我们根据移动团队重要的标准与主要替代方案进行比较。

标准Fastlane (Ruby)Bitrise (Web)Xcode Cloud (Apple)GitHub Actions (YAML)
灵活性最大(代码)中等(步骤)低(受限)
iOS完全支持完全完全(内置)通过自托管 macOS
Android完全支持完全完全
证书 (match)内置通过 Code Signing自动通过 fastlane
本地运行否(仅 CI)通过 act
插件500+200+ 集成20,000+ 操作

Fastlane 仍然是复杂管道最灵活的工具。Bitrise 适用于没有 Ruby 开发人员的团队。Xcode Cloud — 适用于简单的 iOS 项目。GitHub Actions — 如果团队已经在使用 GitHub。

常见问题

既然有 Kotlin 和 Swift,为什么移动开发中还要用 Ruby?

Ruby 不用于编写移动应用程序,而是用于通过 Fastlane 自动化构建、测试和部署过程。Fastlane 是 iOS 和 Android 开发中 CI/CD 的事实标准。

Fastlane 只在 macOS 上运行吗?

Fastlane 可在 macOS、Linux 和 Windows 上运行,但 iOS 构建需要 Xcode 和 macOS。Android 构建可在任何平台上执行。本地开发人员使用 macOS,在 CI 上 — iOS 使用 macOS,Android 使用 Linux。

使用 Fastlane 需要学习 Ruby 吗?

Ruby 的基本理解足以使用 Fastlane。Fastlane 在 Fastfile 中使用类似 Ruby 的 DSL。对于典型任务,了解 gems、do-end 块和基本语法就足够了。不需要深入了解 Ruby。

Fastlane 有哪些替代方案?

替代方案:Gradle 用于 Android(内置)、Xcode Cloud(Apple)、Bitrise(Web 界面)、GitHub Actions(YAML)。由于丰富的操作和插件生态系统,Fastlane 仍然是最灵活的工具。

Fastlane 适合大型团队吗?

是的,Fastlane 被大型公司使用。所有操作和配置文件都在仓库中进行版本控制,match 确保证书在仓库或 S3 中的安全存储。

总结

  • Ruby — 一种动态语言,在移动开发中通过 Fastlane 生态系统用于构建自动化
  • Fastlane — iOS 和 Android 的 CI/CD 标准:200+ 操作、500+ 插件、100,000+ 项目
  • Fastfile — 类似 Ruby 的 DSL,带有 lane:beta、release、screenshots 用于不同的构建场景
  • Match 解决了开发人员之间 iOS 证书和配置文件的同步问题
  • Deliver 和 Supply — 用于在 App Store 和 Google Play 发布带元数据和截图的 actions
  • CI/CD 集成 — Fastlane 在 GitHub Actions、GitLab CI、CircleCI 和 Bitrise 中运行
  • 灵活性 — Fastlane 凭借在 Ruby 中可编程的管道胜过 Bitrise 和 Xcode Cloud

我们将开发一款交钥匙移动应用程序

IT Sectr自2017年以来为初创企业和企业打造iOS和Android应用程序。我们将为您提供咨询并提出最佳解决方案。

讨论项目

另请阅读