iOS 端 gitlab + fastlane 持續集成實踐

參考文章: 搭建用于 iOS 工程的 Gitlab CI

1 背景

由于持續集成對于整個開發流程的提速十分明顯, 故在生產項目中引入持續集成工具.

iOS 端主要使用 gitlab + fastlane 進行持續集成. 通過 fir 的 CLI 工具發布 ad-hoc 包用于集成測試. gitlab 負責控制 MAC 機器(runner)上的 fastlane 進行編譯, 打包及 fir 發布.

其中 gitlab runner 負責將遠程倉庫克隆到 runner 機器上, 然后執行在 .gitlab-ci 文件中配置的指令, 而這些指令一般都是和 fastlane 相關的.

2 詳細過程

2.1 Xcode 中的工程配置(必須)

Xcode 工程中, 需要進行持續集成的 target 需要將其 scheme 設置為 shared.另外最好是每個 target 都有對應的 UnitTest 和 UITest, 因為持續集成過程中可以進行自動化測試.

2.2 節點(Runner)的配置

需要在macos系統的機器上配置runner, 當有代碼push到gitlab上面時, gitlab的ci功能會自動使用已經注冊到gitlab的runner來對工程進行自動化測試和構建打包.

由于當前 iOS 的節點只能是 macos 機器(因為只能用Xcode Build工具編譯代碼), 所以下面的 runner 也只是針對 macos 的安裝.

  1. 下載runner程序:

    sudo curl --output /usr/local/bin/gitlab-runner https://gitlab-ci-multi-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-ci-multi-runner-darwin-amd64
    
  2. 為 runner 程序添加執行權限:

    sudo chmod +x /usr/local/bin/gitlab-runner
    
  3. 運行runner并進行配置:

    gitlab-runner register
    
    Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
    https://gitlab.com   #此處填寫工程路徑(gitlab中的)
    Please enter the gitlab-ci token for this runner
    xxx      #此處的token可以在gitlab的對應倉庫的設置中查詢到
    Please enter the gitlab-ci description for this runner
    my-runner    #此處是自定義的描述信息
    INFO[0034] fcf5c619 Registering runner... succeeded
    Please enter the executor: shell, docker, docker-ssh, ssh?
    shell    #由于使用 xcode build, 所以選擇 shell.
    INFO[0037] Runner registered successfully. Feel free to start it, but if it's
    running already the config should be automatically reloaded!
    
  4. 將 runner 程序安裝為一個服務并啟動它:

    cd ~
    gitlab-runner install
    gitlab-runner start
    

    重啟系統, runner 便運行起來了.

2.3 gitlab 倉庫配置

gitlab 中本工程的倉庫中需要添加一個配置文件: .gitlab-ci.yml. 配置文件詳細寫法請閱讀官方文檔.

另外需要設置好對應的 runner. 具體在項目倉庫對應的設置中設置即可.(上一步已經注冊過runner了, 所以可以直接看到)

2.4 節點上 fastlane 的安裝和配置

fastlane 安裝詳見官網. 項目中可以使用 fastlane init 進行初始化, 然后再按照實際情況修改配置文件即可.

最后一句話總結: 只要 Xcode 能夠打包成功, fastlane 就能夠打包成功! 如果不能工作的時候, 請先在Xcode中打包一次. ??

附上 .gitlab-ci 配置示例文件:

stages:
  - test
  - archive
  - upload
  - releaseBuildP
  - deliverP

# 為了讓上個階段生成的文件能在下一個階段被使用, 需要存下來.
cache:
    paths:
      - build/

test_project:
  stage: test
  script:
    - fastlane test
  only:
    - develop
    - release

archive_project:
  stage: archive
  script:
    - fastlane beta
  only:
    - develop

# 測試版本發布到 fir
upload_project:
  stage: upload
  script:
    - fir publish ./build/xxxxxx.ipa -Q -T "此處為 fir 的應用 ID"
  only:
    - develop

release_project:
  stage: releaseBuildP
  script:
    - fastlane releaseAll
  only:
    - release

# 將打包好的release 包上傳到 app-store
deliver_project:
  stage: deliverP
  script:
# 其中的內容也可以在單獨的文件中配置
    - fastlane deliver -u "蘋果應用商店 ID" -a "應用的 bundle id" -i "xxxxxx.ipa" --skip_screenshots true --skip_metadata true --force true
  only:
    - release

fastlane 配置示例文件:

  1. fastfile:
fastlane_version "2.28.2"
default_platform :ios

platform :ios do
before_all do
  cocoapods   # 讓 pod 執行 install 操作
end

lane :test do
  scan
end

lane :beta do
  gym(scheme: "xxxxxx", export_method: "ad-hoc", export_xcargs: "-allowProvisioningUpdates")
end

lane :releaseAll do
  gym(scheme: "sillychildren_inland", export_method: "app-store", export_xcargs: "-allowProvisioningUpdates")
end

另外在 Appfile 中是執行 fastlane init 后的一些信息記錄, 密碼是保存在鑰匙鏈中的:

Appfile:

app_identifier "The bundle identifier of your app"
apple_id "Your Apple email address"

team_id "Developer Portal Team ID"

在 Gymfile 中可以寫一些 gym 插件的相關配置:

output_directory "./build"  # 輸出目錄是工程根目錄下的build文件夾.
export_options(
  thinning: "<thin-for-all-variants>"
)
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容