[純工程] Fastlane 紀錄:如何在 Fastlane 中使用現有的證書進行自動化的動作

Samuel
Samuel
Aug 28, 2017 · 8 min read

近期因為跟幾個夥伴合作開發 「Kapi 找咖啡」,前期在趕工的路上…默默地審略掉一些應該要做但是卻偷懶不做的流程;在第一版本趕工上線後再來準備 Fastlane 自動化的設定,這邊稍微紀錄一下第一次設定和想要沿用Certification 時可能會遭遇的問題;Fastlane 初步安裝的動作在這篇文章上面已經寫得很清楚啦,在這邊就不再重複敘述一次;後續的內容會設定為已經完成 Fastlane 的初始化的狀態開始繼續說明。

Fastfile:

Fastfile 的部分其實非常簡單;在流程上其實就依序為:

  1. 透過 Match 來處理相關的 Certification 和配置文件(Configuration),基本上分成三種類型 development、adhoc 和 appstore,會自動去拉取相對應類型的 Certification
  2. 透過 cocoapods 這個指令來運作 pod install
  3. 透過 increment_build_number 這個指令來幫你的 build number 加1
  4. 透過 gym 來進行編譯打包的動作(背後其實就是運作 xcodebuild)
  5. 透過 pilot 來推送到 Testflight,同時還可以設定 Team 等相關資訊
  6. 如果是 release 版本的話就需要搭配 deliver 指令
lane :beta do
match(type: “appstore”)
cocoapods
increment_build_number
gym(
workspace: “../你的 Project.xcworkspace”, #注意這邊的路徑喔!
scheme: “你的scheme”,
export_method: “app-store”
)
pilot(skip_waiting_for_build_processing: true)
end

當然你也可以透過 Fastlane 的官方網站快速生成 Fastfile 來使用:


因為我並非初始化時就透過 match 來進行證書的設定,在原本的 Developer Certificate 中可能已經有生成好的 Distribution/Developement 的Certificate,在過程中你可能會遇到一些小障礙,希望底下紀錄的內容能夠幫助你跨越這些小障礙。

後續流程

  • 假定你的 Fastlane 初始化完成 → 要確定運行 Fastlane init 的位置是在 xcodeproj 文件同級目錄底下而不是 xcworkspace ,不然在後續的過程中會卡在 increment_build_number 這個指令裡面。
位置放錯的狀況
  • 如果是第一次 Run Fastlane 自動編譯上傳的話,Fastlane 會需要你的 Certificate 才有辦法進行下一步編譯/上傳的動作;這時候如果你完全沒有 Distribution 的 Certificate 的話,可以搭配 Match 來幫你自動生成跟處理,不過因為 KAPI找咖啡之前已經有上架的版本,iOS Distribution 的 Certificate 早就已經開好囉!所以在這邊我將會說明如何將現有的 Certificate 導入到 Match 裡面進行使用:

→ 首先透過下列這段 Ruby code 取得你的 cert_id;基本上只要告訴 Match 你的 cert_id,fastlane 就不會再自動去生成 certificate,只會生成 provisioning profile;所以在已經有Certification 的狀態下,我們需要建立一個 ooo.rb 的檔案然後在terminal 運行來取得你對應 certification 的id:

require ‘spaceship’ Spaceship.login(‘your@appleid’) 
Spaceship.select_team
Spaceship.certificate.all.each do |cert|
cert_type = Spaceship::Portal::Certificate::CERTIFICATE_TYPE_IDS[cert.type_display_id].to_s.split(“::”)[-1]
puts “Cert id: #{cert.id}, name: #{cert.name}, expires: #{cert.expires.strftime(“%Y-%m-%d”)}, type: #{cert_type}”
end

透過運行 ruby ooo.rb ,就可以看到列出來對應到你 your@appleid 的 certification,找到對應的certification把cert_id記錄下來,在最後會用到喔!

→ 從 Apple Developer,Certification 的 Tab 中下載你的.cer

選擇 All 或者 Production
下載你的 Distribution Certification

→ 從 Keychain 中 export 你原先已經設置好的 p12 Key

→ 完成上述兩個步驟你應該會得到 certificate.cer certificate.p12,名字應該是不會一樣啦xD

→ 透過指令 openssl pkcs12 -nocerts -nodes -out key.pem -in certificate.p12 將你的 certificate.p12 製作成 key.pem

→ 然後再透過下列兩個指令完成fastlane match需要的certificate,注意喔!裡面的cert_id要替換成第一個步驟裡面找出來的cert_id,your_password要替換成你自己設定的密碼。

openssl aes-256-cbc -k your_password -in key.pem -out cert_id.p12 -a openssl aes-256-cbc -k your_password -in certificate.cer -out cert_id.cer -a

→ 將生成出來的 cert_id.p12 和 cert_id.cer 上傳到你的 private repository,配合下一個步驟設定git-url 指到這個上傳的 private repository,我設定的資料夾結構如下圖:

就可以把這原先的 certification 設定到 match上面,這整個步驟就告一個段落囉!

  • 透過運行 match init 使定來生成 Match file ,同時會跳出指令要你輸入你存放 Certificate 的 git-url(例如:git@bitbucket.org:oo/ooo.git),如果不小心跳過輸入指令的話可以透過在Matchfile中新增:「git_url "git@bitbucket.org:oo/ooo.git”」來新增上去!不然每次運行 fastlane build的command 都要填寫一次真的很崩潰。
  • 在 Fastlane 運作的過程中可能會遇到 Fastlane 無法修改你的 Build Version 的問題 → 請參考連結的上半部分,設定你的版本以及設定你的 Versioning SystemApple Generic

Ya,大概先記錄到這邊!接下來的開發會導入更多與 fastlane 相關的功能,有想到什麼或是實驗出什麼會在更新在這篇文章裡面來做些簡單的紀錄!也希望能夠幫助到剛入門感受到困難的夥伴;如果發現文章的過程中有任何問題也歡迎指正或討論喔:)

)

Samuel

Written by

Samuel

Hahow 好學校的 Product Owner & Software Developer,Hahow 線上課程 9 折優惠歡迎 Facebook 私訊索取(😎)。產品設計師 / 工程師 / 企業講師 / 內容產出者。個人網站:www.samuel-kao.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade