![](https://img.haomeiwen.com/i301129/d77e10f6bc0b5640.png)
CI介绍
GitLab CI 是GitLab内置的进行持续集成的工具,只需要在仓库根目录下创建.gitlab-ci.yml 文件,并配置GitLab Runner;每次提交的时候,gitlab将自动识别到.gitlab-ci.yml文件,并且使用Gitlab Runner执行该脚本。
简而言之,拥有有效配置项所需的步骤可以总结为:
1、添加.gitlab-ci.yml到您的存储库的根目录
2、配置Runner
每次推送到Git存储库时,Runner都会自动启动管道,并且该管道将显示在项目的Pipelines页面下。
![](https://img.haomeiwen.com/i301129/0db3d4be6f641e01.png)
gitlab-ci配置
GitLab CI使用YAML文件(.gitlab-ci.yml)来管理项目配置。该文件存放于项目仓库的根目录,并且包含了你的项目如何被编译的描述语句。YAML文件使用一系列约束叙述定义了Job启动时所要做的事情。
.gitlab-ci.yml
文件
stages:
- build
- test
- deploy
job 1:
stage: build
script: xxxx
job 2:
stage: build
script: xxxx
job 3:
stage: test
script: xxxx
job 4:
stage: deploy
script: xxxx
stages是可以支持的步骤,job可以自定义命名,job下的stage是相同的说明这些job可以并发执行,如果是不同的就按stages规定的步骤依次执行。script是可以执行的shell命令。
给GitLab上的server安装runner
https://docs.gitlab.com/runner/install/osx.html
项目仓库配置runner接口
找到当前项目的Runner配置
![](https://img.haomeiwen.com/i301129/3c311808cc51ff04.png)
配置Runner需要的token,配置成功如下
![](https://img.haomeiwen.com/i301129/429a33e6d8c1299b.png)
Runner都配置完了,下面就可以对iOS写相应的CI脚本【script】了。
iOS CI场景
分支合并提醒
在dev-2.0拉出一个业务分支feature/2.0-room进行开发,这时候dev-2.0也许会修复一些bug,我在feature/2.0-room开发的分支应该及时合并dev-2.0的代码过来,feature/2.0-room打包的时候测试就会验证到这些问题。
.gitlab-ci.yml
before_script:
- echo 'this is before script'
- git version
- uname -a
- xcodebuild -version
- sw_vers
stages:
- check
- build
- test
- deploy
merge_check:
stage: check
tags:
- check
script:
- echo "Merge Check..."
- pwd
- python3 -u ci/merge_check.py $CI_COMMIT_REF_NAME
only:
- /^feature.*$/
only代表只有我当前的分支名符合/^feature.*$/这个正则的情况下才出发merge_check这个job。
tags代表我需要调度的Runner服务的哪一个tag。
主分支代码检查
before_script:
- echo 'this is before script'
- git version
- uname -a
- xcodebuild -version
- sw_vers
stages:
- check
- build
- test
- deploy
code_style_check:
stage: check
tags:
- check
script:
- echo "Code Style Check..."
- pwd
- git fetch
- echo "Source Branch " $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
- echo "Target Branch " $CI_MERGE_REQUEST_TARGET_BRANCH_NAME
- git diff origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME...origin/$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME -- '*.h' '*.m' -U0 | grep '^[+]' | grep -Ev '^(--- a/|\+\+\+ b/)' | sed 's/^+//' > ./tmp.diff || echo 'nothing to diff' > ./tmp.diff
- python3 code_style_check.py $CI_MERGE_REQUEST_TARGET_BRANCH_NAME $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
only:
- merge_requests
merge_requests只有feature的业务合并到dev的时候,才触发code_style_check这个job。
gif diff出dev和feature的不同写到tmp.diff文件里面,对tmp.diff进行代码检查舒服符合规范。
def rule_else(line):
if '}else' in line or 'else{' in line:
print("❌ else 两侧需要有空格: } else {", flush=True)
stop(line, 'spaces around else')
def rule_if(line):
if 'if(' in line:
print("❌ if( 中间需要空格", flush=True)
stop(line, 'spaces after if')
def rule_space(line):
if '){' in line:
print("❌ 需要空格 ){ -> ) {", flush=True)
stop(line, 'space between ){')
def rule_string_define_copy(line):
if 'NSString' in line and '@property' in line and 'copy' not in line and '<' not in line:
print("❌ NSString 需要用 copy 修饰", flush=True)
stop(line, 'NSString need copy')
def rule_comma(line):
if ',' in line and ',\n' not in line and ', ' not in line and ',"' not in line:
print("❌ 逗号右侧要有空格", flush=True)
stop(line, 'space after comma')
代码检查的部分脚本如上,对tmp.diff每一行进行扫描,检查是否符合iOS开发规范。
网友评论