fastlane與持續集成

fastlane init

若是Xcode升級到了最新版本,請執行sudo gem install fastlane,確保安裝最新版本的fastlane。segmentfault

安裝完成後,於項目根路徑下執行fastlane init以初始化項目的fastlane配置,按照提示輸入Apple ID。xcode

若是你開啓了Two Factor Authentication,fastlane會要求你輸入一串6位數字的驗證碼。看下你的iPhone,它會提示「您的Apple ID正用於在XX市,XX省附近的設備上登錄。「點擊容許就會顯示驗證碼。ruby

fastlane會執行一些xcodebuild命令,有可能因超時而失敗,默認的timeout是10秒,retry times是4次,通常只須要把timeout延長就行了,方法是添加環境變量FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT,如
export FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT=100cookie

Appfile and Fastfile

執行fastlane init後,當前路徑下會出現一個fastlane/,下面有兩個配置文件,Appfile和Fastfile。session

Appfile用於配置全局的App ID,Apple ID和Team ID。app

使用fastlane最主要的工做在於配置Fastfile,它負責定義稱爲lane的fastlane任務。fastlane init爲咱們建立了一個已經可使用的Fastfile模版。ide

before_all do
    # ENV["SLACK_URL"] = "https://hooks.slack.com/services/..."
    cocoapods
    # carthage
end

before_all就是先於全部lane執行的任務,cocoapods默認是開啓的,也就是每次執行fastlane任務都會先執行pod install。通常來講這並非咱們想要的,建議讀者刪除或註釋掉這一行。工具

lane :test do
    scan
end

test任務經過調用scan執行自動化測試。scan是fastlane工具箱中的一個工具,專門用於執行自動化測試。測試

lane :beta do
    # match(type: "appstore") # more information: https://codesigning.guide
    gym # Build your app - more options available
    pilot

    # sh "your_script.sh"
    # You can also use other beta testing services here (run `fastlane actions`)
end

beta任務執行App的beta提測。gym和pilot是fastlane工具箱中的兩個工具,分別執行打包和上傳testflight。對於有不止一個scheme的項目,要爲gym提供參數。ui

gym(
        scheme: "SegmentFault",
        export_method: "app-store",
        export_options: {
            provisioningProfiles: {
                "com.segmentfault.appid" => "ProvisioningProfileName",
            }
        }
)

提測TestFlight就變成了敲一個fastlane beta命令這麼簡單——其實也沒那麼簡單,iTunes Connect會要求你登錄,可能還要作Two Factor Auth——下文會聊到如何連這個登錄的步驟都省略。

通常來講testflight測試是App上架App Store前最後階段的測試,公司裏還會有平常提測,多采用adhoc或enterprise方式。以enterprise爲例。

lane :enterprise do
    gym(scheme: "SegmentFault", export_method: "enterprise")
end

上面這個lane是Fastfile模版裏沒有的,須要咱們本身添加。提測enterprise就是敲個fastlane enterprise命令。
這裏要注意的是,在執行fastlane enterprisefastlane beta以前,要確保項目的code signing配置無誤,enterprise和testflight的配置不同,於是在兩者間切換時要注意修改配置。

Fastfile模版還爲咱們提供了一個release任務,這個即是上傳app store了。

More about Fastfile

Fastfile是個ruby腳本。
——也就是說你能夠把它當成一個ruby腳原本寫。你不須要把本身侷限於只作一些「規定動做」,你盡能夠在lane中作一些「自選動做」。發郵件啊,或是發slack消息啊,具體作什麼就隨您方便了。以slack爲例。

before_all do
    ENV["SLACK_URL"] = "https://hooks.slack.com/services/...."
end

desc "Runs all the tests"
lane :test do
    scan(scheme: "SegmentFault")
    slack
end

lane :enterprise do
    slack(
      message: "SegmentFault started enterprise archiving."
    )
    gym(scheme: "SegmentFault", export_method: "enterprise")
    slack(
      message: "SegmentFault finished enterprise archiving. Uploading...."
    )
end

Continuous Integration

到此爲止咱們已經看到了fastlane能夠爲咱們節約多少時間精力。若是把它跟其餘工具好比jenkins結合,搭建持續集成服務,還能夠節省更多時間精力——還能夠更酷。

本文以介紹fastlane爲主,關於持續集成和jenkins請讀者本身去了解。

上文提到提測testflight須要登錄iTunes Connect,可是在持續集成的「全自動化」環境中,這種須要管理員敲鍵盤的操做是不存在的。
圖片描述

這就須要咱們預先維護一個login session。具體操做以下

  1. appleid.apple.com/account/manage上生成一個application specific password。
  2. 經過環境變量FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD提供這個application specific password。
  3. 執行fastlane spaceauth -u user@email.com,生成session cookie。
  4. 經過環境變量FASTLANE_SESSION提供session cookies。

Common problems

這裏簡述fastlane使用中的常見問題及解決方法——重要之處在於你要經過閱讀log找出問題是什麼。

Problem 1
xcodebuild命令執行超時。
上文提到過這個問題。
Solution
添加環境變量FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT,如
export FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT=100

Problem 2
錯誤的provisioning profile。
好比你要打testflight包,卻提供了enterprise的provisioning profile。

Error Domain=IDEProfileQualificationErrorDomain Code=3 "Provisioning profile "SegmentFault" is not an "iOS App Store" profile."

Solution
這是一個常規的code signing問題,經過Xcode很容易解決,把錯誤的provisioning profile替換成正確的就行了。有時也可能provisioning profile是正確的,而其餘配置與其不匹配,那麼修改其餘配置使之匹配就行了。

Problem 3
session cookie過時。

Login to iTunes Connect (user@email.com)
Two Factor Authentication for account 'user@email.com' is enabled
Your session cookie has been expired.

Solution

  1. 執行fastlane spaceauth -u user@email.com,生成session cookie。
  2. 經過環境變量FASTLANE_SESSION提供session cookie。

Problem 4
重複/太低的版本號。

ERROR ITMS-90189: "Redundant Binary Upload. There already exists a binary upload with build '4' for version '2.0.9'"
ERROR ITMS-90186: "Invalid Pre-Release Train. The train version '2.0.9' is closed for new build submissions"
ERROR ITMS-90062: "This bundle is invalid. The value for key CFBundleShortVersionString [2.0.9] in the Info.plist file must contain a higher version than that of the previously approved version [2.1.0]."

上面這段error log反饋了兩個問題,一是version 2.0.9已經有build 4了,不要重複提交version與build相同的binary;二是2.1.0版本已經提交過了,不要提交比它低的版本。
Solution修改版本號。

相關文章
相關標籤/搜索