(Mac)iOS自动化打包(Jenkins+fastlane+pgy

环境

| 名称 | 版本 |
| — | — | — |
| macOS | 10.14.4 (18E226) |
| Xcode | Version 10.2.1 |
| ruby | 2.6.3 |
| fastlane | 2.123.0 |

Fastlane安装

Fastlane是一套使用Ruby写的自动化工具集,用于iOS和Android的自动化打包、发布等工作,可以节省大量的时间。
安装过程如下

安装ruby(brew安装最新版)

1
2
3
4
5
6
7
8
9
10
11
12
13
#直接在终端执行,未安装brew,先安装brew

#安装最新的2.6.3
brew install ruby

#安装完根据提示执行
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrc

#退出终端查看ruby版本
ruby -v

#检查安装路径
which -a ruby

更改gem源

  • 更改
    1
    2
    gem sources --remove https://rubygems.org/
    gem sources --add https://gems.ruby-china.com/
  • 查看
    1
    gem sources -l
    如果是以下结果说明正确,如果有其他的请自行百度解决
    1
    2
    *** CURRENT SOURCES ***
    https://gems.ruby-china.com/
  • 安装fastlane
    1
    2
    3
    4
    5
    #安装
    gem install -n /usr/local/bin fastlane -NV

    #检查是否安装成
    fastlane --version

至此,我们已经可以使用fastlane自动化打包了

Fastlane配置

fastlane init

cd 到工程主目录执行fastlane init 我这里选择的是手动配置

1
2
cd /Users/vic/.jenkins/workspace/jike-iOS-Fastlan
fastlane init

会在你项目工程的目录下生成一个fastlane文件夹,里面有Fastlane的配置文件,一个是Appfile文件(保存了苹果开发者的相关信息),一个是Fastfile文件(运行脚本)

编辑Fastfile文件(运行脚本)

有时候一天需要打好几个包,为了区分,我们这里实现一个递增build号的功能。

  • 定义一个递增build号的函数,添加到Fastfile中
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    def updateProjectBuildNumber

    currentTime = Time.new.strftime("%Y%m%d")
    build = get_build_number()
    if build.include?"#{currentTime}."
    # => 为当天版本 计算迭代版本号
    lastStr = build[build.length-2..build.length-1]
    lastNum = lastStr.to_i
    lastNum = lastNum + 1
    lastStr = lastNum.to_s
    if lastNum < 10
    lastStr = lastStr.insert(0,"0")
    end
    build = "#{currentTime}.#{lastStr}"
    else
    # => 非当天版本 build 号重置
    build = "#{currentTime}.01"
    end
    puts("*************| 更新build #{build} |*************")
    # => 更改项目 build
    increment_build_number(
    build_number: "#{build}"
    )
    end
  • 实现自动打包的完整Fastfile如下:
    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
    # 定义fastlane版本号
    fastlane_version "2.123.0"

    # 定义打包平台
    default_platform :ios

    def updateProjectBuildNumber

    currentTime = Time.new.strftime("%Y%m%d")
    build = get_build_number()
    if build.include?"#{currentTime}."
    # => 为当天版本 计算迭代版本号
    lastStr = build[build.length-2..build.length-1]
    lastNum = lastStr.to_i
    lastNum = lastNum + 1
    lastStr = lastNum.to_s
    if lastNum < 10
    lastStr = lastStr.insert(0,"0")
    end
    build = "#{currentTime}.#{lastStr}"
    else
    # => 非当天版本 build 号重置
    build = "#{currentTime}.01"
    end
    puts("*************| 更新build #{build} |*************")
    # => 更改项目 build 号
    increment_build_number(
    build_number: "#{build}"
    )
    end

    #指定项目的scheme名称
    scheme="Ruguo"
    #蒲公英api_key和user_key
    api_key="8e860ee5ba4996f9b19e56c28e07846a"
    user_key="542e73b113b5908da8b028805bf7e837"

    # 任务脚本
    platform :ios do
    lane :development_build do|options|
    branch = options[:branch]

    puts "开始打development ipa"

    updateProjectBuildNumber #更改项目build号

    # 开始打包
    gym(
    #指定需要编译的scheme
    scheme:"#{scheme}",
    #输出的ipa名称
    output_name:"#{scheme}_#{get_build_number()}",
    # 是否清空以前的编译信息 true:是
    clean:true,
    # 指定打包方式,Release 或者 Debug
    configuration:"Release",
    # 指定打包所使用的输出方式,目前支持app-store, package, ad-hoc, enterprise, development
    export_method:"development",
    # 指定输出文件夹
    output_directory:"./fastlane/build",
    )

    #puts "开始上传蒲公英"
    # 开始上传蒲公英
    #pgyer(api_key: "#{api_key}", user_key: "#{user_key}", password: "alpha", install_type: "2")

    end
    end

    注意:蒲公英的 api_key 和 user_key,开发者在自己账号下的 账号设置-API信息 中可以找到。打其它类型的包的方法与development类似,可自定义一个新的lane实现。

自动打包并上传蒲公英,

在终端输入便会进行自动打包并上传蒲公英了。

1
fastlane development_build

Jenkins安装

下载安装Jenkins。具体安装过程可参考 Mac Jenkins搭建

Jenkins配置

  • 点击新建,输入名称,构建一个自由风格的软件项目

  • 添加Git仓库地址,可以是HTTP也可以是SSH。点击Add

  • 构建,点击“添加构建步骤”,选择Execute shell。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    export PATH=/usr/local/bin:$PATH
    fastlane development_build

    filepath=`pwd`fastlane/build/Ruguo*.api
    file=`ls $filepath`
    if [ $uploadPgy = true ]
    then
    curl -F "installType=2" -F "password=alpha" -F "file=@$file" -F "uKey=542e73b113b5908da8b028805bf7e837" -F "_api_key=8e860ee5ba4996f9b19e56c28e07846a" https://qiniu-storage.pgyer.com/apiv1/app/upload
    fi
  • 添加完成之后保存,点击立即构建