iOS使用fastlane自动化打包

最近好多微信文章都推荐fastlane,自己也总结一下我使用fastlane的过程吧。
其实自动化打包的工具有很多,比较流行的有Jenkinsfastlane,原来使用Jenkins,感觉这个工具比较麻烦,需要配置的东西非常多,还需要仓库地址等等很多信息,然而fastlane是比较简单快速的,(ios,Android都支持),github地址:
https://github.com/fastlane/fastlane 文档地址:https://docs.fastlane.tools/
github上有 24014多星星。3659个fork,所以大量的开发者信任并一起维护他。

安装前的准备工作

1、首先确认是否安装了ruby,终端查看下ruby版本
使用命令ruby -v

《iOS使用fastlane自动化打包》 ruby查看

2、确认是否安装了Xcode命令行工具

使用命令
xcode-select --install

《iOS使用fastlane自动化打包》 xcode工具检查

如果是图中的情况是安装成功

如果出现:

《iOS使用fastlane自动化打包》 开发工具

就点击安装就行了。。。

开始正式安装fastlane

1、安装fastlane(两种方式都可以)

# Using RubyGems
sudo gem install fastlane -NV
或者
# Alternatively using Homebrew
brew cask install fastlane

个人操作

执行命令sudo gem install fastlane -NV

《iOS使用fastlane自动化打包》 执行报错

错误原因:应该是
https://gems.ruby-china.org/ 没有找到包,我们可以尝试访问这个地址:
https://gems.ruby-china.org/

出现这个界面

《iOS使用fastlane自动化打包》 源地址报错

也就是我们需要更换源了

1、查看源:
gem sources

2、删除源:
gem sources --remove https://gems.ruby-china.org/

3、更换源:
gem sources -a https://gems.ruby-china.com

具体步骤:

《iOS使用fastlane自动化打包》 更换源

继续安装,执行命令sudo gem install fastlane -NV

《iOS使用fastlane自动化打包》 安装成功

2、使用

1、打开终端,cd 到你的项目下
命令: cd + 项目目录
2、执行fastlane命令 fastlane init
如图

《iOS使用fastlane自动化打包》 fastlane init

下面会有四个选项供你选择

  1. 自动截屏。帮我们自动截取APP中的截图
  2. 自动发布beta版本用于TestFlight
  3. 自动发布到AppStore
  4. 手动设置

选择4 ,会出现如图的操作

《iOS使用fastlane自动化打包》 步骤

一直按enter键就可以了
如图就成功了

《iOS使用fastlane自动化打包》 成功

配置

我们项目下fastlane 有两个文件AppfileFastfile

《iOS使用fastlane自动化打包》 文件

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

app_identifier用于指定APP的bundle idapple_id指的是你的AppleID

Fastfile文件

# 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

default_platform(:ios)

platform :ios do
  desc "Description of what the lane does"
  lane :custom_lane do
    # add actions here: https://docs.fastlane.tools/actions
  end
end

lane :custom_lane do中的custom_lane是函数的名称,打包执行命令的时候使用。
# add actions here: https://docs.fastlane.tools/actions 这块就是让我们加具体执行的插件、命令等操作用于打包。

简单打包用于测试,可打包成ad-hoc 或者development

下面是我的配置

default_platform(:ios)

platform :ios do
  desc "Description of what the lane does"
  lane :NonTeacher do  #函数名称,执行打包的时候使用

    time = Time.new.strftime("%Y%m%d") #获取时间格式
    version = get_version_number#获取版本号
    ipaName = "Release_#{version}_#{time}.ipa"
    gym(
       scheme:"NonTeacher", #项目名称
       export_method:"ad-hoc",#打包的类型
       configuration:"Release",#模式,默认Release,还有Debug
       output_name:"#{ipaName}",#输出的包名
       output_directory:"./build"#输出的位置
  
     )

  end
end

终端先 cd 到项目下,然后执行命令fastlane NonTeacher
下面就是打包成功的截图了

《iOS使用fastlane自动化打包》 打包成功

然后我们就可以在项目下看到包了

《iOS使用fastlane自动化打包》 包的显示

关于gym插件的更多用法可以在这里查看https://docs.fastlane.tools/actions/gym/

上传到蒲公英或者fir

1、上传蒲公英
1)、cd到项目下, 安装pgyer插件 执行命令fastlane add_plugin pgyer

《iOS使用fastlane自动化打包》 安装成功

2)、重新编写项目目录下的Fastfile文件,如下:

《iOS使用fastlane自动化打包》 上传蒲公英

3)、cd 到项目下,执行命令fastlane NonTeacher ,打包上传成功

《iOS使用fastlane自动化打包》 打包上传成功

注意点:
如果遇到这种情况:
Could not find action, lane or variable 'pgyer'. Check out the documentation
可能是你安装pgyer插件的时候,不是在项目下安装的,这个插件必须在项目下面安装

2、上传至fir
1)、cd到项目下, 安装fir插件,执行命令fastlane add_plugin firim

《iOS使用fastlane自动化打包》 安装成功

2)、重新编写项目目录下的
Fastfile文件,如下:

《iOS使用fastlane自动化打包》 编辑Fastfile

3)、cd 到项目下,执行命令
fastlane NonTeacher ,打包上传成功

《iOS使用fastlane自动化打包》 上传成功

上传到appstore

本人没有尝试过,还是习惯用xcode去上传,感觉放心,如果需要了解,可以看看这篇文章https://www.jianshu.com/p/c09097211cd4

发送邮件

先说一下fastlane中的函数

执行顺序方法名说明
1before_all在执行 lane 之前只执行一次
2before_each每次执行 lane 之前都会执行一次
3lane自定义的任务
4after_each每次执行 lane 之后都会执行一次
5after_all在执行 lane 成功结束之后执行一次
6error在执行上述情况任意环境报错都会中止并执行一次

我们使用的是after_all,具体配置如下:

1、如果执行sudo gem install fastlane -NV

报错:ERROR: While executing gem … (Errno::EPERM) Operation not permitted – /usr/bin/commander
换成这样的安装命令:
sudo gem install -n /usr/local/bin fastlane

2、如果执行fastlane init一直停留在bundle update 不动,如图

《iOS使用fastlane自动化打包》 bundle update

两种解决方法如下:
1)、结束进程ctrl +c,删除项目下的fastlane文件夹,使用gem cleanup清理一下,重新cd到项目下执行fastlane init
2)、

查看一下你的源    gem sources -l
如果不是 https://gems.ruby-china.com
尝试换成 https://gems.ruby-china.com 这个源
先移除你原本的源 gem sources  --remove  + 源名称
增加新的源 gem sources --add https://gems.ruby-china.com

3、如果报这种错误

《iOS使用fastlane自动化打包》 xcode找不到

解决办法:

解决这个问题也很简单,找到你的Xcode路径,在终端输入
sudo xcode-select --switch 你的Xcode路径

1、xcode-select --print-path //查看xcode目前所在路径
2、 sudo xcode-select --switch   /Applications/Xcode10.1.app    //切换路径

这篇文章说了一下经常用的一些插件,大家可以看看https://www.jianshu.com/p/5119c115ec90
基本上就是这些,更多的东西,需要我们自己去探索,了解。
希望大家能提出宝贵的意见,可以给我留言,也可以发邮件到我的邮箱:namezyqyx@163.com
谢谢大家,如果你有更好的想法或文章请告知,不胜感激。

    原文作者:谁遇而安
    原文地址: https://www.jianshu.com/p/6ab8d2b7253a
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞