前言
按照 iOS使用Jenkins自動打包+上傳到 fir+釘釘通知(一) 的步驟來配置已可快速實現(xiàn)自動打包了。但測試、產(chǎn)品、設計人員反饋,通過復制打包任務的方式新建任務,仍顯不夠方便。鑒于此,提供一種通過選擇分支和配置來打包的方式就能簡化打包流程。
參數(shù)化構(gòu)建頁面概覽如下:

參數(shù)化構(gòu)建概覽
使用方法
- 在任務詳情頁,點擊 Build with Parameters
- 選擇 打包分支
- 選擇 打包配置
- 點擊 開始構(gòu)建 即可打包。
多次點擊 開始構(gòu)建 會創(chuàng)建多個任務,按照先后順序排隊,上一個任務完成后,自動開始下一個打包任務。
參數(shù)化構(gòu)建
選擇分支
步驟一 使用選擇分支的功能,需要先安裝 Git Parameter 插件。

Git Parameter
在 Jenkins 首頁-系統(tǒng)管理-插件管理-Installed plugins 頁面的輸入框中,輸入Git Parameter,選中Git Parameter,然后安裝。安裝完成后,在網(wǎng)址中輸入鏈接 http://localhost:8080/restart 重啟 Jenkins。
步驟二 安裝好插件后,進入任務詳情頁,點擊 配置-General,然后勾選上 參數(shù)化構(gòu)建過程。

勾選參數(shù)化構(gòu)建過程
步驟三 添加參數(shù)

添加參數(shù)
勾選上 參數(shù)化構(gòu)建過程 后,點擊 添加參數(shù) ,在下拉菜單中選中 Git 參數(shù)。
步驟四 配置 Git 參數(shù)
- 在 Git 參數(shù)-名稱中輸入自定義名稱,如:branchName。
- 在參數(shù)類型選項中選擇-分支
- 在默認值中輸入默認的打包分支
-
勾選上 Required Parameter
配置 Git 參數(shù) - 配置完參數(shù)后,在源碼管理中,將指定分支中的分支名換成
$branchName即可將選中的分支用于打包分支。
在這里插入圖片描述
打包配置選項
配置好了選擇分支的功能,還要再配置一下選擇 debug/release 的功能。點擊 添加參數(shù) ,在下拉菜單中選中 選項參數(shù)。

配置選項
- 在 選項參數(shù)-名稱中輸入自定義名稱,如: configuration。
- 在選項中輸入選項 Debug、Release,使用換行來分隔選項。
- 描述可輸入備注信息,可不填。
配置
經(jīng)過上面的配置后,點擊 應用-保存 后,即可在任務詳情頁看到 Build with Parameters 選項了。
[圖片上傳失敗...(image-b36150-1676282831388)]
在打包腳本中,使用 $configuration 就可以獲取到選中的打包配置了。
附加參數(shù)
在自動打包應用中,根據(jù)反饋,希望知道是誰啟動的打包任務,使用哪臺打包機器打的包?;诖耍枰~外的附加參數(shù)。
- shell 腳本中使用
${USER}可以獲取到當前電腦登錄的用戶名。 - shell 腳本中使用
$BUILD_USER可以獲取到 build user vars plugin 插件提供的環(huán)境變量,代表登錄 jenkins 的用戶名。安裝 build user vars plugin 插件的方法,不再贅述。
build user vars plugin
啟動電腦自動啟動 Jenkins 服務
啟動電腦自動啟動 Jenkins 服務可參考文章:Mac開機自動運行shell腳本
git fetch --tags 超時
打包時偶爾遇到打包失敗的情況,查看打包日志輸出發(fā)現(xiàn)是 git fetch --tags 命令超時,原因是網(wǎng)速差的情況下請求超時導致的。解決此問題,可參考文章:Jenkins ERROR: Timeout after 10 minutes

git fetch --tags 超時
完整打包腳本
經(jīng)過上面的配置后,打包腳本也要隨之修改。

打包腳本
腳本如下:
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
cd ~/projectName
git fetch
lastName="${GIT_BRANCH##*/}"
currentBranch="$(git rev-parse --abbrev-ref HEAD)"
macUser="${USER}"
configuration="${configuration}"
echo "currentBranch=$currentBranch, lastName=$lastName, macUser=$macUser, configuration=$configuration"
if [ "$currentBranch" != "$lastName" ]; then
time=$(date "+%Y-%m-%d %H:%M:%S")
git stash -u -m "$BRANCH_NAME $time"
git checkout $lastName
fi
git pull
pod install
# 更新插件。fir_cli 插件不及時更新的話,可能會導致無法上傳應用文件。
# sudo 是以管理員身份執(zhí)行命令,在<< EOF與EOF之間輸入你的開機密碼
sudo -S bundle exec fastlane update_plugins << EOF
EOF
# 安裝 fir 插件。sudo 是以管理員身份執(zhí)行命令,在<< EOF與EOF之間輸入你的開機密碼
sudo -S fastlane add_plugin fir_cli << EOF
EOF
if [ "$configuration" = "Debug" ]; then
echo "fastlane project_Debug: macUser=$macUser BUILD_USER = $BUILD_USER, BRANCH_NAME = $GIT_BRANCH, jobName = $JOB_NAME outputDirectory = /Users/username/debug/"
bundle exec fastlane project_Debug branchName:$GIT_BRANCH jobName:$JOB_NAME changeLog:"$SCM_CHANGELOG" buildUser:$BUILD_USER macUser:$macUser outputDirectory:"/Users/username/debug/"
else
echo "fastlane project_Release: macUser=$macUser BUILD_USER = $BUILD_USER, BRANCH_NAME = $GIT_BRANCH, jobName = $JOB_NAME outputDirectory = /Users/username/release/"
bundle exec fastlane project_Release branchName:$GIT_BRANCH jobName:$JOB_NAME changeLog:"$SCM_CHANGELOG" buildUser:$BUILD_USER macUser:$macUser outputDirectory:"/Users/username/release/"
fi
Fastlane 配置
腳本配置了參數(shù)后,在 fastlane 的配置文件 Fastfile 中接收參數(shù),然后輸出到釘釘消息中。
default_platform(:ios)
platform :ios do
desc "iOS 自動打包"
lane :project_Debug do |options|
scheme_name = "project"
output_directory = "./debug/"
custom_directory = options[:outputDirectory]
if !(custom_directory.nil? || custom_directory.empty?)
output_directory = custom_directory
end
puts "打包輸出路徑為 #{output_directory}"
#increment_build_number
#version = get_version_number(xcodeproj: "project.xcodeproj", target: "#{scheme_name}")
buildNumber = get_build_number
output_name = "#{scheme_name}_#{buildNumber}_#{Time.now.strftime('%Y%m%d%H%M%S')}.ipa"
gym(scheme: scheme_name,
workspace: "project.xcworkspace",
include_bitcode: false,
configuration: "Debug",
include_symbols: true,
export_method: "development",
output_directory: output_directory,
build_path: output_directory,
archive_path: output_directory,
output_name: output_name)
branchName = options[:branchName]
jobName = options[:jobName]
buildUser = options[:buildUser]
macUser = options[:macUser]
changeLog = options[:changeLog]
answer = fir_cli api_token:"xxxx", need_release_id: true
puts "上傳后的結(jié)果:#{answer}"
download_url = "https://hey.scandown.com/#{answer[:short]}?release_id=#{answer[:release_id]}"
dingdingMsg = "打包結(jié)果通知:Jenkins 打包成功。Debug 開發(fā)包。\n打包者:#{buildUser}\n分支名:#{branchName}\n任務名:#{jobName}\n打包使用的是#{macUser}的電腦\n下載二維碼鏈接:#{download_url} \n修改日志: \n#{changeLog} \n"
puts "打包結(jié)束時,輸出文案:#{dingdingMsg}"
#fir_cli api_token:"xxxx", dingtalk_at_all: true, #dingtalk_access_token:"xxxx", #dingtalk_custom_message: dingdingMsg
# 構(gòu)造消息格式
text = {
"at": {
"isAtAll": false
},
"text": {
"content": "#{dingdingMsg}"
},
"msgtype": "text"
}
puts "發(fā)送的釘釘消息:#{text}"
dingTalk_url = "https://oapi.dingtalk.com/robot/send?access_token=xxxx"
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 = text.to_json
response = https.request(request)
puts "------------------------------"
puts "Response #{response.code} #{response.message}: #{response.body}"
end
lane :project_Release do |options|
scheme_name = "project"
output_directory = "./release/"
custom_directory = options[:outputDirectory]
if !(custom_directory.nil? || custom_directory.empty?)
output_directory = custom_directory
end
puts "打包輸出路徑為 #{output_directory}"
#increment_build_number
#version = get_version_number(xcodeproj: "project.xcodeproj", target: "#{scheme_name}")
buildNumber = get_build_number
output_name = "#{scheme_name}_#{buildNumber}_#{Time.now.strftime('%Y%m%d%H%M%S')}.ipa"
gym(scheme: scheme_name,
workspace: "project.xcworkspace",
include_bitcode: false,
configuration: "Release",
include_symbols: true,
export_method: "development",
output_directory: output_directory,
build_path: output_directory,
archive_path: output_directory,
output_name: output_name)
branchName = options[:branchName]
jobName = options[:jobName]
buildUser = options[:buildUser]
macUser = options[:macUser]
changeLog = options[:changeLog]
answer = fir_cli api_token:"xxxxxx", need_release_id: true
puts "上傳后的結(jié)果:#{answer}"
download_url = "https://hey.scandown.com/#{answer[:short]}?release_id=#{answer[:release_id]}"
dingdingMsg = "打包結(jié)果通知:Jenkins 打包成功。Release 線上包。\n打包者:#{buildUser}\n分支名:#{branchName}\n任務名:#{jobName}\n打包使用的是#{macUser}的電腦\n下載二維碼鏈接:#{download_url} \n修改日志: \n#{changeLog} \n"
puts "打包結(jié)束時,輸出文案:#{dingdingMsg}"
#fir_cli api_token:"xxxx", dingtalk_at_all: true, #dingtalk_access_token:"xxx", #dingtalk_custom_message: dingdingMsg
# 構(gòu)造消息格式
text = {
"at": {
"isAtAll": false
},
"text": {
"content": "#{dingdingMsg}"
},
"msgtype": "text"
}
puts "發(fā)送的釘釘消息:#{text} "
dingTalk_url = "https://oapi.dingtalk.com/robot/send?access_token=xxxx"
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 = text.to_json
response = https.request(request)
puts "------------------------------"
puts "Response #{response.code} #{response.message}: #{response.body}"
end
end



