一、Fastlane 安装
1)安装 Xcode command line tools:
打开终端输入命令
xcode-select --install
2)安装Fastlane
输入命令
sudo gem install fastlane -NV
或是
brew install fastlane
两个中任何一个都可以
3)初始化Fastlane
安装完成后就是在项目初始化Fastlane
cd到你的需要打包的项目目录,然后执行命令\
fastlane init
1:自动截屏。这个功能能帮我们自动截取APP中的截图,并添加手机边框(如果需要的话)
2:自动发布beta版本用于TestFlight
3:自动发布到AppStore
4:手动设置
我在这里选的是第四个(大家可根据自己需要选择,建议选择4)
紧接着一直点击enter键,直到安装成功
安装成功之后,会在我们的项目工程目录生成下面几个文件: 先不去修改这些文件,等安装好Jenkins和配置好后再来修改
二、Jenkins 安装 和 配置
1)首先检查是否有Jenkins依赖的java环境
终端输入命令
java -version
出现java version "1.8.xx"说明已经安装了java
如果没有安装,去下面官网下载对应自己电脑系统的版本安装:www.java.com
2)检查是否有安装HomeBrew
终端输入命令
brew -v
有显示Homebrew版本说明已经安装
如果没有则使用以下命令安装Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
复制代码
终端输入下面命令安装最新版稳定Jenkins,安装时间较长
brew install jenkins-lts
启动Jenkins服务
brew services start jenkins-lts
其他命令
- 启动 Jenkins 服务:
brew services start jenkins-lts
- 重启 Jenkins 服务:
brew services restart jenkins-lts
在浏览器中输入http://localhost:8080
,首次安装应该会出现如下界面,这里需要你去找到Jenkins的初始密码,这个界面不要关闭先放着
右击访达文件,点击 前往文件夹,输入上面图片红色文字路径 找到刚才需要输入的密码输入
重启Jenkins 重新进入http://localhost:8080
输入密码后会进入以下配置界面
点击第一个安装建议的插件,等待装完 会需要等比较长时间
然后根据提交后面页面提示创建账号登录
接下来开始在Jenkins创建任务
接下来进入配置页面
选择 参数化构建过程
然后就是按照下面图片所指点击添加参数
添加 Git参数(此时可能会有很多同学这没有Git参数,或者这是英文)
没有Git参数选项: 那么先点击下面保持目前的配置,回到项目页,点击-> 返回面板 -> 系统管理 -> 插件管理 然后搜索 Git插件安装并重启Jenkins
显示英文处理:Manage Jenkins -> manage Plugins 搜索 Local 插件安装, 然后 Manage Jenkins -> Configure System 在 Locale 中 Default Language 字段填入 zh_CN
接下来我们又重新点击项目->点击配置,回到配置页
1处 输入你项目的 git地址
2处 输入你项目的 账号、密码
下图此处输入 $BRANCH
下面这2个可以不用选
然后就是构建
#!/bin/bash
#bin/bsah - l
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
set -e
#输出错误
function echo_error() {
echo "\033[31m$1\033[0m"
}
#输出信息
function echo_info() {
echo "\033[32m$1\033[0m"
}
#打包
function packaging() {
echo_info "====================开始===================="
#参数校验 $# 添加到Shell的参数个数
if [ $# != "1" ] || ([ $1 != "debug" ] && [ $1 != "release" ] && [ $1 != "appStore" ]); then
echo_error '请指定打包类型:debug, release, appStore'
exit 1
fi
#参数配置
fastlane_output_path="./fastlane_output"
#你的蒲公英账号 API Key
PGY_API_KEY="xxxxxxxxxxxxxxxxxxxx"
#定义一些符变量
export v_env=$1
export v_project_name="你的iOS项目名称"
export v_build_number="${BUILD_NUMBER}"
export v_git_branch="${GIT_BRANCH}"
export v_commit_node="$(git log -n 1 --pretty=format:"%h")"
export v_download_URL="URL"
export v_QRCode_URL="URL"
export v_last_10_logs="$(git log -n 10 --pretty=format:"%h %ci %cn%n%s%n")"
echo_info "====================安装pod依赖===================="
#安装pod依赖
rm -rf './Pods/Local Podspecs'
pod update
#pod install
#删除构建输出
rm -rf "${fastlane_output_path}"
#一个项目里面多个target设置
target="项目1名称"
if [ $1 == "debug" ]; then
target="项目2名称"
fi
#版本号
export v_app_version=$(echo "$(fastlane run get_version_number xcodeproj:OneAset.xcodeproj target:${target}))" | tr -d '\n' | sed -E 's/.*Result: ([0-9\.]*).*/\1/g')
#构建
echo_info "====================构建===================="
ipa_name="${v_project_name}_${v_app_version}_$(date +%s)"
#fastlane打包
fastlane ${v_env} ipa_name:"${ipa_name}" output_path:"${fastlane_output_path}"
#上传二进制包到蒲公英
if [ $1 != "appStore" ]; then
echo_info "====================上传二进制包到蒲公英===================="
resp=$(curl -F "file=@${fastlane_output_path}/${ipa_name}.ipa" -F "_api_key=${PGY_API_KEY}" -F "buildInstallType=1" https://www.pgyer.com/apiv2/app/upload)
echo "pgy_resp=${resp}"
#解析数据,生成下载地址
build_key=$(echo "$resp" | tr "\n" " " | sed -E 's/.*"buildKey" *: *"([^"]*)".*/\1/g')
v_QRCode_URL=$(echo "$resp" | tr "\n" " " | sed -E 's/.*"buildQRCodeURL" *: *"([^"]*)".*/\1/g' | sed -E 's/\\\//\//g')
v_download_URL="https://www.pgyer.com/apiv2/app/install?_api_key=${PGY_API_KEY}&buildKey=${build_key}"
v_download_URL="https://www.pgyer.com/${build_key}"
echo_info "二维码地址:${v_QRCode_URL}"
echo_info "app下载地址:${v_download_URL}"
#发送企业微信通知(最后面的key是企业微信机器人里面的key,需要把xxxxxxxxxxxxxx替换为你自己的)
curl -i -X POST -H "'Content-type':'application/json'" -d '{"msgtype" : "text", "text" : {"content" : "'"iOS ${v_project_name} 新版本发布了!!!\n\n打包环境:${BuildType}\n\n打包分支:${v_git_branch}\n\n版本号:${v_app_version}\n\n更新信息:${Description}\n\n下载地址:${v_download_URL}"'"} }' https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxxxxxxxx
else
echo_info "====================AppStore打包完成===================="
#发送企业微信通知
curl -i -X POST -H "'Content-type':'application/json'" -d '{"msgtype" : "text", "text" : {"content" : "'"iOS ${v_project_name} ipa包上传App Store成功啦!\n\n版本号:${v_app_version}\n\n打包环境:${BuildType}\n\n打包分支:${v_git_branch}\n\n更新信息:${Description}"'"} }' https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxxxxxxxx
fi
}
export FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT=120
#执行打包
packaging ${BuildType}
复制代码
接下来我们再次回到我们第一步Fastlane安装多出来的几个文件那
打开终端cd到你的iOS项目根目录
执行下面命令
fastlane deliver init
复制代码
如果你输入过Apple ID 那么会直接登录iTunes Connect 并下载所需数据,如果没有输入过会让你输入Apple ID,照做就是了。
然后你会发现文件发生了如下变化
接下来就是Fastfile文件的配置
default_platform(:ios)
platform :ios do
#这里是指令描述
desc "Description of what the lane does"
lane :debug do |options|
# add actions here: https://docs.fastlane.tools/actions
#automatic_code_signing和gym都是fastlane命令 可以查阅相关文档
#指定签名参数
automatic_code_signing(
#路径
path: "项目名称.xcodeproj",
#证书
code_sign_identity: "Apple Development: xxxx (xxxx)",
#描述文件 Mac上描述文件路径 ~/Library/MobileDevice/Provisioning Profiles
profile_name: "xxxxxx"
)
#指定构建参数
gym(
#
scheme: "要打包的target项目名称",
workspace: "项目名称.xcworkspace",
clean: true,
configuration: "Debug",
export_method: "development",
output_name: options[:ipa_name],
output_directory: "#{options[:output_path]}",
export_xcargs: "-allowProvisioningUpdates",
archive_path: "#{options[:output_path]}/#{options[:ipa_name]}"
)
end
lane :release do |options|
# add actions here: https://docs.fastlane.tools/actions
#automatic_code_signing和gym都是fastlane命令 可以查阅相关文档
#指定签名参数
automatic_code_signing(
#路径
path: "项目名称.xcodeproj",
#证书
code_sign_identity: "Apple Distribution: xxxxxx (xxxxx)",
#描述文件 Mac上描述文件路径 ~/Library/MobileDevice/Provisioning Profiles
profile_name: "xxxxxx"
)
#指定构建参数
gym(
scheme: "要打包的target项目名称",
workspace: "项目名称.xcworkspace",
clean: true,
configuration: "Release",
export_method: "ad-hoc",
output_name: options[:ipa_name],
output_directory: "#{options[:output_path]}",
export_xcargs: "-allowProvisioningUpdates",
archive_path: "#{options[:output_path]}/#{options[:ipa_name]}"
)
end
lane :appStore do |options|
# add actions here: https://docs.fastlane.tools/actions
puts "以app-store 方式打包"
#指定构建参数
#自动增加build版本号
increment_build_number
gym(
scheme: "要打包的target项目名称",
workspace: "项目名称.xcworkspace",
clean: true,
configuration: "Release",
output_name: options[:ipa_name],
output_directory: "#{options[:output_path]}",
export_xcargs: "-allowProvisioningUpdates",
export_options: {
method: "app-store",
provisioningProfiles: {
# bundleid,打包用的证书名字,这里也可以采用参数传递,看个人喜好吧
"com.you.bundleIdentifier": "your_appStore"
}
}
)
puts "上传 ipa 包到 iTunes connect"
#上传AppStore
deliver(
#跳过截图上传
skip_screenshots: true,
#跳过元数据上传
skip_metadata: true,
#跳过审核直接上传
force: true
)
end
end
复制代码
Deliverfile 文件编辑
添加下面信息
username "你的苹果开发账号"
复制代码
Appfile 文件编辑
添加下面信息
app_identifier("xxxxxxxxx") # The bundle identifier of your app
team_id("xxxxxxxx") # Developer Portal Team ID
apple_id("xxxxxxx") # Your Apple email address
itc_team_id("xxxxx") # App Store Connect Team ID
复制代码
记得把上面刚编辑的几个文件改动push到git仓库,否则后面Jenkins构建项目拿不到这些配置信息的。
支持信息配置完毕,接下里可以去Jenkins操作页面进行打包了(此时打包debug、release包上传到蒲公英是可以顺利进行的,但是打包App Store后面还是有几个坑需要走,请继续往下看)
按照下图点击可以进入正在打包的分支查看 控制台输出 内容
此时你会发现控制台里面会输出下面这样的内容
Error: Unable to validate your application. Sign in with the app-specific password you generated.
If you forgot the app-specific password or need to create a new one, go to appleid.apple.com
复制代码
是因为苹果开发者账号开启了双重认证 进行官网寻找解决方案,发现了这个:
fastlane官网针对双重认证情况下给出的deliver解决方案
采用了第三个方案
- 生成一个不需要进行短信认证的application specific password,这个需要到 苹果开发者中心的账号管理下进行生成 app 专用密码。注意生成后将其备份,因为页面一旦关闭将无法再次查看
- 执行命令 fastlane spaceauth -u [开发者账号] ,生成FASTLANE_SESSION,将其备份
- 配置环境变量如下:
打开终端,输入vim ~/.bash_profile
按i键进入编辑模式,输入下面信息
export FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD=[application specific password(替换为你自己的)]
export FASTLANE_SESSION=[FASTLANE_SESSION(替换为你自己的)]
复制代码
再按esc键,输入 :wq 保持并退出
- 执行source ~/.bash_profile使新增的环境变量生效,分别执行以下命令查看环境变量是否设置成功
echo $FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD
echo $FASTLANE_SESSION
复制代码
再次进入Jenkins进行打包,结果还是提示错误
[32mPlease provide your Apple Developer Program account credentials�[0m
[32mThe login information you enter will be stored in your macOS Keychain
复制代码
我们可以使用CredentialsManager重设钥匙串对应的开发者账号密码为专用密码(切记此处的密码为上面生成的专用密码)
最后再次进行重新构建,接下来可以看到大功告成,提交到App Store成功.