iOS 自动化打包发布(Fastlane+ Jenkins+蒲公英)

  

  安装 Xcode 命令行工具:xcode-select --install

  安装 fastlane:sudo gem install fastlane --verbose

  安装成功后查看版本:fastlane --version

  配置 fastlane:

  终端进入工程主目录:

  输入:fastlane init

  

  出现:What would you like to use fastlane for? 选项时,选择 3,输入苹果开发者账号和密码。

  下面的步骤根据提示输入回车,完成后,fastlane 文件中会多出两个配置文件 Appfile 和 Fastfile

  

  Appfile 中:

  # app_identifier("[[APP_IDENTIFIER]]") # The bundle identifier of your app
  # apple_id("[[APPLE_ID]]") # Your Apple email address

  # For more information about the Appfile, see:
  # https://docs.fastlane.tools/advanced/#appfile

  替换对应的 bundle identifier 和 开发者账号。

  Fastfle 配置:

# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

# 定义fastlane版本号
fastlane_version “2.116.1” 

# 定义打包平台
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=“修改成你的工程scheme名称”

#蒲公英api_key和user_key
api_key=“修改成蒲公英管理后台中应用的 API Key”
user_key=“修改成蒲公英管理平台中应用的 User Key”


platform :ios do
  lane :development_build do|options|
branch = options[:branch]

puts “开始打development ipa”

updateProjectBuildNumber #更改项目build号

# 开始打包
gym(
#输出的ipa名称
output_name:”#{scheme}_#{get_build_number()}”,

# 是否清空以前的编译信息 true:是
clean:true,

# 指定打包方式,Release 或者 Debug
configuration:"Debug",

# 指定打包所使用的输出方式,目前支持app-store, package, ad-hoc, enterprise, development
export_method:"development",

# 指定输出文件夹
output_directory:"./fastlane/build",
)

puts "开始上传蒲公英"
# 开始上传蒲公英
pgyer(api_key: “#{api_key}”, user_key: “#{user_key}”)
  end
end

  

安装蒲公英插件:fastlane add_plugin pgyer  (⚠️进入到工程目录中执行安装蒲公英插件命令)

  

  选择 y

  

  输入电脑密码,安装成功,出现:Successfully installed plugins。

  开始打包:fastlane development_build

  

  进入蒲公英后台就可以查看到最新版本更新了。

  

  遇到的坑:

  1、Could not find action, lane or variable 'pgyer'. Check out the documentation for more details: https://docs.fastlane.tools/actions

  找不到蒲公英插件,原来是安装目录错了,应该在工程目录下安装:fastlane add_plugin pgyer 

  2、xcodebuild -showBuildSettings timed out after 4 retries with a base timeout of 3. You can override the base timeout value with the environment variable FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT, and the number of retries with the environment variable FASTLANE_XCODEBUILD_SETTINGS_RETRIES 

  重试打包命令或修改编译时长。

  3、[!] Exit status of command 'cd /*** && agvtool what-version -terse' was 6 instead of 0. (FastlaneCore::Interface::FastlaneShellError)

  Fastfile 中配置的 Build 自增,需要在 Xcode ->build setting -> version -> current 中配置如下:

  

原文地址:https://www.cnblogs.com/ZachRobin/p/10413263.html