mac 使用fastlane 自动部署

985 阅读2分钟

1. 文件结构

image.png

2. 代码实现

进入项目目录,

  • 部署测试环境 执行./build_beta.sh
  • 部署培训环境 执行./build_train.sh
  • 部署正式环境 执行./build_prod.sh
# fastlane/build_beta.sh 测试 环境
#!/bin/bash
# 测试 环境
# fastlane ios custom_lane buildType:beta notInc:1 #测试 环境  不自增 version版本号
# fastlane ios custom_lane buildType:beta #测试 环境  默认自增 version版本号
cd /Users/jason_pro/Desktop/app_project/yzs-app-bloombeauty-ios/BloomBeauty/fastlane
fastlane ios custom_lane buildType:beta


# fastlane/build_train.sh 培训 环境
cd /Users/jason_pro/Desktop/app_project/yzs-app-bloombeauty-ios/BloomBeauty/fastlane
fastlane ios custom_lane buildType:train


# fastlane/build_prod.sh 正式 环境
cd /Users/jason_pro/Desktop/app_project/yzs-app-bloombeauty-ios/BloomBeauty/fastlane
fastlane ios custom_lane buildType:prod

fastlane/Fastfile

# 先安装插件 sudo fastlane add_plugin fastlane-plugin-versioning

# 执行命令  
# fastlane ios custom_lane    #直接跑开发环境 不建议

# 
# fastlane ios custom_lane buildType:beta    #测试 环境
# fastlane ios custom_lane buildType:train    #培训 环境
# fastlane ios custom_lane buildType:prod    #正式 环境 
# 如: 加参数  notInc = 1 不自动自增加编号
# fastlane ios custom_lane buildType:beta notInc:1 #测试 环境 
# fastlane ios custom_lane buildType:prod notInc:1 #正式 环境

# 注意 不用xcode编辑 general的 版本好和标题,修改后,
# 版本号会变成 $(CURRENT_PROJECT_VERSION) 和 $(CURRENT_PROJECT_VERSION) 导致代码获取失败 建议使用 vscode打开

default_platform(:ios)

platform :ios do
  before_all do
    #添加延时 公司的电脑太慢了 
    ENV["FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT"] = "120"
    ENV["FASTLANE_XCODEBUILD_SETTINGS_RETRIES"] = "4"
  end
  desc "Description of what the lane does"
  lane :custom_lane do|op|
      projectName = "BloomBeauty" 
      buildType = op[:buildType]
      notInc = op[:notInc]
      
      buildtTarget = ""
      buildTimeName = ""
      if buildType.nil? || buildType.empty?
           buildtTarget = projectName
           buildTimeName = Time.new.strftime("%Y%m%d-%H_%M_%S") + projectName
      else
           buildtTarget = projectName + "_" + buildType
           buildTimeName = Time.new.strftime("%Y%m%d-%H_%M_%S") + "-" + projectName + "_" + buildType
      end
      puts "buildtTarget: " + buildtTarget
      
      puts "buildTimeName: " + buildTimeName 
      #获取当前的app 版本  
      
      nowVersionBundle = get_build_number_from_plist( target: buildtTarget)
      puts "nowVersionBundle: " + nowVersionBundle  
      if notInc.nil? || notInc.empty? || notInc == 0
          #默认递增
          puts "开始自增版本号......."
          newVersion , newVersionBundle = getIOSVersionBundleIncrementArray nowVersionBundle
          setNewIOSVersionBundle buildtTarget , newVersion , newVersionBundle
      else
          puts "不自增版本号......."
      end

      gym( 
        scheme:buildtTarget,
        export_method:"enterprise",
        #output_directory:"../../yzs-app-bloombeauty-ios-build/build/"+buildTimeName,
        output_directory:"/Users/jason_pro/Desktop/发布文件夹/mp.ifashioncloud.com_ftp/centric_"+ buildType +"/",
        archive_path:"../../yzs-app-bloombeauty-ios-build/Archive-"+buildTimeName,
        output_name:'centric',
      )
      
      after_all do
          puts "开始执行shell命令" 
          system './startUpload.sh ' + buildType
          puts "执行shell命令---结束" 
      end
  end
end

#  不使用插件自增(有bug),自己封装 自定义动态递增方法-增加版本号:参数为当前的版本信息 返回 version版本号 和 实际bundle版本号
# app使用的是 bundle版本号判断升级
def getIOSVersionBundleIncrementArray(nowVersionBundle)
    newArray = nowVersionBundle.split(".")
    lastNum = newArray.pop
    newVersion = newArray.join(".")
    lastNumInt = Integer(lastNum) + 1
    newArray.insert(3,lastNumInt)
    newVersionBundle = newArray.join(".")
    [newVersion , newVersionBundle]
    #puts "newVersion: " + newVersion
    #puts "newVersionBundle: " + newVersionBundle
end

#封装修改版本号的方法 
def setNewIOSVersionBundle(target,newVersion,newVersionBundle)
      #
      puts "开始设置newVersion: " + newVersion
      puts "开始设置newVersionBundle: " + newVersionBundle
      increment_version_number_in_plist(
              target: target,
              version_number: newVersion
      )
      increment_build_number_in_plist(
              target: target,
              build_number: newVersionBundle
      )
    
end

上传的各个环境sh文件 测试环境 startUpload_beta.sh 培训环境 startUpload_train.sh 正式环境 startUpload_prod.sh

# 记得用 sudo 运行
# 测试
./startUpload.sh beta
# 培训
./startUpload.sh train
# 正式
./startUpload.sh prod

批处理上传七牛 startUpload.sh

#通过参数 控制上传的版本
#注意这里 需要提前 下载和配置七牛的插件qshell 和 账户登录
#调用 ./startUpload.sh beta centric 
echo "shell 第一个参数,buildType: $1"
#先删除服务器代码
echo '开始删除服务器ipa.....'
qshell delete yzs apps/centric_$1/centric.ipa
#上传api包
echo '开始上传ipa....................'
qshell fput yzs apps/centric_$1/centric.ipa /Users/jason_pro/Desktop/发布文件夹/mp.ifashioncloud.com_ftp/centric_$1/centric.ipa
#刷新文件
echo '刷新文件ipa..........'
qshell cdnrefresh -i torefresh.txt