Jenkins+fastlane+fir实现自动化打包

2019-11-20  本文已影响0人  测试开发雨辰
序言

由于本人是iOS开发,所以本文一切配置是基于苹果电脑配置的,本篇文章主要从以下几个方面来讲述:

Mac安装Jenkins

强烈推荐在 macOS 上采用 Homebrew 命令来安装 Jenkins,而不是通过官方网站上下载的 .pkg 包来安装。

pkg包安装问题:

这些问题比较难解决,当然有兴趣的人可以去尝试。

Jenkins命令行安装

1、安装homebrew:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2、升级java

brew cask install java

3、安装jenkins(过程有点漫长,耐心等待)

brew install jenkins

4、启动jenkins

brew services start jenkins

5、重新启动jenkins

brew services restart jenkins

6、停止jenkins

brew services stop jenkins
Fastlane介绍

fastlane是一款自动打包工具,他有一些核心的工具,比如说打包用的build_app(别名:gym),上传App Store用的deliver等等,详细移步官网地址

这里我们主要看下打包工具gym

gym
Key Description Default
workspace workspace文件路径
project project文件路径
scheme 指定工程的scheme,确认这个scheme是勾选了shared的
clean 在构建前是否要clean false
output_directory ipa文件应该存储在其中的目录 .
output_name 生成ipa文件的名字
configuration 构建app时使用的配置。默认为“发布” *
codesigning_identity 代码签名用的名字,它应该与名称完全匹配,例如'iPhone Distribution: SunApps GmbH'
export_method 用于导出archive的方法。有效值是:appstore, ad-hoc, package, enterprise, development, developer-id
export_xcargs 将额外的参数传递给打包阶段的xcodebuild。一定要引用设置名称和值,例如OTHER_LDFLAGS="-ObjC -lstdc++" (我用到了export_xcargs: "-allowProvisioningUpdates"来解决一个EXPORT FAILED问题)

Mac安装fastlane

xcode-select --install

如果本地已经安装了Xcode的话,会出现这样的提示:

xcode-select: error: command line tools are already installed, use "Software Update" to install updates
brew cask install fastlane
安装完毕后会报错: tip.png
//进入当前用户目录
cd~

//创建bash_profile文件
touch .bash_profile

//打开profile文件
open .bash_profile

//编辑,修改在文件里添加export PATH="$HOME/.fastlane/bin:$PATH",更新配置的环境变量
source .bash_profile
gem sources --add https://rubygems.org

输入fastlane,提示如下:

image.png
1、Automate screenshots: 自动截屏
2、Automate beta distribution to TestFlight: 自动发布beta版本到testFlight
3、Automate App Store distribution: 自动发布到App Store
4、Manual setup: 手动设置

我选择了4,初始化工程目录如下:


WeChat34611e205722dd4b945ef1427dff5f99.png
文件名 描述
Appfile 从 Apple Developer Portal 获取和项目相关的信息 详情
Fastfile 核心文件,存放lane任务
Deliverfile deliver的配置文件,从 iTunes Connect 获取和项目相关的信息详细
metadata 同步iTC中的元数据
screenshots 同步iTC中的截图
Jenkins、Fastlane相关配置
Jenkins相关配置

1、命令行启动Jenkins

brew services start jenkins

2、浏览器输入http://localhost:8080,进入

1.png
2.png
3.png
进入到管理员设置页面,设置好相关信息,然后设置URL,保持默认就好
4.png
5.png
进入到以下页面,就表明macOS上Jenkins的安装与配置成功
10.png
3、添加相关插件,Jenkins-->系统管理-->插件管理
image.png
image.png
4、在Jenkins上新建Item
image.png
5、配置Jenkins针对iOS项目的相关信息
jenkins配置界面.png

shell1:

export LANG=en_US.UTF-8
SCHEME="test"
IPANAME="test"
pod install
echo ${buildType}
/Users/yuchen/.fastlane/bin/fastlane gym --export_method development --configuration ${buildType} --scheme ${SCHEME}  --output_name ${IPANAME}

shell2:

#!/bin/bash

MSG=`git log -1 --pretty=%B`
MSG1="${buildType}-${MSG}"

##json解析函数
function jsonParse() { # $1 $2  json lable

     JSON_CONTENT=$1
     KEY='"'$2'":'

     echo ${JSON_CONTENT} | awk -F  ${KEY}  '{print $2}' | awk -F '"' '{print $2}'
}


##replace函数
function TrimAnd(){
    TEXT=$1
    echo ${TEXT//'\u0026'/'&'}
}

# Get API Token from http://fir.im/apps
API_TOKEN="********************"
fileName=`basename /Users/yuchen/.jenkins/workspace/TestProject/******.ipa`
apkPath="/Users/yuchen/.jenkins/workspace/TestProject/$fileName"


# ios or android
TYPE="ios"
# App 的 bundleId
BUNDLE_ID="********"

# Get upload_url
credential=$(curl -X "POST" "http://api.fir.im/apps" \
-H "Content-Type: application/json" \
-d "{\"type\":\"${TYPE}\", \"bundle_id\":\"${BUNDLE_ID}\", \"api_token\":\"${API_TOKEN}\"}" \
2>/dev/null)


SHORT_NAME=$(jsonParse "${credential}" "short")
fir_id=$(jsonParse "${credential}" "id")
binary_response=$(echo ${credential} | grep -o "binary[^}]*")
KEY=$(jsonParse "${binary_response}" "key")
TOKEN=$(jsonParse "${binary_response}" "token")
UPLOAD_URL=$(jsonParse "${binary_response}" "upload_url")

# Upload package
echo 'Uploading...'
echo '✈ -------------------------------------------- ✈'
response=$(curl -F "key=${KEY}" \
-F "token=${TOKEN}" \
-F "file=@${apkPath}" \
-F "x:build=1" \
-F "x:changelog=${MSG1}" \
${UPLOAD_URL}
)

echo ${response}

release_id=$(jsonParse "${response}" "release_id")
download_url=$(jsonParse "${response}" "download_url")

echo APP  名字:        ******IOS
echo APP  包名:       "${BUNDLE_ID}"
echo APP  类型:       "${TYPE}"
echo 应用 ID:         "${fir_id}"
echo 上传apk路径:      "${fileName}"
echo 通用二维码地址:    https://fir.im/"${SHORT_NAME}"
echo APP二维码地址: https://fir.im/"${SHORT_NAME}"?release_id="${release_id}"
QR=`curl -L -s  https://cli.im/api/qrcode/code?text=https://fir.im/"${SHORT_NAME}"?release_id="${release_id}" | grep "qrcode_plugins_img ="| awk -F '//' '{print $2}'| awk -F '\"' '{print $1}'`
echo 草料二维码地址:${QR}
echo 下载地址如下:
echo $(TrimAnd "${download_url}")
配置相关错误总结

1、jenkins使用脚本执行pod命令结果报错pod:command not found,因为没有设置pod的运行路径

解决方法:

2、错误如下图:


image.png

解决方法:在脚本中加入

export LANG=en_US.UTF-8

3、brew命令安装java8出现失败,可以尝试以下脚本解决

brew tap homebrew/cask-versions
brew cask install java
brew cask install homebrew/cask-versions/adoptopenjdk8
上一篇 下一篇

猜你喜欢

热点阅读