使用gitlab+fastlane進(jìn)行自動(dòng)化構(gòu)建

持續(xù)集成優(yōu)點(diǎn):
1、縮減開發(fā)周期,快速迭代版本
2、提供分發(fā)效率,測(cè)試人員以及相關(guān)人員無需等待開發(fā)人員打包
3、減少重復(fù)操作,高效打包,減輕開發(fā)人員工作量。

  1. 將代碼推送到新的分支,CI 服務(wù)器就會(huì)為您運(yùn)行測(cè)試。 如果job都是綠色 的,你的代碼是OK 的。
  2. 如果給定分支中的所有測(cè)試都是綠色的,則可以讓 CI 服務(wù)器自動(dòng)將代碼部署到生產(chǎn)環(huán)境中。 這就是所謂的連續(xù)部署(Continuous Deployment) 。
  3. 減少code review 時(shí)間

搭建方法

運(yùn)行環(huán)境要求

  • gitlab
  • gitlabrunner
  • fastlane
  • 釘釘機(jī)器人

搭建步驟

1. 安裝gitlab runner

  • Install GitLab Runner on macOS(我的是mac環(huán)境所以選擇的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. 注冊(cè)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中項(xiàng)目的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
    Please enter the gitlab-ci tags for this runner (comma separated):
    my-tag,another-tag
     Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
     shel

3. 編寫 yml 文件

配置項(xiàng)目的yml文件進(jìn)行相應(yīng)的job操作
校驗(yàn)yml文件是否正確
本地調(diào)試yml文件
安裝jq 這個(gè)輸出報(bào)告的時(shí)候用

brew install jq

下面是我的yml 文件僅供參考

//構(gòu)建階段 
stages:
- build
- buildAndUpload
- failure

//構(gòu)建前配置
before_script:
    - export LANG=en_US.UTF-8
    - export FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT=120


# build for branch test   build and upload to pgyer.
job1:
//注冊(cè) runner tag名 可以多個(gè)
 tags:
    - wangweitags
 stage: buildAndUpload
 script:
    # - sh ./Shell/dingtalk.sh "新版本" "# 構(gòu)建新版本(justademo)"
    - echo $(pwd)
    - echo "build job1"
    - pod install
    - git reset --hard
   // fastlane 自動(dòng)化構(gòu)建
    - fastlane dev
// 輸出相關(guān)的git commit等相關(guān)信息
    - 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 "新版本" "# 構(gòu)建新版本(justademo)"
    - echo $(pwd)
    - echo "build job2"
    - pod install
    - git reset --hard
//fastlen 構(gòu)建
    - fastlane CI_dev
 only:
    - dev
    - master


# notify when a build has errored 
build_failure_job:
  tags:
    - wangweitags
  stage: failure
  script:
// 丁丁腳本
    - sh ../Shell/dingtalk.sh "新版本" "# 構(gòu)建失敗(LZMDYW)"
  when: on_failure

4. fastlane 構(gòu)建

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

(1)安裝xcode命令行工具
  • xcode-select --install,如果沒有安裝,會(huì)彈出對(duì)話框,點(diǎn)擊安裝。如果提示xcode-select: error: command line tools are already installed, use "Software Update" to install updates表示已經(jīng)安裝
(2) 安裝Fastlane

sudo gem install fastlane -NV或是brew cask install fastlane我這里使用gem安裝的
安裝完了執(zhí)行fastlane --version,確認(rèn)下是否安裝完成和當(dāng)前使用的版本號(hào)。

(3)初始化Fastlane

cd到你的項(xiàng)目目錄執(zhí)行
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: "開發(fā)者賬號(hào)",
        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: "開發(fā)者賬號(hào)",
        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 ' 放自己注冊(cè)的釘釘機(jī)器人的token' \
-H "Content-Type:application/json"  \
-d "{ \
markdown:{ \
title:'$1', \
text:'$2' \
},  \
msgtype:'markdown' \
}"

6 fastlane 構(gòu)建注意事項(xiàng)(三天兩頭的加測(cè)試機(jī),手動(dòng)更新配置文件更新實(shí)在是太麻煩了,要不老是提示雙重認(rèn)證)

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

  • fastlane iOS 雙重認(rèn)證 更新配置文件
    這個(gè)時(shí)候就用到這個(gè),他可以自動(dòng)管理配置開發(fā)者等信息
    https://github.com/fastlane/fastlane/tree/master/spaceship
    • 訪問 AppleId 管理站
    • 找到 安全 - App 專用密碼,生成一個(gè)專用密碼
    • 配置環(huán)境變量 vim ~/.bash_profile
    • export
FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD=YOUR_PSD
  • 執(zhí)行 fastlane spaceauth -u abcd@qq.com 按提示獲取session信息。
    * abcd@qq.com 這個(gè)是 開發(fā)者賬號(hào)
  • 復(fù)制session信息(很長(zhǎng)一大段) 配置環(huán)境變量vim ~/.bash_profile
export FASTLANE_SESSION='YOUR SESSION'
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容