一彤敛、目的
Git
代碼管理工具碍庵,通過Gitlab-runner
實(shí)現(xiàn)CI/CD
的相關(guān)功能。解決開發(fā)人員打包問題割按,也可以讓測試同事更清楚的知道測試的是那一個(gè)版本走哺。
二、適用范圍
適用于通過Git
管理的相關(guān)項(xiàng)目哲虾,需要實(shí)現(xiàn)CI/CD
的功能丙躏,如App
,前端代碼打包并上傳到對應(yīng)的環(huán)境束凑。
三晒旅、環(huán)境配置
3.1 brew
安裝
Mac
安裝GitLab-Runner
需要依賴于brew
的安裝,檢查brew
安裝汪诉,如果沒有安裝废恋,執(zhí)行命令如下:
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
3.2 GitLab-Runner
安裝
用homebrew
進(jìn)行安裝,在命令行中直接執(zhí)行扒寄,執(zhí)行命令如下:
brew install gitlab-runner
四鱼鼓、GitLab-Runner注冊
4.1 查看Git
參數(shù)
打開Settings->CI/CD
頁面,選擇Runners Settings
该编,左側(cè)會(huì)顯示與當(dāng)前項(xiàng)目相關(guān)的參數(shù)迄本。
4.2 客戶端注冊runner
打開MAC
應(yīng)用Terminal
,輸入注冊命令课竣,如下:
gitlab-runner register
指定git
的URL
(輸入圖一里面URL
地址嘉赎,私有git
的路徑)
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
指定gitlab-runner
的token
(輸入圖一里面token
,項(xiàng)目的token
于樟,用于關(guān)聯(lián)runner
和項(xiàng)目)
Please enter the gitlab-ci token for this runner:
給tag
的描述(輸入一個(gè)描述公条,runner
的名字,用于區(qū)分runner
)
Please enter the gitlab-ci description for this runner:
關(guān)聯(lián)git
和runner
的tag
(輸入這個(gè)runner
需要執(zhí)行的Tag
標(biāo)簽迂曲,用于匹配任務(wù)(jobs
)和執(zhí)行任務(wù)的設(shè)備(runners
))
Please enter the gitlab-ci tags for this runner (comma separated):
選擇runner
的執(zhí)行環(huán)境(shell
(Mac
可以在本機(jī)器上運(yùn)行)靶橱,執(zhí)行環(huán)境)
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
注冊成功提示
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
4.3 查看runner
配置
當(dāng)我們完成設(shè)置后,可通過vi ~/.gitlab-runner/config.toml打開runner
的配置文件看到之前配置的內(nèi)容路捧。
配置完成后可以在GitLab
中查看到對應(yīng)配置的Rurnner
关霸,如圖:
點(diǎn)擊
Runner
編號(hào)時(shí)(如:ee42d138
)可以查看到當(dāng)前Runner
的詳細(xì)信息,設(shè)置成如下顯示:激活
-
Runners
暫停后將不能接受新jobs
被保護(hù)
- 這個(gè)
runner
只能運(yùn)行被保護(hù)分支上觸發(fā)的的流水線
運(yùn)行沒有標(biāo)簽的作業(yè)
- 表示此
Runner
可以運(yùn)行沒有任何標(biāo)簽的作業(yè)
鎖定到當(dāng)前項(xiàng)目
-
Runner
一旦被鎖定鬓长,將不能被指派給其它的項(xiàng)目
五谒拴、啟動(dòng)GitLab-Runner
當(dāng)所有不是執(zhí)行后尝江,在Runners settings
會(huì)顯示runner
的狀態(tài)涉波,顯示為綠色,則runner
配置成功。
cd ~
gitlab-runner install
gitlab-runner start
另外還有一些其他的GitLab-Runner
的相關(guān)命令啤覆,如:
gitlab-runner stop
gitlab-runner restart
六苍日、iOS
項(xiàng)目文件配置
所有持續(xù)打包的項(xiàng)目里面需要加入.gitlab-ci.yml
,git push
后會(huì)自動(dòng)識(shí)別.gitlab-ci.yml
文件中的配置窗声,在CI/CD
-> Pipeline(流水線)
中就會(huì)顯示一條記錄相恃。
.gitlab-ci.yml
文件,示例如下:
before_script:
- export LC_ALL=en_US.UTF-8
- chmod 777 shell.sh
stages:
- deploy
- alpha
- prod
deploy:
stage: deploy
script:
- ./shell.sh
environment:
name: deploy
artifacts:
paths:
- IPADir/Release/*.ipa
alpha:
stage: alpha
script:
- ./shell.sh
environment:
name: test
artifacts:
paths:
- IPADir/Release/*.ipa
prod:
stage: prod
script:
- ./shell.sh
environment:
name: production
artifacts:
paths:
- IPADir/Release/*.ipa
when: manual
腳本解析:
before_script
: 覆蓋 job
執(zhí)行前需要執(zhí)行的腳本設(shè)置
stages
: 定義當(dāng)前 job
運(yùn)行在那個(gè)階段
tags
:通過 tags
確定使用指定還是使用通用部署程序笨觅。
script
: 需要在 執(zhí)行的腳本
environment
: 定義由該作業(yè)執(zhí)行部署的環(huán)境的名稱
artifacts
: 定義作業(yè)工件列表
expire_in
:過期時(shí)間
shell.sh
文件處理拦耐,如下代碼:
#目錄處理
if [ ! -d ./IPADir ];
then
mkdir -p IPADir;
fi
rm -rf build;
rm -rf Podfile.lock;
#pod安裝
pod install;
#工程絕對路徑
project_path=$(cd `dirname $0`; pwd)
echo '///-----------'
echo '///--'${project_path}
echo '///-----------'
#工程名
project_name=Scale_Demo
#scheme名
scheme_name=Scale_Demo
#打包模式 Debug/Release
development_mode=Release
#build文件夾路徑
build_path=build
#plist文件所在路徑
exportOptionsPlistPath=exportTest.plist
#導(dǎo)出.ipa文件所在路徑
exportIpaPath=IPADir/${development_mode}
#導(dǎo)出.ipa的相關(guān)證書配置
exportOptionsPlistPath=exportTest.plist
#正在清理工程
xcodebuild clean -configuration ${development_mode} -quiet || exit
#正在編譯工程
xcodebuild \
archive -workspace ${project_name}.xcworkspace \
-scheme ${scheme_name} \
-configuration ${development_mode} -destination 'generic/platform=iOS' \
CODE_SIGN_IDENTITY="Apple Development: **************" \
PROVISIONING_PROFILE="8a6dae04-849e-4dc7-aacf-7a6bf558741d" \
PRODUCT_BUNDLE_IDENTIFIER="com.BB.Scale-Demo" \
-archivePath ${build_path}/${project_name}.xcarchive -allowProvisioningUpdates -quiet || exit
#導(dǎo)出ipa包
xcodebuild -exportArchive -archivePath ${build_path}/${project_name}.xcarchive \
-configuration ${development_mode} \
-exportPath ${exportIpaPath} \
-exportOptionsPlist ${exportOptionsPlistPath} \
-quiet || exit
if [ -e $exportIpaPath/*.ipa ]; then
#ipa包已導(dǎo)出
else
#ipa包導(dǎo)出失敗
fi
#打包ipa完成
exit 0
CODE_SIGN_IDENTITY
為p12
文件的證書信息
PROVISIONING_PROFILE
profile
文件中的UUID
編號(hào)
PRODUCT_BUNDLE_IDENTIFIER
打包的Bundle Identifier
-destination
設(shè)置打包平臺(tái)
導(dǎo)出ipa
包的plist
文件配置,如下代碼:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>provisioningProfiles</key>
<dict>
<key>com.BB.Scale-Demo</key>
<string>Dev_1231</string>
</dict>
<key>method</key>
<string>development</string>
<key>compileBitcode</key>
<false/>
</dict>
</plist>