iOS 基于 launchctl + fastlane + 钉钉机器人实现定时自动打包

前言

作为一个移动端程序员,每次 feature add 或者 bug fix 后经常要打包交付给 QA,以前传统的操作都是手动点击 Xcode -> Product -> Archive -> Organizer -> Distrubute App -> ipa 上传到第三方内测分发平台(蒲公英、fir)-> 手动填写更新日志 -> 发送安装链接到部门群(钉钉或者企业微信),看起来好像很机械和繁琐,又没啥技术含量是吧......

如果能把这部分工作给自动化了就好了,每天可以省一点时间出来发呆也挺好的。需求整理一下大概是这样:

  1. 能够定时触发;

  2. 自动打包;

  3. 自动读取某个时间段内的 git commit messge 信息当做更新日志;

  4. 打包完成自动发送安装链接到部门群(钉钉或者企业微信);

实现

需求一、定时任务

调研了一下,Mac OS 可以基于 launchctl 来配置定时任务。可以配置到不同级别的 LaunchAgents 下,几种的区别如下:

<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 由用户自己定义的任务项
/Library/LaunchAgents 由管理员为用户定义的任务项
/Library/LaunchDaemons 由管理员定义的守护进程任务项
/System/Library/LaunchAgents 由Mac OS X为用户定义的任务项
/System/Library/LaunchDaemons 由Mac OS X定义的守护进程任务项</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选项会将 plist 文件中无效的 key 覆盖掉,建议加上
launchctl load -w xxx.plist

删除任务

launchctl unload -w xxx.plist

查看任务列表, 使用 grep '任务部分名字' 过滤

launchctl list | grep 'xxx'

立即执行一次任务,可用来测试

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)

网络请求依赖

require 'net/http'
require 'uri'
require 'json'


platform :ios do

desc "发布app到 App Store 或者 Fir.im "
lane :customer_hoc do

add actions here: https://docs.fastlane.tools/actions

sh "fastlane adhoc --env Customer"
end

desc "发布app到 App Store 或者 Fir.im "
lane :driver_hoc do

add actions here: https://docs.fastlane.tools/actions

sh "fastlane adhoc --env Driver"
end



desc "发布指定Target到 Fir.im"
lane :adhoc do
gym(
clean:true, #打包前clean项目
workspace: "Hedgehog.xcworkspace",
export_method: "ad-hoc", #导出方式
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, 将鼠标放置右上角账号上面, 在下拉窗选择API token

若使用的蒲公英, 请前往 https://www.pgyer.com/ 查看上传方法

如果使用Firimfile, 此处为 firim 即可

firim(firim_api_token:'xxxx')

钉钉机器人

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")

根据 SCHEME_NAME 区分下载链接

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']} 更新了!!!",
title: "iOS #{ENV['SCHEME_NAME']} #{app_version} (#{app_build_version}) 内测版",
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, 将鼠标放置右上角账号上面, 在下拉窗选择API token

若使用的蒲公英, 请前往 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上,执行定时任务:launchctl

  2. Operation not permitted

    1. Fastlane

    2. 钉钉开发文档


      分享个人技术学习记录和跑步马拉松训练比赛、读书笔记等内容,感兴趣的朋友可以关注我的公众号「by在水一方」。

参考链接

相关配置文件已经上传到 GitHub 仓库,地址点击这里

[图片上传失败...(image-bee394-1580046740118)]

效果如下:

总共折腾了一两天时间,流程基本都跑通了,还剩抓取指定时间段内的 git commit message 当做更新日志的 TODO,总体上还是很愉悦和有成就感的,以后就可以专心干其他的事情了,打包几乎无感,也不用怕忘记。nice!

总结

使用全路径 fastlane 执行命令

解决方案:

虽然 cd 到了当前项目目录,但还是报 fastlane 找不到

原因:

二、在定时脚本中直接执行 fastlane 打包命令出错: /Users/username/Desktop/code/Project/run.sh : fastlane: command not found

[图片上传失败...(image-171f02-1580046740118)]

给足访问权限就行。系统偏好设置 -> 安全性与隐私-> 完全磁盘访问权限,查看是否有勾选☑️ 在定时脚本中声明的解释执行的 shell 的路径,就是#!/bin/ 后面接的,有 bash 、sh、 zsh 等,我的是 sh。没有的话就添加进去。

解决方案:

首先我配置的定时脚本路径在 /Users/username/Desktop/code/Project/run.sh,没有和定时任务的 Plist 配置文件在一个目录下,而配置的定时脚本声明的是 #!/bin/sh,意思是使用 /bin/sh 来解释执行,但是却没有给完全磁盘访问的权限。

[图片上传失败...(image-bb57ee-1580046740118)]

原因:

一、定时脚本执行 /bin/sh: xxx/run.sh: Operation not permitted

踩坑

其他企业微信好像也是可以的,可以自行去查看文档。

[图片上传失败...(image-8327e3-1580046740118)]

我这边目前使用的钉钉进行协作,可以在相关工作群使用钉钉机器人自动发送消息。找钉钉群管理员添加一下获取 token 就行。可以向这个地址 https://oapi.dingtalk.com/robot/send?access_token=Your Token 发送纯文本、图文、markdown 等格式的消息,还可以填写需要 @ 的测试妹子们。

需求四、自动发送安装消息

TODO: 等待实现。

需求三、读取 git commit messge

上传到第三方内测平台(蒲公英、fir等)Fastlane 也有相关的插件,一行代码搞定,如 Fir 就是:

由于我这个是多 target 工程,所以我这边的可能多一点配置,我的 Fastfile 文件配置如下:

这个使用 fastlane 就行,很好很强大。相关的配置可参见官网,建议使用 brew 方式安装。配置安装文档就行,

需求二、自动打包

配置好了就可以加载了,加载后就生效了,相关的命令如下:

  1. Label:对应的需要保证全局唯一性;

  2. Program:要运行脚本;

  3. ProgramArguments:指定要运行的脚本;

  4. StartCalendarInterval:运行的时间,单个时间点使用 dict,多个时间点使用 array <dict>

  5. StartInterval:时间间隔,与 StartCalendarInterval 使用其一,单位为秒

  6. StandardInPath、StandardOutPath、StandardErrorPath:标准的输入、输出、错误文件

相关字段的解释如下:

我的配置文件是这样:

[图片上传失败...(image-210112-1580046740118)]

我们配置在用户目录下就行,也就是这个目录 ~/Library/LaunchAgents,按照固定的格式新建一个 Plist 文件就行,可以看到已经有一些第三方的任务在这里了:

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,117评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,963评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,897评论 0 240
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,805评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,208评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,535评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,797评论 2 311
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,493评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,215评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,477评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,988评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,325评论 2 252
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,971评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,807评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,544评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,455评论 2 266

推荐阅读更多精彩内容