iOS 基于 launchctl + fastlane + 釘釘機(jī)器人實(shí)現(xiàn)定時(shí)自動(dòng)打包

前言

作為一個(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ā)呆也挺好的。需求整理一下大概是這樣:

  1. 能夠定時(shí)觸發(fā)轩缤;

  2. 自動(dòng)打包命迈;

  3. 自動(dòng)讀取某個(gè)時(shí)間段內(nèi)的 git commit messge 信息當(dāng)做更新日志贩绕;

  4. 打包完成自動(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>

  1. Mac上垄开,執(zhí)行定時(shí)任務(wù):launchctl

  2. Operation not permitted

    1. Fastlane

    2. 釘釘開(kāi)發(fā)文檔


      分享個(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)的命令如下:

  1. Label:對(duì)應(yīng)的需要保證全局唯一性坪哄;

  2. Program:要運(yùn)行腳本;

  3. ProgramArguments:指定要運(yùn)行的腳本势篡;

  4. StartCalendarInterval:運(yùn)行的時(shí)間翩肌,單個(gè)時(shí)間點(diǎn)使用 dict,多個(gè)時(shí)間點(diǎn)使用 array <dict>

  5. StartInterval:時(shí)間間隔禁悠,與 StartCalendarInterval 使用其一念祭,單位為秒

  6. StandardInPath、StandardOutPath碍侦、StandardErrorPath:標(biāo)準(zhǔn)的輸入粱坤、輸出隶糕、錯(cuò)誤文件

相關(guān)字段的解釋如下:

我的配置文件是這樣:

[圖片上傳失敗...(image-210112-1580046740118)]

我們配置在用戶目錄下就行,也就是這個(gè)目錄 ~/Library/LaunchAgents站玄,按照固定的格式新建一個(gè) Plist 文件就行枚驻,可以看到已經(jīng)有一些第三方的任務(wù)在這里了:

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市株旷,隨后出現(xiàn)的幾起案子再登,更是在濱河造成了極大的恐慌,老刑警劉巖晾剖,帶你破解...
    沈念sama閱讀 219,110評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件锉矢,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡齿尽,警方通過(guò)查閱死者的電腦和手機(jī)沽损,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)循头,“玉大人绵估,你說(shuō)我怎么就攤上這事】睿” “怎么了壹士?”我有些...
    開(kāi)封第一講書人閱讀 165,474評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)偿警。 經(jīng)常有香客問(wèn)我躏救,道長(zhǎng),這世上最難降的妖魔是什么螟蒸? 我笑而不...
    開(kāi)封第一講書人閱讀 58,881評(píng)論 1 295
  • 正文 為了忘掉前任盒使,我火速辦了婚禮,結(jié)果婚禮上七嫌,老公的妹妹穿的比我還像新娘少办。我一直安慰自己,他們只是感情好诵原,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,902評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布英妓。 她就那樣靜靜地躺著,像睡著了一般绍赛。 火紅的嫁衣襯著肌膚如雪蔓纠。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 51,698評(píng)論 1 305
  • 那天吗蚌,我揣著相機(jī)與錄音腿倚,去河邊找鬼。 笑死蚯妇,一個(gè)胖子當(dāng)著我的面吹牛敷燎,可吹牛的內(nèi)容都是我干的暂筝。 我是一名探鬼主播,決...
    沈念sama閱讀 40,418評(píng)論 3 419
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼硬贯,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼焕襟!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起饭豹,我...
    開(kāi)封第一講書人閱讀 39,332評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤鸵赖,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后墨状,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體卫漫,經(jīng)...
    沈念sama閱讀 45,796評(píng)論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡菲饼,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,968評(píng)論 3 337
  • 正文 我和宋清朗相戀三年肾砂,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片宏悦。...
    茶點(diǎn)故事閱讀 40,110評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡镐确,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出饼煞,到底是詐尸還是另有隱情源葫,我是刑警寧澤,帶...
    沈念sama閱讀 35,792評(píng)論 5 346
  • 正文 年R本政府宣布砖瞧,位于F島的核電站息堂,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏块促。R本人自食惡果不足惜荣堰,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,455評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望竭翠。 院中可真熱鬧振坚,春花似錦、人聲如沸斋扰。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 32,003評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)传货。三九已至屎鳍,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間问裕,已是汗流浹背哥艇。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,130評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留僻澎,地道東北人貌踏。 一個(gè)月前我還...
    沈念sama閱讀 48,348評(píng)論 3 373
  • 正文 我出身青樓十饥,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親祖乳。 傳聞我的和親對(duì)象是個(gè)殘疾皇子逗堵,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,047評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容