1

🐻 iOS自动化方案附脚本

 2 years ago
source link: https://juejin.cn/post/6948239939809050638
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

🐻 iOS自动化方案附脚本

2021年04月07日 02:41 ·  阅读 1980

不同的电脑开发环境不同,多人协作下,因环境不同会导致各种问题,比如cocoapods的版本不同,就会导致某些库无法下载,.lock文件频繁更新等。本文介绍了如何统一开发环境,以及自动化脚本的使用

统一开发环境

统一开发环境,减少因环境不同导致文件冲突。

具体实现:安装rbenv,管理ruby环境;安装Bundler,管理cocoapods、cocoapods插件、fastlane等。

环境搭建者需要配置以下内容:

安装 Homebrew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
复制代码

安装 rbenv

brew install rbenv ruby-build rbenv-vars
复制代码

放到你的 Shell 配置文件里面

export PATH="$HOME/.rbenv/bin:$PATH" 
复制代码
eval "$(rbenv init -)"
复制代码

安装和设置项目的 Ruby 环境

rbenv install 2.7.1
复制代码
rbenv local 2.7.1
复制代码

rbenv 会帮我们建立 一个叫作.ruby-version 的文件

其他人员执行以下脚本:

# 在rbenv 下安装特定版本的 Ruby 开发环境

ruby_version=`cat .ruby-version`
if [[ ! -d "$HOME/.rbenv/versions/$ruby_version" ]]; then
  rbenv install $ruby_version;
fi

# 通过 RubyGems 安装 Bunlder

gem install bundler

# 使用 Bundler 安装 CocoaPods 和 fastlane 等依赖包

bundle install

# 安装各个 Pod

bundle exec pod install

复制代码

至此统一的环境搭建结束,对于xcode,只要不是差别很大就行,保持在一个大版本中就行,如:XCode12

命名该脚本为:setup.sh

配置fastlane

Gemfile 中添加:

# frozen_string_literal: true
source "https://rubygems.org"

gem 'cocoapods', '1.10.1'

# 自动部署
gem 'fastlane', '2.179.0'

plugins_path = File.join(File.dirname(__FILE__), 'fastlane', 'Pluginfile')

eval_gemfile(plugins_path) if File.exist?(plugins_path)

复制代码

再次执行上述脚本 setup.sh

实用lane

执行,创建自定义Fastlane文件

bundle exec fastlane init
复制代码

fastlane 执行时,比较困难的是证书配置。

使用mach来管理证书

首先,在github或者其他git平台,新建一个私有仓库。

  desc "拉取证书" 
  lane :mach_pp do
    # 证书管理
    match(
        git_url: "https://github.com/xxx/xxx.git",
        type: "adhoc" ,
        app_identifier:["com.xxx.xxx","com.xxx.xxx.push"],
        username:"[email protected]",
        team_id: "xxx"
        )
    match(
        git_url: "https://github.com/xxx/xxx.git",
        type: "appstore" ,#can be appstore,adhoc, development,enterprise
        app_identifier:["com.xxx.xxx","com.xxx.xxx.push"],
        username:"[email protected]",
        team_id: "xxx"
    )
  end 
复制代码

填入以上信息:git_url、type、app_identifier、username、team_id

其中:type分为adhoc、appstore

gym 打包

 desc "发正式包到appstore"
  lane :ipa2apple do
    # gym用来编译ipa
    gym(
        scheme: 'XXX',
        configuration: "Release",
        export_method: "app-store", # 指定打包方式
        xcargs: "-allowProvisioningUpdates",
        archive_path: '../appstore',
        output_directory: './appstore',
        output_name: 'XXX.ipa'
    )
  end

复制代码

Pluginfile 管理插件

安装fir插件

fastlane add_plugin fir_cli
复制代码
firim(firim_api_token: "56d42c7266586d43dccad86730555c78")  # token 在fir 上查看。
复制代码

上传符号表

安装firebase插件

fastlane add_plugin firebase_app_distribution

复制代码
  # fir 的 release 包 Crash report symbols
  desc 'Upload symbols to Crashlytics for Release app'
  lane :upload_symbols_to_crashlytics_release do
    upload_symbols_to_crashlytics(
      dsym_path: "./fir_release/xxx.app.dSYM.zip",
      gsp_path: "./xxx/GoogleService-Info_Beta.plist",
      api_token: ENV["FIREBASE_API_TOKEN"]
    )
  end
复制代码
desc "准备工作 install"
  lane :ready do

    # 拉代码
    git_pull

    # 全局变量
    time = Time.new

    # build 号
    build_number = time.strftime("%Y%m%d%H%M%S")
    increment_build_number({
      build_number: build_number
    })

    git_commit(path:".", message:"[robot][auto build to #{build_number}]")
    sh 'git push origin main'    
  end
复制代码
  • 使用undler工具管理后,以后执行pod 需要在前边加上 bundle exec

  • sync_code_signing命令,其实是match命令的另外一种写法

  • build_app命令其实是gym的别名

  • fastlane 常用的action以及参数 juejin.cn/post/684490…

  • zsh: permission denied问题的解决办法 sudo chmod u+x *.sh

  • sudo操作,如果遇到不明原因的错误,可能是权限问题,使用chmod 777 路径 尝试解决

脚手架工具设想:

#### 1. git操作(解决git命令难记的痛点)

- 提交到本地仓库
- 提交到远端仓库
- 撤销本地仓库的提交
- 撤销远端仓库提交
- 创建分支
- 切换分支
- 合并分支
- 暂存、贮藏
- 下载

#### 2. 基础库
- SpeedySwift

#### 3. pod操作
- 初始化
- 安装
- 更新
- 组件创建
- 组件发布
- 组件二进制

#### 4. 自动化构建
- fastlan dev_ipa
- fastlan internal_ipa
- fastlan appstore_ipa
- 符号表上传
- 符号表解析

#### 5. 业务模板
- MVVM
- MVC

#### 6. 环境配置
复制代码

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK