fastlane自动打包

2,790 阅读6分钟

前言

fastlane也是自动化的一个手段,并且其不只是在 ios 中使用,其他平台也适用,其能大幅度提高开发速度,毕竟自动化配置完毕后,测试也能自己打包更新了

测试demo

fastlane文档

fastlane的安装

首先确保安装了最新Xcode命令行工具,打开命令行执行下面命令

xcode-select --install

如果已经安装过了,会提示
xcode-select: error: command line tools are already installed, 
use "Software Update" to install updates

方案一(推荐)

fastlane推荐,这种方式将明确定义要使用的fastlane版本及其依赖项,还将加快fastlane的执行速度

并且该方案基本上不会出现 fastlane 命令找不到的问题,可以减少后续配置

使用 bundler来安装配置 fastlane

首先使用 gem 安装 bundler,相信配置 cocoapods 的过程试用过 gem

sudo gem install bundler

然后进入到 项目目录下,创建 名为 Gemfile 的文件,没有后缀名

然后编辑 Gemfile 文件内容,如下所示

source "https://rubygems.org"

gem "fastlane"

通过 bundle update 命令,可直接更新bundle,并可用于更新和生成Gemfile.lock文件

bundle update

如果 Gemfile.lock 删除,可重新执行 bundle update,或执行 bundle install

然后使用 bundle exec fastlane 命令,生成配置文件

bundle exec fastlane
//此时也可以和其他一样,时使用下面命令,生成配置文件,可以已经没必要了,还需要多打几个字母
bundle exec fastlane init

如下所示,按需要选择即可,这里选择了 4,也可以选择 3 (即:直接从appstore拉取需要的信息,需要输入账号密码信息,选错了也没事,后面可以配置信息)

image.png

注意

1、如需更新fastlane,只需运行 bundle update fastlane 命令,更新的前提是已经生成了 Gemfile.lock

2、 如果配置了CI,在您的 CI 上,添加bundle install作为您的第一个构建步骤

3、fastlane 项目中 里面 bundle 方式的运行命令,均为 bundle exec开头,例如: bundle exec fastlane

4、如果是其他安装方式 改成 bundle, 那么运行方式上 需要将 fastlane 命令 改成 bundle exec fastlane(可以理解为 bundle exec fastlane == fastlane

方案二

使用 homebrew 商店安装 fastlane,执行下面命令

brew install fastlane

安装完成之后,进入到 项目目录中,并执行下面命令

fastlane init

//下面是 swift 的
fastlane init swift

方案三

使用 gem 直接安装 fastlane, 执行下面命令

sudo gem install fastlane

安装完成之后,进入到 项目目录中,并执行下面命令

fastlane init

//下面是 swift 的
fastlane init swift

fastlane 环境安装好了之后,会生成下面的几个主要文件信息,(appstore登陆后生成的文件会更多),如下所示

image.png

fastlane 配置打包上传

上面安装并初始化 fastlane 之后,会生成一个 fastlane文件夹,里面有两个文件需要注意 AppfileFastfile,下面配置他们两个

Appfile

Appfile 主要是配置项目的 账号名字bundleID,如下所示,更改为自己的项目

app_identifier("[APP_IDENTIFIER]") # 项目App的 bundle identifier

apple_id("[APPLE_ID]") # Apple账号邮箱

# itc_team_id("x17383873x") # App Store Connect 上线的 Team ID

# team_id("4BGCVPB2J7") # Developer 开发者团队的 Team ID

team_id 可以在证书或者平台查看,证书查看方式如下所示:

2f7c63ce7b0e44049e59a25c45425af7_tplv-k3u1fbpfcp-watermark.webp

如果本地 xcode 已经登录 apple账号 配置好了相关证书,那么直接使用本地环境下的账号信息即可,

AppFile 文件可以进行如下设置

app_identifier		 ENV['App_Identifier']
apple_id 		 ENV['Apple_Id']
itc_team_id		 ENV['Itc_Team_Id']
team_id			 ENV['Team_Id']

Fastfile

fastfile 是我们项目配置的关键,打包发布的逻辑都在这里

里面默认的主要代码如下所示,如下所示(上传 蒲公英firim 的还有继续往后看)

default_platform(:ios)

platform :ios do
    
    # 运行前的操作,二次密码验证的设定,如果只是上传hoc到蒲公英或firim,注释掉即可
    before_all do
        # ENV["FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD"] = "tcha-fzfe-jcmd-tken"
    end
    
    # desc 为 lane 的注释信息,后面运行时便会理解
    desc "上线 App Store"
    # lane 为平台设置,后面的 release 为 lane 的名称, option可以通过其设置版本号,这里取消了
    lane :release do | options |
        # 编译版本号自增设置,项目的版本号需要自己设定的和appstore一样,自己项目中设置好即可
        # 编译版本号可以默认设置 0,然后自增即可
        increment_build_number(xcodeproj: ENV['Xcodeproj'])

        build_app(
            workspace: ENV['Workspace'], # 编译项目文件,为目录环境下的workspace
            scheme: ENV['Scheme'], # scheme为环境下的的scheme
            clean: true, # 构建前先清理项目缓存
            silent: true, # 静默构建
            output_directory: './fastlane/appstore/', # 设置导出ipa目录
            output_name:"#{currentTime}#{ENV['ipa']}") # 包的名称

        upload_to_app_store(force: true) # 上传到appstore
      end


    desc "上线testflight"
    # lane 为平台设置,后面的 release_to_testFlight 为 lane 的名称
    lane :release_to_testFlight do | options |
        # 编译版本号自增设置,项目的版本号需要自己设定的和appstore一样,自己项目中设置好即可
        increment_build_number(xcodeproj: ENV['Xcodeproj'])

        build_app(
            workspace: ENV['Workspace'],
            scheme: ENV['Scheme'],
            clean: true,
            silent: true,
            output_directory: './fastlane/testflight/',# 设置导出ipa目录
            output_name:"#{currentTime}#{ENV['ipa']}")

        upload_to_testflight
    end


    desc "上线蒲公英"
    lane :adhoc_pgy do |options|
        # 编译版本号自增设置,项目的版本号需要自己设定的和appstore一样,自己项目中设置好即可
        increment_build_number(xcodeproj:ENV['Xcodeproj'])
        # 获取时间,用于设置包目录或者包名
        currentTime = Time.new.strftime("%Y-%m-%d-%H-%M-")
        logDirectory = "#{currentTime}"

        build_app(
            workspace: ENV['Workspace'],
            scheme: ENV['Scheme'],
            silent: true,
            clean: true,
            output_directory: './fastlane/pgy/',
            output_name:"#{currentTime}#{ENV['ipa']}",# 设置导出ipa目录
            export_method:"ad-hoc") # 导出方式为 ad-hoc

        # 设置蒲公英的 api_key 和 user_key
        # pgyer(api_key:'xxb3dd2ab6a5efc2b7d861356854xx',
        # user_key:'xxx753c46ae83961ed9478bdae86ebxx')
        
        # 使用本地环境下的蒲公英 key 信息,运行后首次需要自己输入账号密码
        # pgyer(api_key: ENV['api_key'],
        # user_key: ENV['user_Key'])
    end

$$
\TeX
$$

    desc "上线firim"
    lane :adhoc_firim do |options|

        increment_build_number(xcodeproj:ENV['Xcodeproj'])

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

        logDirectory = "#{currentTime}"

        build_app(
            workspace: ENV['Workspace'],
            scheme: ENV['Scheme'],
            silent: true,
            clean: true,
            output_directory: './fastlane/firim/',
            output_name: "#{currentTime}#{ENV['ipa']}",# 设置导出ipa目录
            export_method:"ad-hoc") # 导出方式为 ad-hoc
            
        # 设置 firim 的 api_token
        firim(firim_api_token:'xxx23412341234xxx')
        # 使用本地环境的 firim_api_token信息,运行后首次需要输入自己账号密码
        # firim(firim_api_token:ENV['firim_api_token'])
  end	

end

蒲公英 or firim 环境配置

上面的命令配置好了之后,需要根据自己需要上传 蒲公英firim 需要如下配置

蒲公英配置

进入项目目录,执行下面命令

//其他方式的
fastlane add_plugin pgyer

// bundle的方式
bundle exec fastlane add_plugin pgyer

firim配置

进入项目目录,执行下面命令

//先通过 gem 安装 fir环境
sudo gem install fir-cli 

然后安装配置 fastlane 的 firim

//其他方式的
fastlane add_plugin firim

// bundle的方式
bundle exec fastlane add_plugin firim

蒲公英 或 firim 配置完毕后,fastlane 会多出来一个 Punginfile文件,说明配置成功,不用更改

image.png

运行 fastlane

进入到项目目录,运行下面命令

//其他方式的
fastlane

// bundle的方式
bundle exec fastlane

运行后,如下所示,会出现一个选择界面,会根据选择的数字,运行对应的 lane下指令

image.png

信息到这,便会理解前面的 desclane 的名称配置了

此外,如果想 直接运行指定的 lane,在运行指令后面加上 lane名称 即可

//其他方式的,直接运行某个 lane
fastlane [lane名称]

// bundle的方式,直接运行某个 lane
bundle exec fastlane [lane名称]

那么,则会直接执行 对应 lande 的操作

例如: 运行 bundle exec fastlane adhoc_pgy 或者 fastlane adhoc_pgy

运行成功,结果如下所示

image.png

最后

上面的配置信息,可以拿来直接用,主要是更改账号teamid蒲公英firim信息

其他的平台,可以自己尝试一下哈,不明白的可以去 fastlane 查看!