美文网首页
使用fastlane 实现ipa自动打包脚本

使用fastlane 实现ipa自动打包脚本

作者: Riven2018 | 来源:发表于2018-05-16 15:20 被阅读0次

    对于一个iOS APP的发布上线,一般来说都需要经历:编译打包 -> 截图 -> 填写一些说明文字 -> 上传ipa到itunes connect -> 提交供审核。每次都要进行这么多“繁琐”的步骤,对于某些步骤可能一次还不能执行成功需要等着界面提示上传错误然后手动重新再来一次(想想都觉得可怕)。
    在日常开发中,打包也是最后上线不可缺少的环节,如果需要生成ipa文件通常需要在Xcode里点击Product -> Archive,然后在弹出来的Organizer中选择导出什么类型(ad hoc/enterprise)的包。对于大项目来说动辄编译十分钟以上的来说,一天打几个包就差不多过去了。
    为了解决这些问题,Felix Krause大神写了一个工具集fastlane。fastlane是一套使用Ruby写的自动化工具集,用于iOS和Android的自动化打包、发布等工作。

    前言

    最近用shell打包ipa发现终端总是提示
    shell error: exportArchive: "***.app" requires a provisioning profile.
    把配置文件删除重新下载也一样,所以索性重新换用另一种脚本工具来打包ipa,发现这个还是挺好用的

    介绍

    配置环境

    • 首先要安装正确的 Ruby 版本。在终端窗口中用下列命令来确认:
      ruby -v

    • 如果没有安装,则输入命令安装gym:
      sudo gem install gym

    • 确保Xcode命令行工具安装最新版本,使用如下命令进行安装:
      xcode-select --install

    • 以上依赖配置好之后就可以通过 rubygem 进行安装fastlane:
      sudo gem install fastlane

    • 完成安装

    fastlane实战

    初始化

    • 打开终端,cd到你的工程目录,然后执行fastlane init命令开始初始化

    • 在执行的过程中会要求填写一些项目的资料,如Apple ID等,fastlane会自动检测当前目录下项目的App Name和App Identifier,可以选择自行输入这些信息。初始化完成会在当前目录下面生成一个fastlane的文件夹。

    • 最重要的两个文件就是Appfile和Fastfile,主要的说明如下

      • Appfile里面存放了App的基本信息包括app_identifier、apple_id、team_id等,如果在init的时候输入了正确的apple_id和密码会自动获取team_id。

      • Fastfile是最重要的一个文件,在这个里面可以编写和定制我们的自动化脚本,所有的流程控制功能都写在这个文件里面。

    fastfile 文件

    Fastfile管理你所创建的 lane ,了解详情。它的格式是这样的:
      lane :inHouse do
      gym(scheme: "XXX",
          export_method:"enterprise",
          output_directory "./build", # 打包后的 ipa 文件存放的目录
          output_name "XXX"  # ipa 文件名
       )
      end
    

    我的用法

    fastfile文件里主要修改四个地方内容

    1. platform :ios do(安卓,iOS都可以用)

    2.desc "ad_Hoc 版本"(对lane的描述,fastlane会自动将desc的内容生成说明文档)

    1. lane :beta do (定义一个lane(任务),可以理解为一个函数,我们在执行的时候使用fastlane lane名称,比如 cd到项目根目录,然后 fastlane beta )

    2. gym(scheme: “项目名称”, export_method:"app-store",output_directory: "./build",)

    我一般用gym语法操作

    gym(scheme: scheme_name, clean: true, export_method:'appstore', configuration: configuration, output_directory: output_directory, output_name: output_name)
    

    结果

    这次我只是用来打包测试,所以

    # 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
    # update_fastlane
    # 指定打包所使用的输出方式,目前支持app-store, package, ad-hoc, enterprise, development, 和developer-id
    
    
    default_platform(:ios)
    
    platform :ios do
      desc "ad_Hoc 版本"
      lane :beta do
        gym(scheme: "***",
            export_method:"ad-hoc",
            output_directory: "./build",#文件路径
             )
        end
    end
    

    接下来我们开始进阶教程,将打包好的ipa上传到fir
    在Fastfile文件里写入:

    # Update this, if you use features of a newer version
    # 指定打包所使用的输出方式,目前支持app-store, package, ad-hoc, enterprise, development, 和developer-id
    
    
    
    default_platform :ios
    
    platform :ios do
    
    
     desc "开始打包-内测版--开发证书 - dev"
        #内测版--开发证书
     lane :adhoc do 
        #开始打包
        puts "开始打包-内测版--开发证书 - dev"
    
        gym(
          export_method:"ad-hoc",
          output_directory:"/Users/weiyuxiang/Desktop/Order/build",# 打包后的 ipa 文件存放的目录
         ) 
        #使用fir-cli上传ipa
        sh "fir publish /Users/weiyuxiang/Desktop/Order/build/***.ipa -T fir的token"
      end
      
      desc "开始打包  --  企业公测版--hoc"
      lane :inhoc do
        gym(
          export_method:"app-store",
          output_directory:"/Users/weiyuxiang/Desktop/Order/build",
        )
        #使用fir-cli上传ipa
        sh "fir publish /Users/weiyuxiang/Desktop/Order/build/***.ipa -T  fir的token"
      end
    
    end
    

    在测试的时候用
    fastlane adhoc
    上架则用
    fastlane inhoc
    到这里一般没有问题,但是楼主我还会遇到一个问题,最后用fir上传的时候会报这些错误:

    /Users/weiyuxiang/.rvm/rubies/ruby-2.2.4/lib/ruby/gems/2.2.0/gems/fastlane-2.95.0/fastlane_core/lib/fastlane_core/ui/interface.rb:153:in `shell_error!': [!] Exit status of command 'fir publish /Users/weiyuxiang/Desktop/Order/build/***.ipa -T a5bd6574b7292220d5c4b44b6' was 1 instead of 0. (FastlaneCore::Interface::FastlaneShellError)
    /Users/weiyuxiang/.rvm/gems/ruby-2.2.4@global/gems/bundler-1.16.1/lib/bundler/rubygems_integration.rb:458:in `block in replace_bin_path': can't find executable fir for gem fir-cli. fir-cli is not currently included in the bundle, perhaps you meant to add it to your Gemfile? (Gem::Exception)
        from /Users/weiyuxiang/.rvm/gems/ruby-2.2.4@global/gems/bundler-1.16.1/lib/bundler/rubygems_integration.rb:478:in `block in replace_bin_path'
        from /Users/weiyuxiang/.rvm/gems/ruby-2.2.4/bin/fir:22:in `<main>'
        from /Users/weiyuxiang/.rvm/gems/ruby-2.2.4/bin/ruby_executable_hooks:15:in `eval'
        from /Users/weiyuxiang/.rvm/gems/ruby-2.2.4/bin/ruby_executable_hooks:15:in `<main>'
    

    按照错误提示,在 Gemfile 文件里加一句:
    gem "fir"
    即可


    Fastlane能做的事情还有很多,大家可以去好好看看文档,研究一些高级的用法吧!

    最后

    脚本虽好用,但是我们在除了能知道如何使用以外,也应该去深入了解其中的一些原理,做工具的主人而不是工具的奴隶

    参考文档:

    1. 使用fastlane gym实现ipa自动打包脚本
    2. iOS中使用Fastlane实现自动化打包和发布
    3. iOS中fastlane的使用

    相关文章

      网友评论

          本文标题:使用fastlane 实现ipa自动打包脚本

          本文链接:https://www.haomeiwen.com/subject/imbpdftx.html