iOS 端 gitlab + fastlane 持續(xù)集成實(shí)踐

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

1 背景

由于持續(xù)集成對于整個開發(fā)流程的提速十分明顯, 故在生產(chǎn)項(xiàng)目中引入持續(xù)集成工具.

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

其中 gitlab runner 負(fù)責(zé)將遠(yuǎn)程倉庫克隆到 runner 機(jī)器上, 然后執(zhí)行在 .gitlab-ci 文件中配置的指令, 而這些指令一般都是和 fastlane 相關(guān)的.

2 詳細(xì)過程

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

Xcode 工程中, 需要進(jìn)行持續(xù)集成的 target 需要將其 scheme 設(shè)置為 shared.另外最好是每個 target 都有對應(yīng)的 UnitTest 和 UITest, 因?yàn)槌掷m(xù)集成過程中可以進(jìn)行自動化測試.

2.2 節(jié)點(diǎn)(Runner)的配置

需要在macos系統(tǒng)的機(jī)器上配置runner, 當(dāng)有代碼push到gitlab上面時, gitlab的ci功能會自動使用已經(jīng)注冊到gitlab的runner來對工程進(jìn)行自動化測試和構(gòu)建打包.

由于當(dāng)前 iOS 的節(jié)點(diǎn)只能是 macos 機(jī)器(因?yàn)橹荒苡肵code 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 程序添加執(zhí)行權(quán)限:

    sudo chmod +x /usr/local/bin/gitlab-runner
    
  3. 運(yùn)行runner并進(jìn)行配置:

    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的對應(yīng)倉庫的設(shè)置中查詢到
    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 程序安裝為一個服務(wù)并啟動它:

    cd ~
    gitlab-runner install
    gitlab-runner start
    

    重啟系統(tǒng), runner 便運(yùn)行起來了.

2.3 gitlab 倉庫配置

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

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

2.4 節(jié)點(diǎn)上 fastlane 的安裝和配置

fastlane 安裝詳見官網(wǎng). 項(xiàng)目中可以使用 fastlane init 進(jìn)行初始化, 然后再按照實(shí)際情況修改配置文件即可.

最后一句話總結(jié): 只要 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

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

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

# 將打包好的release 包上傳到 app-store
deliver_project:
  stage: deliverP
  script:
# 其中的內(nèi)容也可以在單獨(dú)的文件中配置
    - fastlane deliver -u "蘋果應(yīng)用商店 ID" -a "應(yīng)用的 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 執(zhí)行 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 中是執(zhí)行 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 插件的相關(guān)配置:

output_directory "./build"  # 輸出目錄是工程根目錄下的build文件夾.
export_options(
  thinning: "<thin-for-all-variants>"
)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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