生成 iOS 代碼簽名證書(shū)與 Provisioning Profile)
fastlane 如何用 cert 和 sigh 自動(dòng)生成 iOS 代碼簽名證書(shū)與 Provisioning Profile【免費(fèi)下載鏈接】fastlane The easiest way to automate building and releasing your iOS and Android apps項(xiàng)目地址: https://gitcode.com/GitHub_Trending/fa/fastlane當(dāng)你要把 iOS 應(yīng)用的構(gòu)建放進(jìn) CI 或自動(dòng)化腳本時(shí)代碼簽名證書(shū)和 Provisioning Profile 是繞不開(kāi)的兩個(gè)文件證書(shū)過(guò)期、Profile 失效都會(huì)讓打包直接失敗。fastlane 提供cert和sigh兩個(gè)動(dòng)作來(lái)分別解決這兩件事——cert負(fù)責(zé)創(chuàng)建并安裝代碼簽名證書(shū)sigh負(fù)責(zé)創(chuàng)建、續(xù)期、下載并修復(fù) Provisioning Profile。本文說(shuō)明如何用這兩個(gè)動(dòng)作完成“證書(shū) Profile”的自動(dòng)化生成以及每一步如何確認(rèn)結(jié)果。適用前提擁有 Apple Developer Portal開(kāi)發(fā)者門(mén)戶(hù)賬號(hào)證書(shū)創(chuàng)建需要 Team Admin 權(quán)限才能創(chuàng)建 Distribution 證書(shū)建議運(yùn)行環(huán)境為 macOScert會(huì)在 macOS 上把證書(shū)導(dǎo)入 Keychain 并做本地安裝校驗(yàn)非 macOS 系統(tǒng)上這兩步會(huì)被跳過(guò)keychain_path、keychain_password等參數(shù)也不支持登錄方式二選一Apple ID 賬號(hào)密碼fastlane 的 CredentialsManager 負(fù)責(zé)管理或 App Store Connect API Key。準(zhǔn)備配置 Appfilecert和sigh都優(yōu)先從Appfile讀取應(yīng)用的 bundle identifier 與 Apple ID無(wú)需每次都傳參數(shù)。倉(cāng)庫(kù)中的 Appfile 模板AppfileTemplate展示了最小配置app_identifier(com.example.app) # 你的 App bundle identifier apple_id(youexample.com) # Apple Developer Portal 用戶(hù)名如果在多個(gè)團(tuán)隊(duì)之間切換還可以配置 team_id / team_name對(duì)應(yīng)cert、sigh的team_id、team_name參數(shù)。確認(rèn)某個(gè)動(dòng)作支持哪些參數(shù)和環(huán)境變量可以直接運(yùn)行fastlane action cert fastlane action sigh第一步用 cert 生成并安裝簽名證書(shū)最基本的命令是fastlane certsigh/cert的文檔明確指出在 fastlane 中cert是get_certificates的別名sigh是get_provisioning_profile的別名見(jiàn) get_certificates 文檔、get_provisioning_profile 文檔。cert的執(zhí)行邏輯由 Runner 實(shí)現(xiàn) 決定是先檢查本地機(jī)器上是否已安裝可用的簽名證書(shū)已存在則直接復(fù)用只有在需要新建證書(shū)時(shí)才會(huì)創(chuàng)建新的私鑰、創(chuàng)建證書(shū)簽名請(qǐng)求CSR、向 Apple 請(qǐng)求生成證書(shū)下載并安裝到本地 Keychain。如果 Appfile 中沒(méi)寫(xiě)apple_id可以在命令行直接傳入 Apple IDfastlane cert -u certexample.com常用參數(shù)均可通過(guò)fastlane action cert查看也支持對(duì)應(yīng)環(huán)境變量如CERT_DEVELOPMENT、CERT_OUTPUT_PATHdevelopment: true創(chuàng)建 Development 證書(shū)而不是默認(rèn)的 Distribution 證書(shū)generate_apple_certs創(chuàng)建 Xcode 11 及以上使用的 Apple Development / Apple Distribution 證書(shū)macOS Xcode 11 環(huán)境下默認(rèn)為truetype:指定特殊證書(shū)類(lèi)型取值mac_installer_distribution、developer_id_installer、developer_id_application、developer_id_kextoutput_path-o證書(shū)和私鑰文件的保存目錄默認(rèn).keychain_path、keychain_password自定義 Keychain 及其密碼僅 macOS。執(zhí)行后如何確認(rèn)成功在 macOS 上終端會(huì)輸出 “Verifying the certificate is properly installed locally...”成功后顯示Successfully installed certificate 證書(shū)ID如果在 Keychain 中找不到新證書(shū)會(huì)直接報(bào)錯(cuò)退出證書(shū)文件被寫(xiě)入output_path默認(rèn)為當(dāng)前目錄同時(shí)在 lane 中產(chǎn)生兩個(gè)共享值CERT_FILE_PATH證書(shū)文件路徑和CERT_CERTIFICATE_ID證書(shū) ID非 macOS 環(huán)境會(huì)提示 “Skipping verifying certificates...”只生成證書(shū)文件而不做本地安裝。一個(gè)需要知道的邊界cert永遠(yuǎn)不會(huì)撤銷(xiāo)你已有的證書(shū)。當(dāng)證書(shū)數(shù)量達(dá)到 Apple 的上限時(shí)cert會(huì)拋出異常需要你先在開(kāi)發(fā)者門(mén)戶(hù)手動(dòng)撤銷(xiāo)舊證書(shū)才能騰出名額。第二步用 sigh 生成 Provisioning Profilesigh默認(rèn)針對(duì) App Store 類(lèi)型執(zhí)行“創(chuàng)建、修復(fù)、下載”P(pán)rofile 的完整流程fastlane sigh指定 bundle identifier 和 Apple ID 的完整寫(xiě)法fastlane sigh -a com.example.app -u youexample.com按目標(biāo)用途選擇 Profile 類(lèi)型三類(lèi)參數(shù)互斥不能同時(shí)開(kāi)啟fastlane sigh # 默認(rèn) App Store fastlane sigh --adhoc # Ad Hoc fastlane sigh --development # Development與cert配合時(shí)最關(guān)鍵的幾個(gè)參數(shù)-ooutput_pathProfile 保存目錄默認(rèn)當(dāng)前目錄。例如fastlane sigh -o ~/Certificates/force-f無(wú)論現(xiàn)有 Profile 狀態(tài)如何都重新生成得到最長(zhǎng)有效期的 Profile并且會(huì)把所有可用設(shè)備加進(jìn) Profile-qfilename指定生成的 Profile 文件名必須以.mobileprovision結(jié)尾例如fastlane sigh -a com.example.app -u youexample.com -q myProfile.mobileprovision--skip_install只生成 Profile 文件不安裝到本地-ccert_owner_name指定新 Profile 使用哪張證書(shū)例如fastlane sigh -c SunApps GmbH。關(guān)于證書(shū)選擇有一條明確的聯(lián)動(dòng)規(guī)則如果同一個(gè) fastlane lane 中先運(yùn)行了certsigh會(huì)自動(dòng)使用cert剛生成的簽名證書(shū)cert會(huì)把證書(shū) ID 寫(xiě)入SIGH_CERTIFICATE_ID供sigh使用不需要再用-c手動(dòng)指定。執(zhí)行成功后Profile 文件.mobileprovision落在output_path指定的目錄lane 中可以得到SIGH_PROFILE_PATH本次 Profile 的絕對(duì)路徑、SIGH_UUID、SIGH_NAME等共享值供后續(xù)動(dòng)作如構(gòu)建、上傳直接使用。組合到 Fastfile一條 lane 完成證書(shū)和 Profilecert和sigh官方推薦的用法是在同一個(gè) lane 中串聯(lián)。在Fastfile中寫(xiě)入lane :beta do cert sigh(force: true) end然后用fastlane beta觸發(fā)。這里force: true的作用是每次都重新生成 Provisioning Profile保證sigh始終使用本地機(jī)器上剛由cert安裝的那張簽名證書(shū)。已知限制與注意事項(xiàng)文檔明確建議對(duì)于大多數(shù)項(xiàng)目更推薦用 fastlane 的match方案統(tǒng)一生成和維護(hù)證書(shū)與 Profile直接組合certsigh適合想完全掌控簽名流程、并熟悉代碼簽名的場(chǎng)景cert無(wú)法從 Apple 開(kāi)發(fā)者門(mén)戶(hù)下載“已有證書(shū)的私鑰”——私鑰永遠(yuǎn)只存在于當(dāng)初創(chuàng)建證書(shū)的機(jī)器上這也是sigh依賴(lài)本地 Keychain 中證書(shū)的前提sigh使用環(huán)境變量SIGH_CERTIFICATE或-c指定證書(shū)時(shí)也可以傳證書(shū)名稱(chēng)或過(guò)期日期如果sigh提示找不到 App Identifier說(shuō)明開(kāi)發(fā)者門(mén)戶(hù)里還沒(méi)有這個(gè) bundle id需要先創(chuàng)建應(yīng)用標(biāo)識(shí)文檔指出的對(duì)應(yīng)動(dòng)作是producesigh不會(huì)觸碰 Xcode 自己管理的 Profile它只管理自己生成和下載的那一套。完成fastlane beta或分別運(yùn)行fastlane cert和fastlane sigh后你的工作目錄中應(yīng)能看到新生成的.cer證書(shū)文件與.mobileprovisionProfile 文件終端出現(xiàn) “Successfully installed certificate” 即為整條鏈路跑通的標(biāo)志。【免費(fèi)下載鏈接】fastlane The easiest way to automate building and releasing your iOS and Android apps項(xiàng)目地址: https://gitcode.com/GitHub_Trending/fa/fastlane創(chuàng)作聲明:本文部分內(nèi)容由AI輔助生成(AIGC),僅供參考