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

持续集成优点:
1、缩减开发周期,快速迭代版本
2、提供分发效率,测试人员以及相关人员无需等待开发人员打包
3、减少重复操作,高效打包,减轻开发人员工作量。

  1. 将代码推送到新的分支,CI 服务器就会为您运行测试。 如果job都是绿色 的,你的代码是OK 的。
  2. 如果给定分支中的所有测试都是绿色的,则可以让 CI 服务器自动将代码部署到生产环境中。 这就是所谓的连续部署(Continuous Deployment) 。
  3. 减少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
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:
sudo chmod +x /usr/local/bin/gitlab-runner

2. 注册gitlab runner

  • To register a Runner under macOS:
    *1. Run the following command:
   gitlab-runner register
  • 2Enter your GitLab instance URL: (你gitlab的地址)
    Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
    https://gitlab.com

* 3. Enter the token you obtained to register the Runner:(CI/CD中项目的token)
    Please enter the gitlab-ci token for this runner
    xxx
* 4. Enter a description for the Runner, you can change this later in GitLab’s UI:
    Please enter the gitlab-ci description for this runner
    [hostame] my-runner
    1. Enter the tags associated with the Runner, you can change this later in GitLab’s UI:
    Please enter the gitlab-ci tags for this runner (comma separated):
    my-tag,another-tag
  • 6 Enter the Runner executor:
     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 这个输出报告的时候用

brew install jq

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

//构建阶段 
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)打包到蒲公英
fastlane add_plugin pgyer
  • 自己的fastlane脚本

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.钉钉通知

#! 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
FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD=YOUR_PSD
  • 执行 fastlane spaceauth -u [email protected] 按提示获取session信息。
    * [email protected] 这个是 开发者账号
  • 复制session信息(很长一大段) 配置环境变量vim ~/.bash_profile
export FASTLANE_SESSION='YOUR SESSION'

你可能感兴趣的:(使用gitlab+fastlane进行自动化构建)