iOS fastlane 多环境配置自动打包及自动上传蒲公英

IOS 自动化打包需要几步:

一、安装fastlane

1.fastlane是用Ruby语言编写的工具,需要有Ruby开发环境,先查看是否安装

ruby -v
  • 如果终端提示一下信息则表示已安装过:
ruby 2.6.10p210 (2022-04-12 revision 67958) [universal.x86_64-darwin22]

2.安装fastlane

  • 安装fastlane,安装过程中可能会出现错误,比如权限错误,可根据错误提示自行百度
brew install fastlane
  • 查看fastlan版本fastlane --version,提示下面信息,则表示已经安装成功
    image.png

3.安装 Xcode command line tools:

xcode-select--install
  • 安装过会提示已经安装


    image.png

二、 配置fastlane

1.到项目根路径执行初始化命令

cd  项目跟路径

fastlane init

终端会显示一下内容:选择 4 手动设置,中间会让你点击此 enter 键,进行确认

What would you like to use fastlane for?
///自动化截图
1. 📸  Automate screenshots
///
2. 👩✈️  Automate beta distribution to TestFlight
///上传appstore,
3. 🚀  Automate App Store distribution
/// 自定义上传
4. 🛠  Manual setup - manually setup your project to automate your tasks
  • 初始化成功后项目中会多出一下几个文件
    image.png
  • 编辑Fastfile


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

fastlane_version "2.214.0" 

default_platform(:ios)

# 项目ID
project_identifier_Dev = "com.onlytest.dev"
project_identifier_Dis = "com.onlytest"

# 测试环境scheme名称
project_Dev_scheme = "x_Dev"
# 正式环境scheme名称
project_Dis_scheme = "x_Dis"
# 发布环境scheme名称
project_App_scheme = "x_AppStore"

# 项目的描述文件名称(这里也可以配置一个通用的Adhoc文件)
project_provisioningProfiles_dev = "onlytest_AdHoc_dev"
project_provisioningProfiles_dis = "onlytest_AdHoc_dis"
project_provisioningProfiles_appstore = "onlytest_appstore"

# 默认内测打包方式,目前支持app-store, package, ad-hoc, enterprise, development
# 注:由于如果使用手动配置证书,在export_options指定打包方式!
ipa_exportMethod_adhoc = "ad-hoc"
ipa_exportMethod_appstore = "app-store"


#蒲公英pgyer_apiKey和pgyer_userkey 换成你的
pgyer_apiKey ="5db239fd960xxxxxx"
#这个可以不用
pgyer_userkey ="bf1cd97e6bb3xxxxxx"


currentTime = Time.new.strftime("%Y%m%d")

ipa_name = "应用名"

# .ipa文件输出路径
ipa_outputDirectory_Debug = "~/Desktop/IPA/#{ipa_name}/Dev/#{currentTime}"
ipa_outputDirectory_Release = "~/Desktop/IPA/#{ipa_name}/Dis/#{currentTime}"
ipa_outputDirectory_AppStore = "~/Desktop/IPA/#{ipa_name}/AppStore/#{currentTime}"


# 计算buildNumber
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

platform :ios do
  desc "Description of what the lane does"
  lane :ad_dev do
        puts "*************| 开始打包.ipa文件... |*************"
        # 更新项目build号
        updateProjectBuildNumber

        #指定Xcode路径,装多个版本xcode时指定xcode版本
        xcode_select("/Applications/Xcode.app")
        #build_app(export_method: "ad-hoc")
        # 开始打包
        gym(
            # 指定输出的ipa名称
            output_name:"#{project_Dev_scheme}_#{get_build_number()}",
            # 指定项目的scheme
            scheme:"#{project_Dev_scheme}",
            # 是否清空以前的编译信息 true:是
            clean:true,
            # 指定打包方式,Release 或者 Debug
            configuration:"Release",
            # 指定打包方式,目前支持app-store, package, ad-hoc, enterprise, development
            # 注:由于使用手动配置证书,在export_options指定打包方式
            export_method:"#{ipa_exportMethod_adhoc}",
            # 指定输出文件夹
            output_directory:"#{ipa_outputDirectory_Debug}",
            # Xcode9将不会允许你访问钥匙串里的内容,除非设置allowProvisioningUpdates
            export_xcargs:"-allowProvisioningUpdates",
            # 隐藏没有必要的信息
            silent:true,
            # 手动配置证书,注意打包方式需在export_options内使用method设置,不可使用export_method
            export_options: {
                method:"#{ipa_exportMethod_adhoc}",
                provisioningProfiles: {
                    "#{project_identifier_Dev}":"#{project_provisioningProfiles_dev}",
                    "#{project_identifier_Dis}":"#{project_provisioningProfiles_dis}"

                },
            }
        )
        puts "*************| 开始上传蒲公英... |*************"
        # 开始上传蒲公英
    # pgyer(api_key: "#{pgyer_apiKey}", password: "123456", install_type: "2")
        pgyer(api_key: "#{pgyer_apiKey}", password: "123456", install_type: "2", update_description: "更新内容")
        puts "*************| 上传蒲公英成功🎉 |*************"

  end

  desc "Description of what the lane does"
  lane :ad_dis do
        puts "*************| 开始打包.ipa文件... |*************"
        # 更新项目build号
        updateProjectBuildNumber

    #指定Xcode路径,装多个版本xcode时需指定
    #xcode_select("/Applications/Xcode.app")
    #build_app(export_method: "ad-hoc")
    # 开始打包
        gym(
            # 指定输出的ipa名称
            output_name:"#{project_Dis_scheme}_#{get_build_number()}",
            # 指定项目的scheme
            scheme:"#{project_Dis_scheme}",
            # 是否清空以前的编译信息 true:是
            clean:true,
            # 指定打包方式,Release 或者 Debug
            configuration:"Release",
            # 指定打包方式,目前支持app-store, package, ad-hoc, enterprise, development
            # 注:由于使用手动配置证书,在export_options指定打包方式
            export_method:"#{ipa_exportMethod_adhoc}",
            # 指定输出文件夹
            output_directory:"#{ipa_outputDirectory_Debug}",
            # Xcode9将不会允许你访问钥匙串里的内容,除非设置allowProvisioningUpdates
            export_xcargs:"-allowProvisioningUpdates",
            # 隐藏没有必要的信息
            silent:true,
            # 手动配置证书,注意打包方式需在export_options内使用method设置,不可使用export_method
            export_options: {
                method:"#{ipa_exportMethod_adhoc}",
                provisioningProfiles: {
                    "#{project_identifier_Dev}":"#{project_provisioningProfiles_dev}",
                    "#{project_identifier_Dis}":"#{project_provisioningProfiles_dis}"

                },
            }
        )
        puts "*************| 开始上传蒲公英... |*************"
        # 开始上传蒲公英
    # pgyer(api_key: "#{pgyer_apiKey}", password: "123456", install_type: "2")
    # pgyer(api_key: "#{pgyer_apiKey}", password: "123456", install_type: "2", update_description: "哈哈哈哈哈哈哈")
        puts "*************| 上传蒲公英成功🎉 |*************"

  end


    # ----------------------- 上传AppStore -----------------------
    lane :aps do

        puts "*************| 开始打包.ipa文件... |*************"

        # 更新项目build号
        updateProjectBuildNumber

        gym(
            # 指定输出的ipa名称
            output_name:"#{project_App_scheme}_#{get_build_number()}",
            # 指定项目的scheme
            scheme:"#{project_App_scheme}",
            # 是否清空以前的编译信息 true:是
            clean:true,
            # 指定打包方式,Release 或者 Debug
            configuration:"Release",
            # 指定打包方式,目前支持app-store, package, ad-hoc, enterprise, development
            # 注:由于使用手动配置证书,在export_options指定打包方式
            #export_method:"#{app-store}",
            # 指定输出文件夹
            output_directory:"#{ipa_outputDirectory_AppStore}",
            # Xcode9将不会允许你访问钥匙串里的内容,除非设置allowProvisioningUpdates
            export_xcargs:"-allowProvisioningUpdates",
            # 隐藏没有必要的信息
            silent:true,
            # 手动配置证书,注意打包方式需在export_options内使用method设置,不可使用export_method
            export_options: {
                method:"app-store",
                provisioningProfiles: {
                    "#{project_identifier_Dev}":"#{project_provisioningProfiles_dev}",
                    "#{project_identifier_Dis}":"#{project_provisioningProfiles_appstore}"
                },
            }
         )
        puts "*************| 上传AppStore成功🎉 |*************"
        #发布testflight测试
        # pilot
    end


end

三、打包发布

  • 需要打包时,进入项目目录,
    需要打测试环境时:执行命令 fastlane ad_dev
    需要打正式环境时:执行命令 fastlane ad_dis


    开始打包.png

    上传蒲公英成功.png
打包成功后ipa自动保存.png

配置过程中出现的一些问题

  1. 打包或导出失问题,其实在配置过程中大部分问题都是证书引起的,特别是配置多环境多个Bundle Identifier 多Target或多Scheme。
  • export_method 与 method 方式不一致
    image.png

手动配置证书时,需在export_options内使用method设置,不可使用export_method

 /// 手动配置证书,注意打包方式需在export_options内使用method设置,不可使用export_method
  export_options: {
        method:"ad-hoc"       
  }
  • 提示需要再fastfile文件中指定xcode版本


    image.png

    根据错误提示再文件中进行设置即可,例如:

 #指定Xcode路径,装多个版本xcode时指定xcode版本
xcode_select("/Applications/Xcode14.2.app")
  • Archive 及 Export成功,上传报错
    image.png
    没有添加蒲公英命令:在项目更目录下,执行:fastlane add_plugin pgyer
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容