【工具】使用Fastlane实现iOS项目自动打包上传到App Store和第三方托管蒲公英平台

一、安装前准备

开发环境:


【工具】使用Fastlane实现iOS项目自动打包上传到App Store和第三方托管蒲公英平台_第1张图片

1.OS X 10.9 (Mavericks) 以上
2.Ruby 2.0 以上
3.Xcode 最新版
4.拥有一个付费的苹果开发者账号(我的账号是加入开发组,并且给我开了管理员权限,未付费)

二、配置环境

1、安装xcode
2、更新ruby版本,安装rvm

# 安装
curl -L get.rvm.io | bash -s stable  
  
# 测试是否安装正常
rvm -v

# 列出已知ruby版本         
rvm list known   

# 安装一个最新ruby版本 注:此处xxxxx为list中的最新版本号    
rvm install ruby-xxxxx     

# 如果报错的话
brew install openssl 

# 注意修改xxxxxx
reinstall|install ruby-xxxxx     

以上所需的ruby环境基本配置好了

3、打开终端,选择ruby 源
(rubygems、taobao这两个源不知道哪个能成功,所以都分别切换试一下,后文会提到)

# 查看gem源
gem sources

# 删除默认的gem源
gem sources --remove https://rubygems.org/

# 增加taobao作为gem源
gem sources -a https://ruby.taobao.org/

# 查看当前的gem源
gem sources
*** CURRENT SOURCES ***
http://ruby.taobao.org

# 清空源缓存
gem sources -c

# 更新源缓存
gem sources -u

三、安装Fastlane

1、安装xcode-select
xcode-select --install

# 如果 Xcode CLT 已经安装,则会报如下错误
# command line tools are already installed, use "Software Update" to install updates.
# 如果未安装,终端会开始安装 CLT

2.安装fastlane
sudo gem install fastlane --verbose

#  如果报错:ERROR: While executing gem ... (Errno::EPERM) Operation not permitted - /usr/bin/commander 
sudo gem install -n /usr/local/bin fastlane 

# 等待着安装完毕....coffee or tea

# 安装结束后,查看版本
fastlane --version

# 实际上目前安装的fastlane并不是最新版本,还需要更新,怎么更新呢,看下面

# cd到项目文件夹
cd xxxxx

# 初始化文件
fastlane init

Fastlane版本号


【工具】使用Fastlane实现iOS项目自动打包上传到App Store和第三方托管蒲公英平台_第2张图片

Fastlane在项目文件夹初始化后产生的文件


【工具】使用Fastlane实现iOS项目自动打包上传到App Store和第三方托管蒲公英平台_第3张图片
Snip20170608_8.png

四、配置文件修改

# Customise this file, documentation can be found here:
# https://github.com/fastlane/fastlane/tree/master/fastlane/docs
# All available actions: https://docs.fastlane.tools/actions
# can also be listed using the `fastlane actions` command

# Change the syntax highlighting to Ruby
# All lines starting with a # are ignored when running `fastlane`

# If you want to automatically update fastlane if a new version is available:
# update_fastlane

# This is the minimum version number required.
# Update this, if you use features of a newer version
fastlane_version "2.37.0"

default_platform :ios

platform :ios do
  before_all do
    # ENV["SLACK_URL"] = "https://hooks.slack.com/services/..."
    #cocoapods
    
  end

  desc "Runs all the tests"
  lane :test do
    scan
  end

  desc "Submit a new Beta Build to Apple TestFlight"
  desc "This will also make sure the profile is up to date"
  lane :beta do
    # match(type: "appstore") # more information: https://codesigning.guide
    gym(scheme: "FM") # Build your app - more options available
    pilot

    # sh "your_script.sh"
    # You can also use other beta testing services here (run `fastlane actions`)
  end

  desc "Deploy a new version to the App Store"
  lane :release do
    # match(type: "appstore")
    # snapshot
    gym(scheme: "FM") # Build your app - more options available
    deliver(force: true)
    # frameit
  end

#2.定义自定义lane 上传到App Store
  desc "自定义lane"
  lane :ldd_release do
  #2.1编译 选择scheme和功能
    # 增加build版本号
    increment_build_number
    gym(
      scheme: "FM",
      workspace: "com.xxxx.xxxxx",
      include_bitcode: false
      )

    deliver(
    #2.2上传appstore
      force: true,
      # skip_metadata: true,
      skip_screenshots: true,
      # skip uploading an ipa or pkg to iTunes  如果有最新包ipa包上传一定要 fasle
      skip_binary_upload: false,
      submit_for_review: true,
      automatic_release: true,
      price_tier: 0
      ) 
  end

# 上传到蒲公英
  lane :ldd_beta do
     gym(
        export_method: "ad-hoc",
        scheme: "FM"
        )
  pgyer(api_key: "accxxxxxxxxxb731b1c", user_key: "4f223xxxxxxxxxxxc089bb7e")
  end

  # You can define as many lanes as you want

  after_all do |lane|
    # This block is called, only if the executed lane was successful

    # slack(
    #   message: "Successfully deployed new App Update."
    # )
  end

  error do |lane, exception|
    # slack(
    #   message: exception.message,
    #   success: false
    # )
  end
end


# More information about multiple platforms in fastlane: https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Platforms.md
# All available actions: https://docs.fastlane.tools/actions

# fastlane reports which actions are used. No personal data is recorded. 
# Learn more at https://github.com/fastlane/fastlane#metrics


五、使用

切换到项目根目录下
1、上传到App Store
fastlane ldd_release

2、上传到蒲公英
bundle exec fastlane ldd_beta

你可能感兴趣的:(【工具】使用Fastlane实现iOS项目自动打包上传到App Store和第三方托管蒲公英平台)