Fastlane集成与使用-iOS

FastLane是一种能自动部署beta版和release版本的 iOS和Android App的工具(Ruby脚本的集合)。他可以处理生成截图、代码签名以及发布App等任务。

Fastlane的优势

  • 节省上传新版本到商店或测试服务的时间
  • 集成了当前开发环境中所的工具和服务
  • 100%开源
  • 使用简单,能根据提供的文档几分钟配置好
  • 基于你自己的App和数据,运行在你自己的电脑上
  • 集成几乎所有CI系统
  • 支持iOS、Mac、Android App
  • 你可以根据需要扩展和定制fastlane
  • 不需要记住出fastlane以外的任何命令
  • 可以在任何电脑上配置,包括CI服务器

安装Fastlane

确保你的电脑上已安装最新版本的Xcode命令行工具(Xcode command line tools)

xcode-select –install

安装fastlane

[sudo] gem install fastlane -NV

也可以使用

brew cask install fastlane

如果使用gem包管理器安装fastlane,首先需安装Ruby环境可以运行ruby –v查看是否安装,如果没有安装可参考我的另一篇文章安装Ruby环境中的安装Ruby
如果使用brew安装fastlane,首先需安装Homebrew,可以运行brew -v查看是否安装,如果没有安装可参考我的另一篇文章安装Ruby环境中的安装Homebrew

Fastlane使用

1. 初始化Fastlane

导航到你的工程目录下执行以下命令

fastlane init

以下是部分运行结果实例:
这里写图片描述
选择配置方式,根据提示完成配置
如果出现一下错误:
这里写图片描述
这时需要进入你的工程”Product”->”Scheme”->”Manage Scheme”,然后在对应的Scheme 的shared字段选中,如下图:
这里写图片描述
有可能会卡在”bundle update”这一步,不知道什么原因,但这时我们需要的主要的文件已经生成好了,因此我们可以关闭正在运行的控制台程序,新开一个继续进行下一步
如:选择”Automate App Store distribution”
这里写图片描述

2. 配置Appfile文件以及FastFile文件

Appfile:存储App公共信息,如:
这里写图片描述
更多关于Appfile的信息请阅读https://docs.fastlane.tools/advanced/#appfile
Fastfile:包含了要对你app操作的所有信息, 如:
这里写图片描述
要编译一个app,你只需要在Fastfile文件中定义一个lane,如下:

lane :release do  
  build_app(scheme: "MyApp") #编译App
end

以上代码中:

release为lane的名字,在执行lane的时候会用到,你可以为lane取任意的名字。

build_app用来编译app,你可以为他指定更多的的参数,如:

lane :release do
  build_app(scheme: "MyApp",
            workspace: "Example.xcworkspace",
            include_bitcode: true)
end

3. 执行lane

使用以下命令来执行这个lane:

fastlane release

如果执行完成,会在当前目录下生成[ProductName].ipa的文件
可以通过执行以下命令来查看build_app的所有支持的参数

fastlane action build_app

我们现在知道build_app 是fastlane用来编译app的action,当然fastlane还提供了其他action如:

  • 截图:capture_screenshots
  • 运行单元测试和UI测试:run_tests
  • 上传app至appstore:upload_to_app_store

如果要查询fastlane所有支持的actions,请访问 fastlane官方文档-Actions

参数传递

要从命令行向lane传递参数,使用以下语法:

fastlane [lane] key:value key2:value2
fastlane upload_testflight ipaPath:"./myapp.ipa"

要想访问这些参数,在定义lane的时候需要包含 |options|,如

desc "上传已知的ipa包至testflight"
lane :upload_testflight do |options|   #options获取传入的参数
  upload_to_testflight(
    ipa: args[:ipaPath], #ipa路径 ipaPath为传入的参数的名字
  )
end

添加 Credentials

你可以为fastlane添加Credentials,如:添加Apple ID 以及密码。

命令行方式

  • 添加
fastlane fastlane-credentials add --username [email protected]
  • 移除Credential
fastlane fastlane-credentials remove --username felix@krausefx.com

传入环境变量方式

你也可以通过环境变量FASTLANE_USER, FASTLANE_PASSWORD传入Credentials 如:

lane :release do |args|
    ENV["FASTLANE_USER"] = "[email protected]"
    ENV["FASTLANE_PASSWORD"] = "test123"
    upload_to_app_store(
      ipa: args[:ipa_path]
    )
end

以上方式会将[email protected]自动存入“钥匙链”,如果你不想存入“钥匙链”可以设置环境变量”FASTLANE_DONT_STORE_PASSWORD”为”1”,如下:

lane :release do |args|
    ENV["FASTLANE_USER"] = "[email protected]"
    ENV["FASTLANE_PASSWORD"] = "test123"
    ENV["FASTLANE_DONT_STORE_PASSWORD"] = "1"
    upload_to_app_store(
      ipa: args[:ipa_path]
    )
end

插件的使用

  • 查看所支持的插件
fastlane search_plugins
  • 查看某种插件
fastlane search_plugins [query] #query为插件名
  • 添加插件
fastlane add_plugin [name] #name 为插件名
  • 插件使用实例:上传ipa包至pyger

首先将添加pyger插件:fastlane add_plugin pyger
编辑FastFile,添加以下信息

desc "打包并上传测试版至蒲公英"
  lane :beta_pgyer do 
    #编译并导出ipa包
    gym(
clean: true,
scheme: “scheme_name”
export_method: "development",
   )
    #上传至蒲公英
    pgyer(
      api_key: "393c8071e8072d8c029205cf7924ca5a", 
      user_key: "c957c712b5e22bce2a2eef4125589195", 
    )
 end

执行:fastlane beta_pgyer

FastFile配置的实例:


#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#

# Uncomment the line if you want fastlane to automatically update itself
update_fastlane          # 设置是否自动更新 fastlane

default_platform(:ios)  # 设置默认的平台为ios

platform :ios do       #针对iOS平台的配置信息

  begin
      last_build = get_build_number.to_i #get_build_number,获取工程中的build number
    rescue
      puts "No Active Build using build nr. 0"
      last_build = 0
  end

  #scheme name,不同App需要修改
  scheme_name = "FastLaneDemo"
  #ipa存放路径
  ipaPath = "./ipas"

  desc "运行单元测试和UI测试"
  lane :tests do
    run_tests(scheme: scheme_name)
  end

  desc "打包并上传测试版至testflight"
  lane :beta_testflight do 
    now_build = last_build.to_i + 1
    #增加build
    increment_build_number(build_number: now_build)
    #编译App
    gym(
      clean: true,
      scheme: scheme_name,
      configuration: "Release",
      export_method: "ad-hoc",
      output_directory: ipaPath,
      output_name: "#{scheme_name}_#{now_build}",
       include_bitcode: false, #bitcode
      export_options: {   #指定配置文件等设置,如果在Xcode中配置好了可不传
      provisioningProfiles: { 
        "com.bestwise.fastlane.test.archieve" => "abbecb3e-7d7f-49eb-ad6a-be12a740b8b1", 
      }
    }
    )
    #上传至testFlight
    upload_to_testflight(
      skip_waiting_for_build_processing: true  #是否跳过等待apple处理的过程
    )
  end

  desc "打包并上传至app-store"
  lane :app_store do
    now_build = last_build + 1
    #增加build number
    increment_build_number(
      build_number: now_build
    )
    #导出ipa包
    gym(
      clean: true,
      scheme: scheme_name,
      export_method: "app-store",
      output_directory: "#{ipaPath}/release_app_store",
      output_name: "#{scheme_name}_#{now_build}"
    )
    #上传至app-store
    deliver(
      force: true,              #是否跳过HTML验证报告,默认false
      skip_metadata: true,      #是否跳过上传metadata,默认false
      skip_screenshots: true    #是否跳过上传屏幕截图,默认false
    )
  end

  desc "打包并上传测试版至蒲公英"
  lane :beta_pgyer do |args|       #args获取传入的参数
    puts args
    now_build = last_build + 1
    #增加build版本号
    increment_build_number(build_number: now_build)
    #编译并导出ipa包
    gym(
      clean: true,
      scheme: scheme_name,
      export_method: "development", #app-store、ad-hoc、development、enterprise
      output_directory: ipaPath,   #ipa包路径
      output_name: "#{scheme_name}_#{now_build}"   #ipa包名字
    )
    #上传至蒲公英
    pgyer(
          api_key: "393c8071e8072d8c029205cf7924ca5a",   #ipa_key, 可在蒲公英账户下查看
      user_key: "c957c712b5e22bce2a2eef4125589195",   #user_key, 可在蒲公英账户下查看
          update_description: args[:updateDesc] #更新描述,updateDesc为传入的参数的名字, 如:fastlane beta_pgyer updateDesc: "更新说明"
    )
  end


  desc "上传已知的ipa包至testflight"
  lane :upload_testflight do |args|   #args获取传入的参数
    upload_to_testflight(
      ipa: args[:ipaPath], #ipa路径 ipaPath为传入的参数的名字
      skip_waiting_for_build_processing: false #是否跳过等待apple 处理
    )
  end
end

参考文档

fastlane官方文档

猜你喜欢

转载自blog.csdn.net/u011656331/article/details/80708255