26.fastlane 自动打包上传

2021-11-15  本文已影响0人  MrCoderLin

fastlane安装

fastlane文档
安装最新版Xcode命令行工具,打开终端输入一下命名

xcode-select --install

如果已经安装过了,会提示
xcode-select: error: command line tools are already installed, 
use "Software Update" to install updates

使用 gem 直接安装 fastlane, 执行下面命令

sudo gem install fastlane

安装完成之后,进入到 项目目录中,并执行下面命令

// 安装后自动生成./Gemfile文件 ./fastlane目录 => Appfile Fastfile
fastlane init

//下面是 swift 的 可使用swift编写配置文件
fastlane init swift

fastlane插件Plugins配置

根据自己需要添加FTP、蒲公英、firim等等插件,进入到项目目录,执行命令

# https://docs.fastlane.tools/plugins/available-plugins/
// 蒲公英配置
fastlane add_plugin pgyer

// firim配置
//先通过 gem 安装 fir环境
sudo gem install fir-cli 
// 添加firim
fastlane add_plugin firim

ftp配置
fastlane add_plugin ftp

fastlane 配置打包上传

上面安装并初始化 fastlane 之后,会生成一个 fastlane文件夹,里面有两个文件需要配置 Appfile 和 Fastfile,下面配置他们两个

Appfile

Appfile 主要是配置项目的 账号名字 和 bundleID

app_identifier("APP_IDENTIFIER") # The bundle identifier of your app
apple_id("APPLE_ID") # Your Apple email address
itc_team_id("xx96xx6xx") # App Store Connect Team ID
team_id("xxYNxxK") # Developer Portal Team ID

=begin
如果本地 xcode 已经登录 apple账号 配置好了相关证书,那么直接使用本地环境下的账号信息即可
app_identifier   ENV['App_Identifier']
apple_id         ENV['Apple_Id']
itc_team_id      ENV['Itc_Team_Id']
team_id          ENV['Team_Id']
=end

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

fastfile 是项目配置的关键,打包发布的逻辑都在这里

打包企业版ipa并上传至FTP服务器,Fastfile如下所示
# 企业版打包 正式版: bundle exec fastlane ios release
# 默认使用/Applications/Xcode.app打包,get_build_number需要CURRENT_PROJECT_VERSION有值且与Info.plist build版本号一致

default_platform(:ios)

platform :ios do

  # 获取当前项目相关信息
  currentTime = Time.new.strftime("%Y-%m-%d %H-%M-%S")
  version_number = get_version_number # App版本号
  build_number = get_build_number # (xcodeproj: ENV['xcodeproj']) 具体项目 build版本号
  version_build = "#{version_number}#{'_'}#{build_number}"
  # puts version_build
  ipa_directory = "#{'/Users/xxx/Documents/IPA/Project  '}#{currentTime}"

  desc "正式版(enterprise)打包并上传FTP服务器"
  lane :release do | options |
    gym(workspace: ENV['Workspace'], # 编译项目文件,为目录环境下的workspace
    scheme: ENV['Scheme'], # scheme为环境下的
    clean:true, # 构建前先清理项目缓存
    silent: true, # 静默构建
    configuration: "Release",
    export_method: "enterprise",
    output_name:"#{'ios-phone-xxx-'}#{version_build}#{ENV['ipa']}",
    output_directory: ipa_directory) # Build your app - more options available
    # deliver(force: false)

    # 上传ipa文件至服务器
    ipa_name = "#{'ios-phone-xxx-'}#{version_build}#{'.ipa'}"
    ipa_src = "#{ipa_directory}#{'/'}#{ipa_name}"
    ftp(
      host: 'ftp.domain.com',
      username: 'your username',
      password: 'your password',
      upload: {
        src: ipa_src,
        dest:"/xxx/" 
      }
    )
  end

#  desc "上线 App Store"
#  lane :releaseAppStore do
#    increment_build_number(xcodeproj: "Project.xcodeproj")
# build_app(
            workspace: ENV['Workspace'], # 编译项目文件,为目录环境下的workspace
            scheme: ENV['Scheme'], # scheme为环境下的的scheme
            clean: true, # 构建前先清理项目缓存
            silent: true, # 静默构建
            output_directory: './fastlane/appstore/', # 设置导出ipa目录
            output_name:"#{currentTime}#{ENV['ipa']}") # 包的名称
#    build_app(workspace: "Project.xcworkspace", scheme: "Project")
#    upload_to_app_store(skip_metadata: true, skip_screenshots: true)
#  end

#  desc "Push a new beta build to TestFlight"
#  lane :beta do
#    build_app(workspace: "Project.xcworkspace", scheme: "Project")
#    upload_to_testflight
#  end

end

存在多个Xcode时,如果想使用旧版Xcode打包并上传FTP服务器,Fastfile配置如下所示

# Xcode项目配置自动签名,Info文件路径:./项目名/Info.plist(CFBundleVersion=整型数)
# 企业版旧版Xcode打包 内测版: bundle exec fastlane ios alpha_release

default_platform(:ios)
platform :ios do
    
  # 获取当前项目相关信息
  currentTime = Time.new.strftime("%Y-%m-%d %H-%M-%S")
  xcode_select("/Applications/Xcode 10.1.app") # 选择旧版Xcode10.1打包
  version_number = get_version_number(xcodeproj: ENV['xcodeproj']) # App版本号
  #build_number = get_build_number(xcodeproj: ENV['xcodeproj']) # 具体项目 build版本号
  build_number = get_info_plist_value(path: "./Project/Info.plist", key: "CFBundleVersion")
  version_build = "#{version_number}#{'_'}#{build_number}"
  # puts version_build
  ipa_directory = "#{'/Users/xxx/Documents/IPA/Project '}#{currentTime}" # ipa导出目录
  
  # ipa名称前半部分ios-phone-
  def getIpaName(var="sys")
    ipa_name = "#{'ios-phone-'}#{var}#{'-'}"
    # puts "#{'ipa名称:'}#{ipa_name}"
    return ipa_name
  end

  desc "内测版(enterprise)打包并上传FTP服务器"
  lane :alpha_release do | options |
    # xcode_select("/Applications/Xcode 10.1.app")
    ipa_name_prefix = getIpaName("alpha") 
    gym(build_path: "/Applications/Xcode 10.1.app",
    workspace: ENV['Workspace'], # 编译项目文件,为目录环境下的workspace
    scheme: ENV['Scheme'], 
    clean:true, # 构建前先清理项目缓存
    silent: true, # 静默构建
    configuration: "Release",
    export_method: "enterprise",
    export_options: {
      provisioningProfiles: { 
        "bundle_identifier" => "xxxDistributionProfile"
      },
      "signingStyle": "manual"
    },
    output_name:"#{ipa_name_prefix}#{version_build}#{ENV['ipa']}",
    output_directory: ipa_directory) # Build your app - more options available
    # deliver(force: false)

    # 上传ipa文件至服务器
    ipa_name = "#{ipa_name_prefix}#{version_build}#{'.ipa'}"
    ipa_src = "#{ipa_directory}#{'/'}#{ipa_name}"
    ftp(
      host: 'ftp.domain.com',
      username: 'your username',
      password: 'your password',
      upload: {
        src: ipa_src,
        dest:"/alpha/" 
      }
    )
  end 

end

上传蒲公英Fastfile

desc "上线蒲公英"
lane :adhoc_pgy do |options|
    # 编译版本号自增设置,项目的版本号需要自己设定的和appstore一样,自己项目中设置好即可
    increment_build_number(xcodeproj:ENV['Xcodeproj'])
    # 获取时间,用于设置包目录或者包名
    currentTime = Time.new.strftime("%Y-%m-%d-%H-%M-")
    logDirectory = "#{currentTime}"

    build_app(
        workspace: ENV['Workspace'],
        scheme: ENV['Scheme'],
        silent: true,
        clean: true,
        output_directory: './fastlane/pgy/',
        output_name:"#{currentTime}#{ENV['ipa']}",# 设置导出ipa目录
        export_method:"ad-hoc") # 导出方式为 ad-hoc

    # 设置蒲公英的 api_key 和 user_key
    # pgyer(api_key:'xxb3dd2ab6a5efc2b7d861356854xx',
    # user_key:'xxx753c46ae83961ed9478bdae86ebxx')
        
    # 使用本地环境下的蒲公英 key 信息,运行后首次需要自己输入账号密码
    # pgyer(api_key: ENV['api_key'],
    # user_key: ENV['user_Key'])
end

运行 fastlane

进入到项目目录,运行下面命令

// 自行选择lane执行
bundle exec fastlane

// 执行指定lane 
bundle exec fastlane ios alpha_release
上一篇下一篇

猜你喜欢

热点阅读