前言
作為一個(gè)移動(dòng)端程序員,每次 feature add 或者 bug fix 后經(jīng)常要打包交付給 QA,以前傳統(tǒng)的操作都是手動(dòng)點(diǎn)擊 Xcode -> Product -> Archive -> Organizer -> Distrubute App -> ipa 上傳到第三方內(nèi)測(cè)分發(fā)平臺(tái)(蒲公英蔚叨、fir)-> 手動(dòng)填寫更新日志 -> 發(fā)送安裝鏈接到部門群(釘釘或者企業(yè)微信)
,看起來(lái)好像很機(jī)械和繁瑣个曙,又沒(méi)啥技術(shù)含量是吧......
如果能把這部分工作給自動(dòng)化了就好了察郁,每天可以省一點(diǎn)時(shí)間出來(lái)發(fā)呆也挺好的。需求整理一下大概是這樣:
能夠定時(shí)觸發(fā)轩缤;
自動(dòng)打包命迈;
自動(dòng)讀取某個(gè)時(shí)間段內(nèi)的 git commit messge 信息當(dāng)做更新日志贩绕;
打包完成自動(dòng)發(fā)送安裝鏈接到部門群(釘釘或者企業(yè)微信);
實(shí)現(xiàn)
需求一壶愤、定時(shí)任務(wù)
調(diào)研了一下淑倾,Mac OS 可以基于 launchctl 來(lái)配置定時(shí)任務(wù)≌鹘罚可以配置到不同級(jí)別的 LaunchAgents 下娇哆,幾種的區(qū)別如下:
<pre mdtype="fences" cid="n133" lang="shell" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">~/Library/LaunchAgents 由用戶自己定義的任務(wù)項(xiàng)
/Library/LaunchAgents 由管理員為用戶定義的任務(wù)項(xiàng)
/Library/LaunchDaemons 由管理員定義的守護(hù)進(jìn)程任務(wù)項(xiàng)
/System/Library/LaunchAgents 由Mac OS X為用戶定義的任務(wù)項(xiàng)
/System/Library/LaunchDaemons 由Mac OS X定義的守護(hù)進(jìn)程任務(wù)項(xiàng)</pre>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" cid="n147" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;"><plist version="1.0">
<dict>
<key>Label</key>
<string>com.autoArchiveTask.plist</string>
?
<key>Program</key>
<string>/Users/username/Desktop/code/Project/run.sh</string>
?
<key>ProgramArguments</key>
<array>
<string>/Users/username/Desktop/code/Project/run.sh</string>
</array>
<key>StartCalendarInterval</key>
<array>
<dict>
<key>Minute</key>
<integer>00</integer>
<key>Hour</key>
<integer>11</integer>
</dict>
<dict>
<key>Minute</key>
<integer>00</integer>
<key>Hour</key>
<integer>16</integer>
</dict>
</array>
?
<key>StandardOutPath</key>
<string>/Users/username/Desktop/code/Project/run.log</string>
<key>StandardErrorPath</key>
<string>/Users/username/Desktop/code/Project/run.error</string>
</dict>
</plist></pre>
<pre mdtype="fences" cid="n198" lang="shell" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;"># 加載任務(wù), -w選項(xiàng)會(huì)將 plist 文件中無(wú)效的 key 覆蓋掉,建議加上
launchctl load -w xxx.plist
?
刪除任務(wù)
launchctl unload -w xxx.plist
?
查看任務(wù)列表, 使用 grep '任務(wù)部分名字' 過(guò)濾
launchctl list | grep 'xxx'
?
立即執(zhí)行一次任務(wù)勃救,可用來(lái)測(cè)試
launchctl start xxx.plist</pre>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" cid="n208" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">default_platform(:ios)
?
網(wǎng)絡(luò)請(qǐng)求依賴
require 'net/http'
require 'uri'
require 'json'
?
?
platform :ios do
desc "發(fā)布app到 App Store 或者 Fir.im "
lane :customer_hoc do
add actions here: https://docs.fastlane.tools/actions
sh "fastlane adhoc --env Customer"
end
?
desc "發(fā)布app到 App Store 或者 Fir.im "
lane :driver_hoc do
add actions here: https://docs.fastlane.tools/actions
sh "fastlane adhoc --env Driver"
end
?
?
desc "發(fā)布指定Target到 Fir.im"
lane :adhoc do
gym(
clean:true, #打包前clean項(xiàng)目
workspace: "Hedgehog.xcworkspace",
export_method: "ad-hoc", #導(dǎo)出方式
scheme: ENV['SCHEME_NAME'], #scheme
output_name: ENV['SCHEME_NAME']+".ipa", # ipa 文件名
output_directory: "./ipa", #ipa的存放目錄
export_options: {
provisioningProfiles: {
"cn.ccmore.hedgehog.customer"=>"CustomerAdhoc",
"cn.ccmore.hedgehog.driver"=>"DricerAdhoc"
}
}
)
前往fir.im獲取 api token, 將鼠標(biāo)放置右上角賬號(hào)上面, 在下拉窗選擇API token
若使用的蒲公英, 請(qǐng)前往 https://www.pgyer.com/ 查看上傳方法
如果使用Firimfile, 此處為 firim 即可
firim(firim_api_token:'xxxx')
?
釘釘機(jī)器人
app_patch = "ipa/" + ENV['SCHEME_NAME']+".ipa"
app_version = get_ipa_info_plist_value(ipa: app_patch, key: "CFBundleShortVersionString")
app_build_version = get_ipa_info_plist_value(ipa: app_patch, key: "CFBundleVersion")
app_name = get_ipa_info_plist_value(ipa: app_patch, key: "CFBundleDisplayName")
?
?
根據(jù) SCHEME_NAME 區(qū)分下載鏈接
app_url = "https://fir.im/6udv"
?
if ENV['SCHEME_NAME'] == "Driver" then
app_url = "https://fir.im/sa4q"
end
app_icon = "./Hedgehog/ipa/icons/57.png"
dingTalk_url = "https://oapi.dingtalk.com/robot/send?access_token=xxx"
markdown =
{
msgtype: "link",
link: {
text: "iOS #{ENV['SCHEME_NAME']} 更新了0帧!蒙秒!",
title: "iOS #{ENV['SCHEME_NAME']} #{app_version} (#{app_build_version}) 內(nèi)測(cè)版",
picUrl: "#{app_icon}",
messageUrl: "#{app_url}"
}
}
?
uri = URI.parse(dingTalk_url)
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
?
request = Net::HTTP::Post.new(uri.request_uri)
request.add_field('Content-Type', 'application/json')
request.body = markdown.to_json
?
response = https.request(request)
puts "------------------------------"
puts "Response #{response.code} #{response.message}: #{response.body}"
end
?
end</pre>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" cid="n243" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;"># 前往fir.im獲取 api token, 將鼠標(biāo)放置右上角賬號(hào)上面, 在下拉窗選擇API token
若使用的蒲公英, 請(qǐng)前往 https://www.pgyer.com/ 查看上傳方法
如果使用Firimfile, 此處為 firim 即可
firim(firim_api_token:'xxxx') </pre>
<pre mdtype="fences" cid="n95" lang="shell" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">/Users/username/.fastlane/bin/fastlane lane</pre>
-
-
分享個(gè)人技術(shù)學(xué)習(xí)記錄和跑步馬拉松訓(xùn)練比賽、讀書筆記等內(nèi)容税肪,感興趣的朋友可以關(guān)注我的公眾號(hào)「by在水一方」溉躲。
參考鏈接
相關(guān)配置文件已經(jīng)上傳到 GitHub 倉(cāng)庫(kù),地址點(diǎn)擊這里益兄。
[圖片上傳失敗...(image-bee394-1580046740118)]
效果如下:
總共折騰了一兩天時(shí)間锻梳,流程基本都跑通了,還剩抓取指定時(shí)間段內(nèi)的 git commit message 當(dāng)做更新日志的 TODO净捅,總體上還是很愉悅和有成就感的疑枯,以后就可以專心干其他的事情了,打包幾乎無(wú)感蛔六,也不用怕忘記荆永。nice!
總結(jié)
使用全路徑 fastlane 執(zhí)行命令
解決方案:
雖然 cd 到了當(dāng)前項(xiàng)目目錄国章,但還是報(bào) fastlane 找不到
原因:
二具钥、在定時(shí)腳本中直接執(zhí)行 fastlane 打包命令出錯(cuò): /Users/username/Desktop/code/Project/run.sh : fastlane: command not found
[圖片上傳失敗...(image-171f02-1580046740118)]
給足訪問(wèn)權(quán)限就行。系統(tǒng)偏好設(shè)置 -> 安全性與隱私-> 完全磁盤訪問(wèn)權(quán)限液兽,查看是否有勾選?? 在定時(shí)腳本中聲明的解釋執(zhí)行的 shell 的路徑骂删,就是#!/bin/ 后面接的,有 bash 四啰、sh宁玫、 zsh 等,我的是 sh柑晒。沒(méi)有的話就添加進(jìn)去欧瘪。
解決方案:
首先我配置的定時(shí)腳本路徑在 /Users/username/Desktop/code/Project/run.sh,沒(méi)有和定時(shí)任務(wù)的 Plist 配置文件在一個(gè)目錄下匙赞,而配置的定時(shí)腳本聲明的是 #!/bin/sh佛掖,意思是使用 /bin/sh 來(lái)解釋執(zhí)行妖碉,但是卻沒(méi)有給完全磁盤訪問(wèn)的權(quán)限。
[圖片上傳失敗...(image-bb57ee-1580046740118)]
原因:
一苦囱、定時(shí)腳本執(zhí)行 /bin/sh: xxx/run.sh: Operation not permitted
踩坑
其他企業(yè)微信好像也是可以的,可以自行去查看文檔脾猛。
[圖片上傳失敗...(image-8327e3-1580046740118)]
我這邊目前使用的釘釘進(jìn)行協(xié)作撕彤,可以在相關(guān)工作群使用釘釘機(jī)器人自動(dòng)發(fā)送消息。找釘釘群管理員添加一下獲取 token 就行猛拴「Γ可以向這個(gè)地址 https://oapi.dingtalk.com/robot/send?access_token=Your Token
發(fā)送純文本、圖文愉昆、markdown 等格式的消息职员,還可以填寫需要 @ 的測(cè)試妹子們。
需求四跛溉、自動(dòng)發(fā)送安裝消息
TODO: 等待實(shí)現(xiàn)焊切。
需求三、讀取 git commit messge
上傳到第三方內(nèi)測(cè)平臺(tái)(蒲公英芳室、fir等)Fastlane 也有相關(guān)的插件专肪,一行代碼搞定,如 Fir 就是:
由于我這個(gè)是多 target 工程堪侯,所以我這邊的可能多一點(diǎn)配置嚎尤,我的 Fastfile 文件配置如下:
這個(gè)使用 fastlane 就行,很好很強(qiáng)大伍宦。相關(guān)的配置可參見(jiàn)官網(wǎng)芽死,建議使用 brew 方式安裝。配置安裝文檔就行次洼,
需求二关贵、自動(dòng)打包
配置好了就可以加載了,加載后就生效了卖毁,相關(guān)的命令如下:
Label:對(duì)應(yīng)的需要保證全局唯一性坪哄;
Program:要運(yùn)行腳本;
ProgramArguments:指定要運(yùn)行的腳本势篡;
StartCalendarInterval:運(yùn)行的時(shí)間翩肌,單個(gè)時(shí)間點(diǎn)使用 dict,多個(gè)時(shí)間點(diǎn)使用 array <dict>
StartInterval:時(shí)間間隔禁悠,與 StartCalendarInterval 使用其一念祭,單位為秒
StandardInPath、StandardOutPath碍侦、StandardErrorPath:標(biāo)準(zhǔn)的輸入粱坤、輸出隶糕、錯(cuò)誤文件
相關(guān)字段的解釋如下:
我的配置文件是這樣:
[圖片上傳失敗...(image-210112-1580046740118)]
我們配置在用戶目錄下就行,也就是這個(gè)目錄 ~/Library/LaunchAgents站玄,按照固定的格式新建一個(gè) Plist 文件就行枚驻,可以看到已經(jīng)有一些第三方的任務(wù)在這里了: