GitLab使用及iOS持集成

一、目的

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,左侧会显示与当前项目相关的参数。

image-20211230172028742.png

4.2 客户端注册runner

打开MAC应用Terminal,输入注册命令,如下:

gitlab-runner register

指定gitURL(输入图一里面URL地址,私有git的路径)

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):

指定gitlab-runnertoken(输入图一里面token,项目的token,用于关联runner和项目)

Please enter the gitlab-ci token for this runner:

tag的描述(输入一个描述,runner的名字,用于区分runner

Please enter the gitlab-ci description for this runner:

关联gitrunnertag(输入这个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,如图:

image-20211230173411671.png

点击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.ymlgit 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_IDENTITYp12文件的证书信息
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>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容