Fastlane使用

简介

fastlane是一个命令行工具,帮助开发者完成一些自动化任务,比如打包应用,发布应用,管理证书配置文件等等。

安装

brew install fastlane
sudo gem install fastlane
fastlane init

fastlane init之后,会在当前目录下创建一个fastlane文件夹,以及./Gemfile、./Gemfile.lock文件。
fastlane文件夹下会有Appfile、Fastfile、Matchfile、Pluginfile。接下来就详细说下,这4个file里面的内容。

Appfile

Appfile 存储了应用相关的信息,比如App id、bundle id等。

app_identifier "com.app.xxx" # The bundle identifier of your app
apple_dev_portal_id "xxxx@xx.com" # Apple Developer Account
itunes_connect_id "xxxx@xx.com" # App Store Connect Account

apple_id "xxxx@xx.com" # Your Apple email address
team_id "xxxx" # Developer Portal Team ID
itc_team_id "xxxxx" # App Store Connect Team ID

# For more information about the Appfile, see:
#     https://docs.fastlane.tools/advanced/#appfile

Fastfile

Fastfile存储着fastlane运行的自动化配置。比如自定义打包的lane、配置环境变量、以及通过执行一些Action或者插件去执行任务,比如打包上传Fir、AppStore以及打包lane结果通过企业微信通知。接下来讲下Fastfile里面的相关内容。

Lanes

自定义lane

一个lane可以看成是一个函数方法。在终端命令行fastlane my_lane param1:xxx param2:xxx,传递了参数param1、param2。在lane里面,options接收参数。

lane :my_lane do |options|
  # Whatever actions you like go in here.
  value1 = options[:param1]
  value2 = options[:param2]
end

before_all block

before_all在所有lane执行之前会先执行一次

before_all do |lane, options|
  cocoapods
end

after_all block

after_all在所有lane执行之完之后执行一次

after_all do |lane, options|
  say("Successfully finished deployment (#{lane})!")
  slack(
    message: "Successfully submitted new App Update"
  )
  sh("./send_screenshots_to_team.sh") # Example
end

error block

lane执行失败,会走error

error do |lane, exception, options|
  slack(
    message: "Something went wrong with the deployment.",
    success: false,
    payload: { "Error Info" => exception.error_info.to_s } 
  )
end

导入其他的Fastlane

  • import
    导入本地的Fastlane
import "../GeneralFastfile"

override_lane :from_general do
  # ...
end
  • import_from_git
    从Git远程仓库导入Fastlane
default_platform(:ios)

platform :ios do
  xcversion(version: "~> 10.2")
  github_url = "git@github.com"
  import_from_git(url: "#{github_url}:XXX/FastlaneConfig.git", 
               branch: "master", 
                 path: "fastlane/Fastfile")
end

Actions

官网Actions
action对应每一个任务,底层都是调用其他的工具或者工具,都是对一些插件功能的封装。

常用的action

Action 描述
gym build_app 的别名
build_app 构建应用
xcodebuild 构建应用
cocoapods 运行 cocoapods
xcclean 清理工程
increment_build_number 增加版本号
match sync_code_signing 的别名,同步证书和配置描述文件
upload_to_testflight 上传到 TestFlight

打包配置

# 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
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself

default_platform(:ios)

platform :ios do

  before_all do
    ENV["FASTLANE_ITC_TEAM_ID"] = "xxx"
    ENV["FASTLANE_USER"] = "xxx@xxx.com"
    ENV["FASTLANE_PASSWORD"] = "***"
    ENV["FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD"] = "****-****"

  end
  
  
  desc "Description of what the lane does"

  lane :deploy do |options|
    # add actions here: https://docs.fastlane.tools/actions
    
    distribute = options[:distribute]
    target = options[:target]


    export = options[:exports] #导出方式
    symbols = false
    bitcode = false

    bundleName = "com.xxx.app"
    extensionName = "com.xxx.app.extension"
    
    provisioning = "match AdHoc #{bundleName}" #App 签名
    extension_provisioning = "match AdHoc #{extensionName}" #测试 extension 签名
    # extension_product_provisioning = "match AdHoc com.app.wisecart.ServiceExtensionProduct" #正式 extension 签名
    
    configuration = options[:configuration]

    if target == "Product"
   
    end

    if distribute == "Fir"

    else
      # export = "app-store"
      # configuration = "Release"
      if distribute == "AppStore"
        provisioning = "match AppStore #{bundleName}"
        extension_provisioning = "match AppStore #{extensionName}"
        # extension_product_provisioning = "match AppStore com.app.wisecart.ServiceExtensionProduct"
        symbols = true
        bitcode = false
      end
    end

    # the name of the ipa file
    time = Time.new
    dateTimeStr = "#{time.year}-#{time.month}-#{time.day}_#{time.hour}-#{time.min}"
    ipaName = "#{dateTimeStr}_#{target}.ipa"
    directoryPath = "../../../Output_ipa"
    fullPath = "../../../Output_ipa/#{ipaName}"
    xcode_select("/Applications/Xcode.app")

    # 编译
    gym(
      scheme: target,  # 指定需要打那个 scheme 的包
      export_method: export,  # 打包的方式
      clean: true, # 打包前是否 clean 项目
      configuration: configuration,
      silent: false, # 隐藏不必要信息
      include_bitcode: bitcode,  #是否开启bitcode
      include_symbols: symbols,  #包含符号
      output_directory: directoryPath,
      output_name: ipaName,
      export_options: {
        stripDebugSymbols: true,
        provisioningProfiles: {
          bundleName => provisioning,
          extensionName => extension_provisioning
        }
      }
    )

    # 上传
    description = options[:description]
    if distribute == "Fir"
        # 插件打包上传Fir
        gofir(description: description, path: fullPath, branch: options[:branch], target: target, buildName: options[:buildName], jobName: options[:jobName])
    elsif distribute == "Pgyer"
        # 插件打包上传蒲公英
        goPgyer(description: description, path: fullPath, configuration: configuration, scheme: target, export_method: export_method)
    else
      # 插件打包上传苹果
      upload_testflight
    end
  end

  lane :gofir do |options|
    desc "上传 beta 到 fir"
    
    description = options[:description]
    path = options[:path]
    branch = options[:branch]
    # gym export_method: "ad-hoc"
    
    # 多个参数 可以使用逗号(, )分离   
    info = fir_cli(api_token:"api_token", specify_file_path:"#{path}", changelog:"#{description}", skip_update_icon: "true", need_release_id: "true")
    notice(description: description,branch: branch, release_id: info[:release_id],short: info[:short],target: options[:target],buildName: options[:buildName],jobName: options[:jobName])
    # download_url = "http://d.firim.top/#{info[:short]}?release_id=#{info[:release_id]}"

  end

  lane :notice do |options|
    desc "企业微信通知"
    description = options[:description]
    branch = options[:branch]
    release_id = options[:release_id]
    short = options[:short]
    target = options[:target]
    path = options[:path]
    buildName = options[:buildName]
    jobName = options[:jobName]

    bundleName = get_ipa_info_plist_value(ipa: path, key: "CFBundleName")
    qrCodeImagePath = File.expand_path("../../../../Output_ipa/fir-#{bundleName}.png")
    md5 = sh("md5sum #{qrCodeImagePath}|awk '{print $1}'").gsub(/\s+/,'')
    qrcodeBase64Text = sh("base64 #{qrCodeImagePath} |sed ‘/^$/d'").gsub(/\s+/,'')

    # 接入企业微信插件,通知消息
    work_wechat(webhook_URL: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxx-xxxx-xxxx",
                image_md5: md5,
                image_base64: qrcodeBase64Text)

    work_wechat(webhook_URL: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxx-xxxx-xxxx",
                markdown_content: "👆👆👆美女帅锅们,二维码在上面\n打包项目:#{jobName}\n打包分支:#{branch}\n打包人:#{buildName}\n链接:[http://d.3appstore.com/#{short}?release_id=#{release_id}](http://d.3appstore.com/#{short}?release_id=#{release_id})\n更新内容:#{description}")

  end

  lane :goPgyer do |options|
    desc "上传 pgyer"
    
    description = options[:description]
    path = options[:path]
    configuration = options[:configuration]
    target = options[:scheme]
    export = options[:export]

    # gym export_method: "ad-hoc"
    gym(
    configuration: configuration,
    clean:true,
    scheme: target,   #工程下要打包的项目,如果一个工程有多个项目则用[项目1,项目2]
    export_method: export, #打包的方式, development/adhoc/enterprise/appstore
    output_directory: path,  #指定ipa最后输出的目录
    )
    # 多个参数 可以使用逗号(, )分离   
    pgyer(api_key: "api_key", user_key: "user_key",password: "****", install_type: "2", update_description: description)
    # download_url = "http://d.firim.top/#{info[:short]}?release_id=#{info[:release_id]}"

  end

  lane :upload_testflight do
    desc "上传 testflight"
    
    api_key = app_store_connect_api_key(
      key_id: "team_id",
      issuer_id: "xxxxx",
      key_filepath: "/AuthKey_xxxx.p8",
      duration: 1200, # optional (maximum 1200)
      in_house: false # optional but may be required if using match/sigh
    )

    upload_to_testflight(
      skip_waiting_for_build_processing: true
    )

    # 上传符号文件
    upload_symbols_to_crashlytics(
      gsp_path: "./Targets/Mitra/GoogleService-Info.plist",
      binary_path: "../upload-symbols"
    )

  end

end

以上打包配置可根据项目自身实际情况,稍作修改。

Matchfile管理iOS证书

Fastlane通过match来生成证书配置以及描述文件,通过加密上传到Git仓库中,为这种团队开发提供了方便。

接入

fastlane match init

命令会在./fastlane文件夹里生成一个Matchfile的文件。在Matchfile中,配置下远程git仓库地址、远程分支、bundle id、git账号密码等。

git_url("git@xxxx:xxxx/xxxx.git")

storage_mode("git")
# 如果有多个项目App的情况下,可以用不同的分支
git_branch("master")
# 证书的类型
type("development","adhoc","appstore") # The default type, can be: appstore, adhoc, enterprise or development

api_key_path("api_key.json")
# 可以配置多个bundle id
app_identifier(["com.app.xxxxx"])
username("xxxx@xxxx.com")
team_id("team_id") 
keychain_password("password")

match

使用

# 配置开发,测试,苹果证书以及描述文件
fastlane match development
fastlane match adhoc
fastlane match appstore

参数

# 在控制台可以打印详细信息
--verbose
# 在新机器上不会创建新的证书或配置文件
--readonly
# 设备发生了变化,更新配置文件
--force_for_new_devices 

文件夹结构

文件夹结构.png

第一次运行match后,会有2个文件夹上传到git仓库。还有一个README.md,方便团队新成员加入

  • certs 包含了所有的证书及其私钥
  • profiles 包含所有的配置文件

更新证书

更新证书.png
  • 先删掉远程的certs/development/.cer文件。
  • 执行fastlane match unke development,会清理开发者账号下的development证书和配置文件。
  • 执行fastlane match development会自动更新。

Pluginfile

# Autogenerated by fastlane
#
# Ensure this file is checked in to source control!

gem 'fastlane-plugin-fir_cli'
gem 'fastlane-plugin-apphoster'
gem 'fastlane-plugin-firim'
gem 'fastlane-plugin-work_wechat'
gem 'fastlane-plugin-pgyer'
gem 'fastlane-plugin-emerge'

在add_plugin添加插件之后,会自动生成fastlane/Pluginfile文件,安装好的插件会添加插件到该文件里。

搜索插件

# 控制台打印出所有的插件列表
fastlane search_plugins

添加插件

fastlane add_plugin fir_cli

其他机器安装插件

# 安装插件,确保所有的插件已经安装在本机器中
fastlane install_plugins
# 更新插件
fastlane update_plugins

Gemfile

source "https://rubygems.org"

gem "fastlane"

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

在fastlane init命令初始化之后,会自动生成该文件以及Gemfile.lock。在fastlane add_plugin执行成功之后,会自动修改该文件。

参考资料

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,427评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,551评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,747评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,939评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,955评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,737评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,448评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,352评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,834评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,992评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,133评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,815评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,477评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,022评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,147评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,398评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,077评论 2 355

推荐阅读更多精彩内容