fastlane+jenkins自动打包

这两个工具都可以通过homebrew安装,所以请先在电脑中安装homebrew。在终端执行homebrew的安装命令即可。

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

fastlane

fastlane提供的功能分两种,一种是官方自己提供的功能,被fastlane称之为Action,例如match、gym、sign、deliver等等;还有一种是第三方提供的功能,在fastlane中被称为plugin(插件),比如蒲公英、Fir这两个第三方的ipa分发平台都在fastlane上提供了上传ipa的插件,所以fastlane提供的服务是可扩展的。所有的action和plugin的使用方法都可以在fastlane的文档中心很遍历的搜索到。目前fastlane的plugin还是很多的。

fastlane文档中心

安装

brew install fastlane

使用方法

终端cd到工程目录下面,执行“fastlane init”。命令执行完成后会在工程目录下面生成如下的文件。

image.png

红框标注的即是fastlane为我们生成的文件。这里我们重点需要关注的有Gemfile、Fastfile和Pluginfile三个文件。

Gemfile

image.png

gemfile文件用来申明打包脚本需要用到的ruby工具,因为我们在打包脚本中使用了cocoapods的install命令,所以这里也要申明一下cocoapods。

Pluginfile

image.png

pluginfile文件用来申明我们使用了哪些第三方插件,这也是fastlane比较好的一点,允许第三方开发人员提交自定义的插件。

Fastfile(打包脚本)

我们先列出几个常用的官方action的说明和用法。

match

提供证书和配置文件的管理功能,团队开发时建议使用该功能进行证书和配置文件的统一管理,创建一个证书和配置文件的仓库,用于存储相应的证书和配置文件。

仓库结构:

截屏2020-06-17 15.55.50.png

certs文件夹下存储不同打包环境所需的证书和私钥,例如development下面存储开发所用的cer文件和p12私钥文件,fastlane不支持带密码的私钥文件,所以导出p12时不要设置密码,证书文件和私钥文件都需要经过加密,且加密后的文件需要符合命名规范。

加密方法:

  • 执行 openssl pkcs12 -nocerts -nodes -out key.pem -in {证书}.p12 生成.pem文件
  • 执行 openssl aes-256-cbc -k {密码} -in key.pem -out {cert_id}.p12 -a生成加密后的p12
  • 执行 openssl aes-256-cbc -k {密码} -in {证书}.cer -out {cert_id}.cer -a 生成加密的证书

cert_id可以在钥匙串中通过显示简介中的用户id获得。

profiles文件夹下存储打包时target使用的配置文件,有些app可能有多个target,可以将对应的配置文件都存储下来,配置文件也需要加密,且加密后的文件需要符合命名规则。

加密配置文件:

openssl aes-256-cbc -k {密码} -in xxxx.mobileprovision -out Development_yyyy.mobileprovision -a

命名规则:

{Development/ADHoc/AppStore/InHouse}_bundleId.mobileprovision

将加密好的证书和配置文件上传到仓库的对应文件夹下,然后就可以通过match组件安装到不同的开发mac中了。

match(
      git_url: "[email protected]:wangwenxuan/enterpriseCertsRepo.git",
      type: "development",
      app_identifier: ["com.suiyi.foodshop1","com.suiyi.foodshop1.watchkit.watchkitextension","com.suiyi.foodshop1.watchkit"],
      readonly: true,
      verbose: true
    )

match会根据type和app_identifier去profiles和certs文件夹下的对应目录寻找匹配app_identifier的配置文件和证书并下载,下载完成后解密并安装。密码通过fastlane的环境变量指定:

ENV["MATCH_PASSWORD"] = "123"

sign

提供签名配置功能,为targets中指定的工程target设置签名证书、配置文件、bundleId。签名证书的名称就是钥匙串中对应证书的常用名称,

配置证书从苹果开发者网站中的profiles中获取,多target情况下可以设置多个automatic_code_signing,只要指定好targets参数即可。

automatic_code_signing(
    	path: "sxsx.xcodeproj",
    	use_automatic_signing: false,
    	targets: ["sxsx"],
    	code_sign_identity: "Apple Development: 明华 梅 (WFB4N4L3AQ)",//证书名称
    	profile_name: "iPhone Dev",//配置文件名称
    	bundle_identifier: "com.suiyi.foodshop1"
    )

gym

提供打包功能,使用方法如下:

gym(
      workspace: "sxsx.xcworkspace",
      scheme: "sxsx",
      clean: true,
      configuration: "Debug",
      export_method: "development",
      output_directory: "./fastlane/development",//文件输出路径
      output_name: "sxsx_#{timeStr}.ipa",//ipa名称
      include_symbols: true,     
      include_bitcode: false,
      export_options: {
          compileBitcode: false,
          provisioningProfiles: {
              app_identifier => "iPhone Dev.mobileprovision"
          },
          signingCertificate: "Apple Development: 明华 梅 (WFB4N4L3AQ)"
      }
    )

这里需要注意的一点是workspace的值,从fastfile文件的路径来看,工程的入口文件的路径应该是"../sxsx.xcworkspace",但是实际上的值为"sxsx.xcworkspace",这里需要注意一下。

configuration的值默认是Release,某些场景下我们需要打Debug的包时设置为Debug。

export_method的值根据使用的配置文件不同可以设置不同的值,比如app-store, ad-hoc, package, enterprise, development, developer-id。

export_options中的配置为生成archieve文件后的导出配置,具体支持哪些设置可以通过xcodebuild -help查看。

脚本如何执行?

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

该例中custom_lane就是最终fastlane的一个可执行方法,可以在工程目录下使用"fastlane custom_lane"执行该方法。

fastlane最后的注意点

不要拷贝其他工程的脚本文件到新工程里,新工程必须通过fastlane init创建相应的脚本文件。

Jenkins

安装

使用homebrew安装

brew install jenkins

安装完成后在终端执行"jenkins"即可启动jenkins服务器。

配置打包任务

  1. image.png
  2. 输入任务名称并选择流水线。image.png
  3. 设置忽略老的构建缓存。image.png
  4. 设置源码仓库地址和打包分支信息
  5. 设置打包触发器,可手动触发也可像图中一样设置定时打包。image.png
  6. 设置任务被触发时执行的shell命令。image.png

猜你喜欢

转载自blog.csdn.net/u010843426/article/details/106824020