【iOS】Fastlane一键打包上传到TestFlight、蒲公英

Fastlane一键打包上传到TestFlight、蒲公英

前言

  • 废话不多说,能知道fastlane的,代表您已经对fastlane的用处有了初步了解,并且想使用在自己项目中,本文只适用于iOS项目的fastlane配置(Android端未验证),直接开始配置。

一、准备

  • Ruby环境CocoaPods环境蒲公英官网
  • 打正式包上传testflight过程中需要输入密码,对于开启了双重认证的账户需要去 ,申请专属密码app专属密码申请
  • iOS开发必须要知道的基础开发环境配置,终端执行pod env
  • 登录蒲公英,获取api_key,复制备用

二、探索

一、Fastlane配置

1、Fastlane安装

  • 终端执行以下命令以安装Fastlane
    需要配置的安装:
ruby 复制代码
sudo gem install fastlane
  • 快速安装:
ruby 复制代码
sudo gem install fastlane -NV

执行完成后出现以下字样即为安装成功

2、Fastlane更新

以下两种终端更新Fastlane方式,使用一种即可:

  • 方式一:
ruby 复制代码
bundle update fastlane
  • 方式二:
ruby 复制代码
fastlane update_fastlane

3、Fastlane卸载

  • 终端执行以下命令来卸载Fastlane
powershell 复制代码
sudo gem unstall fastlane

4、查看Fastlane版本

  • 执行以下命令查看Fastlane版本,以验证
ruby 复制代码
fastlane --version

5、查看Fastlane位置

powershell 复制代码
which fastlane

6、Fastlane初始化

  • 终端cd到项目工程根目录下,执行以下命令,来初始化fastlane
ruby 复制代码
fastlane init
  • 项目工程里会创建出fastlane文件夹、Gemfile和Gemfile.lock
  • 在fastlane init过程中有些需要输入开发者账号密码,后续可通过以下命令重新登录账号。
ruby 复制代码
fastlane cert create

二、Fastlane安装蒲公英插件

  • 终端执行以下命令来安装蒲公英插件
ruby 复制代码
fastlane add_plugin pgyer
  • 执行过程中出现以下内容,是为了在Gemfile文件里面写入Pluginfile内容,输入y按回车
ruby 复制代码
Plugin 'fastlane-plugin-pgyer' was added to './fastlane/Pluginfile'
[10:08:29]: It looks like fastlane plugins are not yet set up for this project.
[10:08:29]: fastlane will modify your existing Gemfile at path 'Gemfile'
[10:08:29]: This change is necessary for fastlane plugins to work
[10:08:29]: Should fastlane modify the Gemfile at path 'Gemfile' for you? (y/n)
  • 出现以下内容即为安装蒲公英插件成功

三、Fastlane文件编辑

1、Gemfile文件

一般安装好蒲公英插件和fastlane初始化后会默认生成

powershell 复制代码
source "https://rubygems.org"

gem "fastlane"

gem "cocoapods"

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

2、Appfile文件

一般安装好蒲公英插件和fastlane初始化后会默认生成,可以按需求修改,个人的Apple ID方便接收AppStore上传及审核信息

powershell 复制代码
app_identifier("com.xxx.xxx") #app的bundleID 
apple_id("xxx@xxx.com") #个人的Apple ID,邮箱

itc_team_id("xxxxxx") #App Store的团队ID App Store Connect Team ID
team_id("xxxxxx") #团队ID Developer Portal Team ID
branch ENV['xxxxxx'] #分支
# For more information about the Appfile, see:
#     https://docs.fastlane.tools/advanced/#appfile

3、Fastfile文件

Fastfile文件为核心配置文件,需要编辑打包流程到信息,每一个功能块由 ==lane: xxx do ==开头 end 结尾

1、递增build号

定义一个递增build号的函数,添加到Fastfile中

powershell 复制代码
def updateProjectBuildNumber

currentTime = Time.new.strftime("%Y%m%d")
build = get_build_number()
if build.include?"#{currentTime}."
	# => 为当天版本 计算迭代版本号
	lastStr = build[build.length-2..build.length-1]
	lastNum = lastStr.to_i
	lastNum = lastNum + 1
	lastStr = lastNum.to_s
	if lastNum < 10
		lastStr = lastStr.insert(0,"0")
	end
	build = "#{currentTime}.#{lastStr}"
else
	# => 非当天版本 build 号重置
	build = "#{currentTime}.01"
end
puts("*************| 更新build #{build} |*************")
# => 更改项目 build 号
increment_build_number(
build_number: "#{build}"
)
end
2、任务配置
  • 以下为任务配置信息仅供参考(请切换为属于你的专属信息):
powershell 复制代码
#fastlane版本号
fastlane_version "2.216.0"
#打包平台
default_platform :ios

#指定项目的scheme名称 ---- 修改
scheme="xxx"
#蒲公英api_key和user_key ---- 修改
api_key="xxx"
user_key="xxx"

# 任务脚本
platform :ios do
  desc "以 development 方式打包并上传到蒲公英"
  lane :test_beta do
    puts "以 development 方式打包"
    gym(
      # 指定打包所使用的输出方式 (可选: app-store, package, ad-hoc, enterprise, development)
      export_method: "development",
      # 指定项目的 scheme 名称
      scheme: "xxx",
      # 指定输出的文件夹地址
      output_directory: "./archive/test_beta/" + Time.new.strftime("%Y-%m-%d-%H:%M:%S"),
    )
    puts "上传 ipa 包到蒲公英"
    pgyer(
      # 蒲公英 API KEY
      api_key: "xxx",
      # 蒲公英 USER KEY
      user_key: "xxx"
    )
  end
  desc "以 ad-hoc 方式打包并上传到蒲公英"
  lane :beta do
    puts "自动生成 Provisioning Profiles 文件"
    sigh(
      # 指定输出的文件夹地址
      output_path: "./archive/sign",
      # 是否为 AdHoc 证书(设为 false 或不写默认为 AppStore 证书)
      adhoc: true
    )
    puts "以 ad-hoc 方式打包"
    gym(
      # 指定打包所使用的输出方式 (可选: app-store, package, ad-hoc, enterprise, development)
      export_method: "ad-hoc",
      # 指定项目的 scheme 名称
      scheme: "xxx",
      # 指定输出的文件夹地址
      output_directory: "./archive/beta/" + Time.new.strftime("%Y-%m-%d-%H:%M:%S"),
      # 指定打包方式 (可选: Release, Debug)
      configuration: "Release"
    )
    puts "上传 ipa 包到蒲公英"
    pgyer(
      # 蒲公英 API KEY
      api_key: "xxx",
      # 蒲公英 USER KEY
      user_key: "xxx"
    )
  end
  desc "以 app-store 方式打包并上传到 iTunes Connect"
  lane :release do
    puts "自动生成 Provisioning Profiles 文件"
    sigh(
      # 指定输出的文件夹地址
      output_path: "./archive/sign"
    )
    puts "以 app-store 方式打包"
    gym(
      # 指定打包所使用的输出方式 (可选: app-store, package, ad-hoc, enterprise, development)
      export_method: "app-store",
      # 指定项目的 scheme 名称
      scheme: "xxx",
      # 指定输出的文件夹地址
      output_directory: "./archive/release/" + Time.new.strftime("%Y-%m-%d-%H:%M:%S"),
      # 指定打包方式 (可选: Release, Debug)
      configuration: "Release"
    )
    puts "上传 ipa 包到 iTunes Connect"
    deliver(
      # 跳过截图上传
      skip_screenshots: true,
      # 跳过元数据上传
      skip_metadata: true,
      # 跳过审核直接上传
      force: true
    )
  end
end

以上配置完成就可以在本地终端进行打包了,

  • export_method是在Fastlane工具中使用的命令,指定导出iOS应用存档的方法,以及可用的配置文件和签名证书。
    以下是几种导出存档方法:
    • app-store:提交到应用商店的存档
    • development:用于开发目的的存档
    • ad-hoc:用于分发给有权限的设备或用户的存档
    • enterprise:公司内部网络分发的存档
  • configuration构建方式:分Release和Debug
  • scheme:
  • output_directory:输出文件夹地址
  • desc、puts:信息描述

打开终端进入到项目fastlane文件夹上一级,输入以下命令即可打一个以 development 方式打包并上传到蒲公英的包。

powershell 复制代码
fastlane test_beta

4、Pluginfile文件

一般安装好蒲公英插件后会默认生成

powershell 复制代码
# Autogenerated by fastlane
#
# Ensure this file is checked in to source control!

gem 'fastlane-plugin-pgyer'

四、项目工程配置Fastlane

1、递增版本号配置
  • 修改Build Settings中的Versioning配置,Current Project Version随便填一个,Versioning System选择Apple Generic
2、修改info.plist File路径

把项目工程中所有的info.plist文件的路径中带有SRCROOT的路径全部改成相对路径,要不然会报错。

五、Fastlane执行

做 fastlane 命令的时候都是在项目的根目录下进行的,打开终端进入到项目fastlane文件夹上一级。

  • 1,获取到所有的 lane
powershell 复制代码
fastlane lanes

2、账号权限

  • 1,Adding a Credential(添加凭证)
    //输入命令:
powershell 复制代码
fastlane fastlane-credentials add --username felix@krausefx.com

//会自动要求输入密码:App-Specific Passwords

Password: *********(这里就是要输入刚刚生成的App-Specific Passwords)

如果手动输入错误:请执行以下命令:删除账号,再重复添加凭证,完成添加

  • 2,Removing a Credential(移除凭证)
powershell 复制代码
fastlane fastlane-credentials remove --username felix@krausefx.com

//password has been deleted.

三、总结

工具的使用没有什么技术含量,想研究fastlane代码可参考github

四、常见问题归纳

  • 1,账号权限问题,重复一下上面账号权限模块,移除凭证,添加凭证的命令步骤
powershell 复制代码
[!] Error uploading ipa file:
 [Application Loader Error Output]: Error uploading '/var/folders/sp/_4jyc68d0hvbm70nm50ljvvw0000gp/T/3a982536-cad1-483e-8b64-5c74740a168f.ipa'.
[Application Loader Error Output]: Unable to upload archive. Failed to get authorization for username 'xxx@126.com' and password. (
[Application Loader Error Output]: The call to the altool completed with a non-zero exit status: 1. This indicates a failure.
相关推荐
tangweiguo0305198714 小时前
SwiftUI布局完全指南:从入门到精通
ios·swift
T1an-118 小时前
最右IOS岗一面
ios
坏小虎20 小时前
Expo 快速创建 Android/iOS 应用开发指南
android·ios·rn·expo
光影少年21 小时前
Android和iOS原生开发的基础知识对RN开发的重要性,RN打包发布时原生端需要做哪些配置?
android·前端·react native·react.js·ios
北京自在科技21 小时前
Find My 修复定位 BUG,AirTag 安全再升级
ios·findmy·airtag
Digitally1 天前
如何不用 USB 线将 iPhone 照片传到电脑?
ios·电脑·iphone
Sim14801 天前
iPhone将内置本地大模型,手机端AI实现0 token成本时代来临?
人工智能·ios·智能手机·iphone
Digitally2 天前
如何将 iPad 上的照片传输到 U 盘(4 种解决方案)
ios·ipad
报错小能手2 天前
ios开发方向——swift并发进阶核心 @MainActor 与 DispatchQueue.main 解析
开发语言·ios·swift
LcGero2 天前
Cocos Creator 业务与原生通信详解
android·ios·cocos creator·游戏开发·jsb