iOS-組件化開發(fā)- 自動(dòng)化

上一篇制作私有庫(組件)的過程中,使用的命令有很多,以下我們就使用自動(dòng)化的方式提交推送私有組件。

Fastlane

fastlane is the easiest way to automate beta deployments and releases for your iOS and Android apps. ?? It handles all tedious tasks, like generating screenshots, dealing with code signing, and releasing your application.
簡(jiǎn)而言之,就是一堆ruby腳本集合,使用Actions機(jī)制,實(shí)現(xiàn)自動(dòng)化。

  • 安裝

    • $ gem install fastlane -NV
    • $ brew cask install fastlane
  • 創(chuàng)建

    標(biāo)準(zhǔn)創(chuàng)建,是使用$ fastlane init

    此處,我們是簡(jiǎn)單創(chuàng)建

    • 在私有庫(組件)的根目錄創(chuàng)建fastlane文件夾和Fastfile文件

      36.png

    • 配置Fastfile文件

      # 描述航道
      desc "pod私有庫自動(dòng)化"
      
      # 定義航道, 航道名稱是 repo
      lane :repo do |options|
      target = options[:target]
      tag = options[:tag]
      
      # 定義action
      
      #測(cè)試工程install
      # pod install
      cocoapods(
      podfile: "./Example/Podfile"
      )
      
      # 提交本地倉庫
      # git add .
      git_add(path: ".")
      # git commit -m "xxx"
      git_commit(path: ".", message: "維護(hù)pod私有庫")
      
      # 推送遠(yuǎn)程倉庫
      # git push origin master
      push_to_git_remote
      
      # 判斷是否有該版本, 如果有就先刪除
      if git_tag_exists(tag:tag)
      UI.message("Found tag #{tag} ??, remove it")
      remove_tag(tag:tag)
      end
      
      # 打版本標(biāo)簽
      # git tag -a '#{tag}'
      add_git_tag(
      tag: tag
      )
      # 推送到遠(yuǎn)程庫
      # git push --tags
      push_git_tags
      
      # 本地驗(yàn)證私有庫
      # pod lib lint
      pod_lib_lint(allow_warnings: true)
      
      # 推送到私有管理庫
      # pod repo push HQSpecs "#{targetName}.podspec"
      pod_push(path: "#{target}.podspec", repo: "HQSpecs", allow_warnings: true)
      
      end
      

      其中HQSpecs是私有庫管理庫

  • 使用

    使用之前需注意以下幾點(diǎn)

    1. 必須cd到項(xiàng)目的根目錄
    2. 需手動(dòng)配置.podspec文件信息,例如:增加版本號(hào)
    3. 如果是首次創(chuàng)建私有庫,則需手動(dòng)關(guān)聯(lián)遠(yuǎn)程庫,如果是維護(hù)升級(jí),則不需要
    • $ fastlane 航道名稱 tag:版本號(hào) target:組件庫名稱
    • 例:$ fastlane repo tag:0.1.1 target:HQMain
      37.png

      38.png

自定義Action

ActionFastlane自動(dòng)化流程中的最小執(zhí)行單元,體現(xiàn)在Fastfile腳本中的一個(gè)個(gè)命令。上述所用的action,社區(qū)都有提供,但有時(shí),我們所需的action沒有提供,需我們手動(dòng)創(chuàng)建

  • 場(chǎng)景
    • 在制作私有庫(組件)時(shí),在執(zhí)行手動(dòng)或自動(dòng)命令后,本地和遠(yuǎn)程的標(biāo)簽已經(jīng)生成,如果此時(shí)驗(yàn)證沒通過,修改正確后重新執(zhí)行命令,此時(shí)的版本號(hào)如何處理?使用之前的版本會(huì)報(bào)錯(cuò),增加一個(gè)新版本又不合適。
    • 解決方案: 刪除本地和遠(yuǎn)程的版本號(hào)
    • $ git tag -d xxx & $ git push origin :xxx
  • 尷尬
    • Fastlane社區(qū)中,并沒有提供類似刪除git版本號(hào)的action
    • 此時(shí),需要自己創(chuàng)建所需的action
  • 創(chuàng)建action
    • 執(zhí)行命令:$ fastlane new_action

    • 輸入自定義action的名稱

      39.png

      40.png

    • 編輯remove_tag.rb

      • 核心代碼如下:
      def self.run(params)
          tag = params[:tag]
          isRemoveRemoteTag = params[:isRemoveRemoteTag]
                    
          cmd = []
          cmd << "git tag -d #{tag} "
              
          if isRemoveRemoteTag
              cmd << " git push origin :#{tag}"
          end
            
          result = Actions.sh(cmd.join('&'));
          return result
      end
               
      def self.available_options
          [
              FastlaneCore::ConfigItem.new(key: :tag,
                                                     description: "被刪除的標(biāo)簽",
                                                     optional: false,
                                                     is_string: true
                                                     ),
              FastlaneCore::ConfigItem.new(key: :isRemoveRemoteTag,
                                                     description: "是否刪除遠(yuǎn)程標(biāo)簽",
                                                     optional: true,
                                                     is_string: false,
                                                     default_value: true),
          ]
      end
      
    • 查看remove_tag.rb編輯后的文檔

      • $ fastlane action remove_tag
      41.png
  • 使用
    • 重新修改Fastfile文件

      • 在添加版本之前,先判斷該版本是否存在,如果存在,則先刪除該版本后再添加
      if git_tag_exists(tag:tag)
          UI.message("Found tag #{tag} ??, remove it")
          remove_tag(tag:tag)
      end
      
    • 驗(yàn)證

      • $ fastlane repo tag:0.1.1 target:HQMain
      42.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容