Fastlane一键发布Cocoapods仓库

1,088 阅读4分钟

今儿看了一位兄弟的文章Fastlane自动化管理自己的私有库,想到我许久之前写过一个快捷管理pod发布的脚本。

依赖Fastlane

首先需要安装Fastlane,这个脚本管理集合,可以用来打包发布ipa,cocoapods等等,比较方便,就是更新很频繁,不会开飞机的话可能比较漫长。

步骤

  • 初始化fastlane 首先需要在仓库根目录执行fastlane init初始化fastlane脚本文件,生成Fastfile文件以及actions等。

  • actions脚本编辑 主要是对Git的操作,打tag,push等等

module Fastlane
    module Actions
        module SharedValues
            REMOVE_GIT_TAG_CUSTOM_VALUE = :REMOVE_GIT_TAG_CUSTOM_VALUE
        end
        
        class RemoveGitTagAction < Action
            def self.run(params)
            command = []
            
            target_tag = params[:tag]
            remove_local = params[:remove_local]
            remove_remote = params[:remove_remote]
            
            command << "git tag -d #{target_tag}" if remove_local
            command << "git push origin :#{target_tag}" if remove_remote
            
            if command.empty?
            UI.message('👉 If you really want to delete a tag, you should to set up remove_local and remove_remote at least one for true!')
            else
            result = command.join(' & ')
            Actions.sh(result)
            UI.message('Remove git tag Successfully! 🎉')
        end
    end
    
    #####################################################
    # @!group Documentation
    #####################################################
    
    def self.description
    'Remove git tag'
end

def self.details
'Remove the local tag or remote tag for a git repertory'
end

def self.available_options
[
FastlaneCore::ConfigItem.new(key: :tag,
                             description: 'The tag to delete',
                             is_string: true,
                             optional: false),
FastlaneCore::ConfigItem.new(key: :remove_local,
                             description: 'If delete local tag',
                             is_string: false,
                             optional: true,
                             default_value: true),
FastlaneCore::ConfigItem.new(key: :remove_remote,
                             description: 'If delete remote tag',
                             is_string: false,
                             optional: true,
                             default_value: true)
]
end

def self.output

end

def self.return_value
nil
end

def self.authors
# So no one will ever forget your contribution to fastlane :) You are awesome btw!
["组件"]
end

def self.is_supported?(platform)
# you can do things like
#
#  true
#
#  platform == :ios
#
[:ios, :mac].include?(platform)
#

#        platform == :ios
end
def self.example_code
[
'remove_git_tag(tag: "0.1.0") # Delete both local tag and remote tag',
'remove_git_tag(tag: "0.1.0", remove_local: false) # Only delete remote tag',
'remove_git_tag(tag: "0.1.0", remove_remote: false) # Only delete local tag'
]
end

def self.category
:source_control
end
end
end
end

这块脚本没啥太大变化,直接拿过去用就行。

  • Fastfile脚本编辑

这个是真正的打包验证操作

lane :release_pod do |options|

  target_version = options[:t]
 
  target_project = options[:p]

  target_repo  = options[:r]

  spec_path = "#{target_project}.podspec"
  # git pull
  git_pull 
  # 确认是 master 分支
  ensure_git_branch
  # 修改 spec 为即将发布的版本
  version_bump_podspec(path: spec_path, version_number: target_version)
  # 提交代码到远程仓库
  git_add(path: '.')
  git_commit(path: '.', message: 'release')
  push_to_git_remote
  # 检查对于 tag 是否已经存在
  if git_tag_exists(tag: target_version)
      # 删除对应 tag
      remove_git_tag(tag: target_version)
  end
  # 添加 tag
  add_git_tag(tag: target_version)
  # 提交 tag
  push_git_tags
  # 验证 spec 文件
  pod_lib_lint(verbose: true, allow_warnings: true)
  # # pod trunk push 'spec_path'
  # pod_push(path: spec_path, allow_warnings: true,verbose: true)
    # 检查是否传了 repo 参数
  if target_repo
    pod_push(path: spec_path, repo: target_repo, allow_warnings: true, verbose: true)
  else
    # pod trunk push 'spec_path'
    pod_push(path: spec_path, allow_warnings: true, verbose: true)
  end
end

我这没有push内容,如有必要可编辑一下脚本加上,也可以直接使用我提供的。

这个时候其实就可以用终端进入到目录输入命令调用fastlane命令进行打包发布仓库了,但是每次都这么搞很麻烦,所以我搞了个简单的下边进入正题:

  • 执行脚本编辑 写个shell脚本,chmod +x改成了可执行性文件,放到和fastlane文件夹目录同级就行,主要是执行时获取路径,然后执行询问。脚本我写了抓取当前电脑私有仓库和公有仓库,以及抓取当前pod的podspec,只要路径能获取到就没问题。
#!/bin/bash

# 计时
SECONDS=0
# 获取工程根目录

fadir()  
{  
    local this_dir=`pwd`  
    local child_dir="$1" 
    dirname "$child_dir" 
    cd $this_dir  
} 
CUR_DIR=$(cd `dirname $0` && pwd -P ) 
echo "当前文件路径 $CUR_DIR"
cd $CUR_DIR

echo "
🎉  🎉  🎉  🎉  🎉  🎉  🎉  🎉  🎉  🎉  🎉  🎉  
"
echo "请选择要发布的podspec序号:
"
echo "(0) 退出
"
#查找文件
  a=1
  list=`find . -type f -name "*.podspec"`
  for i in $list  
    do  
    echo "($a) $i
    "  
    array[$a]=$i
    a=$(($a+1)) 
  done  
echo "
🎉  🎉  🎉  🎉  🎉  🎉  🎉  🎉  🎉  🎉  🎉  🎉  
"
read input

case $input in
  
    [1-9])
# 处理文件名 
	name1=${array[$input]} 
   # echo "111$name1"
# 截取掉/左边的
	name2=${name1#*/}
    # echo "222$name2"
# 截取掉。后边的
	podspec=${name2%.*}
    # echo "333$podspec"

    echo "
🚀 已经选择仓库名称为:$podspec
    "

    read -p "🚀 请输入版本号: " user_tags


echo "
🚀 版本号: V $user_tags 
"

    ;; 

    0)
    exit
    ;;
esac


echo "🎉  🎉  🎉  🎉  🎉  🎉  🎉  🎉  🎉  🎉  🎉  🎉
 "
echo "是否发布pod到私有仓库?:
"
echo "(0) 不是
"
#查找文件
  b=1
  listRepo=`ls ~/.cocoapods/repos`
  for i in $listRepo  
    do  
    echo "($b) $i
    "  
    arrayRepo[$b]=$i
    b=$(($b+1)) 
  done  
echo "
🎉  🎉  🎉  🎉  🎉  🎉  🎉  🎉  🎉  🎉  🎉  🎉  
"
read input

case $input in
  
    [1-99])

     repo=${arrayRepo[$input]} 
    echo "
🚀 已经选择Repo名称为:$repo
    "

echo "
🚀 即将发布 V $user_tags 版本的 $podspec$repo 
"
    fastlane release_pod t:$user_tags p:$podspec r:$repo
    ;; 

    0)
    echo "
🚀 即将发布 V $user_tags 版本的 $podspec
"
    fastlane release_pod t:$user_tags p:$podspec 
    ;;
esac

echo "
🚀 总用时: ${SECONDS}s
"
exit
 

双击后执行结果就是这样,根据提示选择输入序号:要发布的podspec,然后版本号,最后选择发布到哪一个仓库,私有或公开,最后直接回车即可完成发布了

iShot2021-03-22 11.28.10.png

最后

抓取仓库列表是~/.cocoapods/repos目录,如有不同可以文本编辑修改下。

repos目录列表无法区分哪个是公开哪个是私有,都列出来了,所以询问是否发布到私有仓库,选择0直接发布到cocoapods公开仓,选择自己的或公司的私有仓(名字是你自己取的那个),就会发布到私有仓。

以后可以进入到工程目录,然后双击脚本,就开始执行发布操作了,IPA打包也可以这样搞。

具体用法参见:IPAAutoBuild里边的脚本可以直接下载下来用。还有一个打包ipa的脚本

不知道脚本路径关系的可以看一下我的pod库,全都是这个脚本发布的。 比如:SwiftMediator