一、目的
Git
代码管理工具,通过Gitlab-runner
实现CI/CD
的相关功能。解决开发人员打包问题,也可以让测试同事更清楚的知道测试的是那一个版本。
二、适用范围
适用于通过Git
管理的相关项目,需要实现CI/CD
的功能,如App
,前端代码打包并上传到对应的环境。
三、环境配置
3.1 brew
安装
Mac
安装GitLab-Runner
需要依赖于brew
的安装,检查brew
安装,如果没有安装,执行命令如下:
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
3.2 GitLab-Runner
安装
用homebrew
进行安装,在命令行中直接执行,执行命令如下:
brew install gitlab-runner
四、GitLab-Runner注册
4.1 查看Git
参数
打开Settings->CI/CD
页面,选择Runners Settings
,左侧会显示与当前项目相关的参数。
4.2 客户端注册runner
打开MAC
应用Terminal
,输入注册命令,如下:
gitlab-runner register
指定git
的URL
(输入图一里面URL
地址,私有git
的路径)
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
指定gitlab-runner
的token
(输入图一里面token
,项目的token
,用于关联runner
和项目)
Please enter the gitlab-ci token for this runner:
给tag
的描述(输入一个描述,runner
的名字,用于区分runner
)
Please enter the gitlab-ci description for this runner:
关联git
和runner
的tag
(输入这个runner
需要执行的Tag
标签,用于匹配任务(jobs
)和执行任务的设备(runners
))
Please enter the gitlab-ci tags for this runner (comma separated):
选择runner
的执行环境(shell
(Mac
可以在本机器上运行),执行环境)
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
配置
当我们完成设置后,可通过vi ~/.gitlab-runner/config.toml打开runner
的配置文件看到之前配置的内容。
配置完成后可以在GitLab
中查看到对应配置的Rurnner
,如图:
点击
Runner
编号时(如:ee42d138
)可以查看到当前Runner
的详细信息,设置成如下显示:激活
-
Runners
暂停后将不能接受新jobs
被保护
- 这个
runner
只能运行被保护分支上触发的的流水线
运行没有标签的作业
- 表示此
Runner
可以运行没有任何标签的作业
锁定到当前项目
-
Runner
一旦被锁定,将不能被指派给其它的项目
五、启动GitLab-Runner
当所有不是执行后,在Runners settings
会显示runner
的状态,显示为绿色,则runner
配置成功。
cd ~
gitlab-runner install
gitlab-runner start
另外还有一些其他的GitLab-Runner
的相关命令,如:
gitlab-runner stop
gitlab-runner restart
六、iOS
项目文件配置
所有持续打包的项目里面需要加入.gitlab-ci.yml
,git push
后会自动识别.gitlab-ci.yml
文件中的配置,在CI/CD
-> Pipeline(流水线)
中就会显示一条记录。
.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
执行前需要执行的脚本设置
stages
: 定义当前 job
运行在那个阶段
tags
:通过 tags
确定使用指定还是使用通用部署程序。
script
: 需要在 执行的脚本
environment
: 定义由该作业执行部署的环境的名称
artifacts
: 定义作业工件列表
expire_in
:过期时间
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
#导出.ipa文件所在路径
exportIpaPath=IPADir/${development_mode}
#导出.ipa的相关证书配置
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
#导出ipa包
xcodebuild -exportArchive -archivePath ${build_path}/${project_name}.xcarchive \
-configuration ${development_mode} \
-exportPath ${exportIpaPath} \
-exportOptionsPlist ${exportOptionsPlistPath} \
-quiet || exit
if [ -e $exportIpaPath/*.ipa ]; then
#ipa包已导出
else
#ipa包导出失败
fi
#打包ipa完成
exit 0
CODE_SIGN_IDENTITY
为p12
文件的证书信息
PROVISIONING_PROFILE
profile
文件中的UUID
编号
PRODUCT_BUNDLE_IDENTIFIER
打包的Bundle Identifier
-destination
设置打包平台
导出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>