使用gitlab+fastlane进行自动化构建

持续集成优点:
1、缩减开发周期,快速迭代版本

2、提供分发效率,测试人员以及相关人员无需等待开发人员打包

3、减少重复操作,高效打包,减轻开发人员工作量。

4 . 将代码推送到新的分支,CI 服务器就会为您运行测试。 如果 job 都是绿色 的,你的代码是 OK 的。

5 . 如果给定分支中的所有测试都是绿色的,则可以让 CI 服务器自动将代码部署到生产环境中。 这就是所谓的连续部署(Continuous Deployment) 。

6 . 减少 code review 时间

搭建方法

运行环境要求

  • gitlab
  • gitlabrunner
  • fastlane
  • 钉钉机器人

搭建步骤

1. 安装 gitlab runner

  • Install GitLab Runner on macOS(我的是 mac 环境所以选择的 Install GitLab Runner on macOS)
    *1.Download the binary for your system
1
sudo curl --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-darwin-amd64
  • 2.Give it permissions to execute:
1
sudo chmod +x /usr/local/bin/gitlab-runner

2. 注册 gitlab runner

  • To register a Runner under macOS:
    *1. Run the following command:
1
gitlab-runner register
  • 2Enter your GitLab instance URL: (你 gitlab 的地址)
1
2
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
https://gitlab.com
    1. Enter the token you obtained to register the Runner:(CI/CD 中项目的 token)
1
2
Please enter the gitlab-ci token for this runner
xxx
    1. Enter a description for the Runner, you can change this later in GitLab’s UI:
1
2
Please enter the gitlab-ci description for this runner
[hostame] my-runner
1
2
Please enter the gitlab-ci tags for this runner (comma separated):
my-tag,another-tag
1
2
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
shel

3. 编写 yml 文件

配置项目的 yml 文件进行相应的 job 操作
校验 yml 文件是否正确
本地调试 yml 文件
安装 jq 这个输出报告的时候用

1
brew install jq

下面是我的 yml 文件仅供参考

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//构建阶段
stages:
- build
- buildAndUpload
- failure

//构建前配置
before_script:
- export LANG=en_US.UTF-8
- export FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT=120


# build for branch test build and upload to pgyer.
job1:
//注册 runner tag名 可以多个
tags:
- wangweitags
stage: buildAndUpload
script:
# - sh ./Shell/dingtalk.sh "新版本" "# 构建新版本(justademo)"
- echo $(pwd)
- echo "build job1"
- pod install
- git reset --hard
// fastlane 自动化构建
- fastlane dev
// 输出相关的git commit等相关信息
- export pgyerDes=`git log -10 --oneline | cut -d " " -f2- | grep -E '^\[.*|.*finish$'`
- echo ${pgyerDes}
- echo $(pwd)
- echo $(ls)
- export result=$(curl -# -F "file=@../product/LZMDYW.ipa" -F "_api_key=2c34b4faa9e0559667c828f415f7ebee" -F "userKey=1ce44e894fbe779bbf23e75d12c482d8" -F "buildUpdateDescription=${pgyerDes}" https://www.pgyer.com/apiv2/app/upload)
- echo ${result} | jq
- export buildKey=`echo ${result} | jq '. | .data.buildKey' | awk -F '"' '{print $2}'`
buildQRCodeURL=`echo ${result} | jq '. | .data.buildQRCodeURL' | awk -F '"' '{print $2}'`
buildVersion=`echo ${result} | jq '. | .data.buildVersion+"("+.data.buildBuildVersion+")"' | awk -F '"' '{print $2}'`
buildUpdated=`echo ${result} | jq '. | .data.buildUpdated' | awk -F '"' '{print $2}'`
change_log=`git log -20 --oneline | cut -d " " -f2- | grep -E '^\[.*|.*finish$' | awk '{{printf"##### %s; \n",$0}}'`
- export text="### 版本"${buildVersion}" \n > 更新记录 \n\n > "${change_log}" \n\n > ![screenshot]("${buildQRCodeURL}")\n##### [下载链接](https://www.pgyer.com/"${buildKey}")\n\n###### 上传日期:"${buildUpdated}""
//钉钉脚本
- sh ./Shell/dingtalk.sh "新版本" "$text"
only:
- /^test.*$/


# build for branch only build.
job2:
tags:
- wangweitags
stage: build
script:
# - sh ./Shell/dingtalk.sh "新版本" "# 构建新版本(justademo)"
- echo $(pwd)
- echo "build job2"
- pod install
- git reset --hard
//fastlen 构建
- fastlane CI_dev
only:
- dev
- master


# notify when a build has errored
build_failure_job:
tags:
- wangweitags
stage: failure
script:
// 丁丁脚本
- sh ../Shell/dingtalk.sh "新版本" "# 构建失败(LZMDYW)"
when: on_failure

4. fastlane 构建

https://docs.fastlane.tools/getting-started/ios/setup/

(1)安装 xcode 命令行工具
  • xcode-select –install,如果没有安装,会弹出对话框,点击安装。如果提示 xcode-select: error: command line tools are already installed, use “Software Update” to install updates 表示已经安装

#####(2) 安装 Fastlane
sudo gem install fastlane -NV 或是 brew cask install fastlane 我这里使用 gem 安装的
安装完了执行 fastlane –version,确认下是否安装完成和当前使用的版本号。

(3)初始化 Fastlane

cd 到你的项目目录执行
fastlane init

(4)打包到蒲公英
1
fastlane add_plugin pgyer
  • 自己的 fastlane 脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

default_platform(:ios)

platform :ios do
desc "Description of what the lane does"

lane :dev do

sigh(force: true,
username: "开发者账号",
app_identifier: "bundleid",
output_path: '.ipa包输出路径',
adhoc: true (是否adhoc)
)

build_app(
workspace: "XXXX.xcworkspace",
configuration: "Debug", (scheme配置)
scheme: "XXXX",
export_method: "ad-hoc",
output_directory:".ipa包输出路径",
include_bitcode:true,
)
// 打包到蒲公英
pgyer(
api_key: "",
user_key: "",
update_description: "scheme is dev, update by fastlane"
)

end

lane :CI_dev do

sigh(force: true,
username: "开发者账号",
app_identifier: "bundleid",
output_path: '.ipa包输出路径',
adhoc: true (是否adhoc)
)

build_app(
workspace: "XXXX.xcworkspace",
configuration: "Debug", (scheme配置)
scheme: "XXXX",
export_method: "ad-hoc",
output_directory:".ipa包输出路径",
include_bitcode:true,
)
end
end

5.钉钉通知

1
2
3
4
5
6
7
8
9
10
11
12
13
#! bin/bash
echo $2


curl ' 放自己注册的钉钉机器人的token' \
-H "Content-Type:application/json" \
-d "{ \
markdown:{ \
title:'$1', \
text:'$2' \
}, \
msgtype:'markdown' \
}"

6 fastlane 构建注意事项(三天两头的加测试机,手动更新配置文件更新实在是太麻烦了,要不老是提示双重认证)

refer:https://blog.csdn.net/weixin_34293911/article/details/87011860

  • fastlane iOS 双重认证 更新配置文件
    这个时候就用到这个,他可以自动管理配置开发者等信息
    https://github.com/fastlane/fastlane/tree/master/spaceship
  • 访问 AppleId 管理站
  • 找到 安全 - App 专用密码,生成一个专用密码
  • 配置环境变量 vim ~/.bash_profile
  • export
1
FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD=YOUR_PSD
  • 执行 fastlane spaceauth -u abcd@qq.com 按提示获取 session 信息。
  • 复制 session 信息(很长一大段) 配置环境变量 vim ~/.bash_profile
1
export FASTLANE_SESSION='YOUR SESSION'