«

iOS Jenkins 自动打包上传至蒲公英 持续集成

CodeiOS 发布于 阅读:63


  1. 安装JAVA环境
    Jenkins 是一个用Java编写的持续集成工具,依赖于Java,所以在安装Jenkins前,需要先安装Java环境。

首先,检查电脑是否已经安装有环境。

在终端输入命令:java -version。

如果已经安装,则会打印java版本。

image
否则,会说没有安装

如果没有安装Java环境的话,需要去官网下载JAVA JDK(注意是JDK,不是JRE)。在下载前注意勾选上方的 Accept License Agreement。否则,无法下载。

image

  1. 安装Jenkins
  2. 使用Homebrew的命令行进行安装
    在终端输入命令:brew install jenkins

如果没有安装Homebrew,则需要先安装。

【Homebrew 安装方法】:前往Homebrew官网,直接拷贝官网页面上的命令,在终端运行即可。

  1. 启动jenkins,等待其安装完毕。
    在终端输入命令:jenkins

  2. 浏览器输入http://localhost:8080并进行访问。

  3. 安装插件
    Xcode integration
    GIT plugin
    GitLab Plugin
    Gitlab Hook Plugin
    Keychains and Provisioning Profiles Management

首页的左侧,点击系统管理
在系统管理界面找到插件管理
在管理插件界面,切换到可选插件选项卡,在右上角的过滤搜索里面输入我们要安装的插件名称,回车。找到我们需要的插件,打勾,点击直接安装。

  1. 项目新建和设置
    点击首页的创建一个新任务
    选择构建一个自由风格的软件项目。PS:这里的任务名称还是写我们的APP项目名比较好。后面的配置会用到这个任务名称。

image

  1. 配置项目
  2. General 参数。
    切换到General选项。勾选“丢弃旧的构建”。

设置包(即构建)的保留天数,以及最大保留个数。这个主要按照需要做设置

“丢弃旧的构建”不勾选也可以。

image

  1. 源码管理
    切换到源码管理选项。

输入我们要打包的项目的Git地址。最好SSH的。

填写我们要打包的分支(branch)。

点击Add按钮,Add按钮会弹出一个按钮Jenkins,点击它,进行添加SSH key

image

  1. 构建触发器
    切换到“构建触发器”选项。

这里只设置最常用的Poll SCM。切换到“构建触发器”,勾选“Poll SCM”。

H/20 表示:每20分支构建一次。

image

  1. 构建环境

image

image

  1. Excute shell
    切换到“构建”选项

勾选“增加构建步骤”里面的Excute shell。

添加命令到Comman上。

image
打包

!/bin/bash -l

export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8

pod install --verbose --no-repo-update

打包环境

config="Release"

config="Debug"

构建

!/bin/bash -l

export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8

pod install --verbose --no-repo-update

打包环境 (核心)

config="Release"

config="Debug"

构建

xcodebuild -archivePath "/Users/(用户名)/.jenkins/workspace/iOS-lighthouse-ci_beta/build/Debug-iphoneos/(项目名称).xcarchive" -workspace (项目名称).xcworkspace -sdk iphoneos -scheme "(项目名称)" -configuration ${config} archive

xcodebuild -exportArchive -archivePath "/Users/(用户名)/.jenkins/workspace/iOS-lighthouse-ci_beta/build/Debug-iphoneos/(项目名称).xcarchive" -configuration ${config} -exportPath "/Users/(用户名)/.jenkins/workspace/iOS-lighthouse-ci_beta/build/(项目名称)_debug" -exportOptionsPlist '/Users/(用户名)/.jenkins/workspace/iOS-lighthouse-ci_beta/build/ExportOptions.plist' -allowProvisioningUpdates

上传至蒲公英平台

通过api上传到蒲公英当中

echo "===上传至蒲公英平台==="

蒲公英userkey

pgyerUserKey="userkey"

蒲公英apiKey

pgyerApiKey="apiKey"

指定ipa输出地址

project_path=$(pwd)

指定项目名

project_name="项目名"

描述信息

des=cat ci-beta-description.txt
echo "des:+$des"
packageTime="打包时间:【date \"+%Y-%m-%d %H:%M:%S\"】---"
echo $packageTime

ipa_path="${project_path}/build/(项目名称)_debug"

if [ -e ${ipa_path}/${project_name}.ipa ]; then

RESULT=$(curl -F "_api_key=${pgyerApiKey}" \
-F "file=@${ipa_path}/${project_name}.ipa" \
-F "buildInstallType=2" \
-F "buildPassword=1230" \
-F "buildUpdateDescription=${packageTime}${des}" \
https://www.pgyer.com/apiv2/app/upload)

# echo ${RESULT}
if [ "${RESULT}" ]; then
    echo "===完成蒲公英平台上传==="
else
    echo "===上传蒲公英平台失败==="
fi
# open ${ipa_path}

else
echo "===上传蒲公英平台失败==="
fi

  1. Keychains and Provisioning Profiles Management
    login.keychain 文件路径 /Users/(用户名)/Library/Keychains

image

image
问题
error: Couldn't load -exportOptionsPlist: The file “ExportOptions.plist” couldn’t be opened because there is no such file.
Error Domain=NSCocoaErrorDomain Code=260 "The file “ExportOptions.plist” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/Users/ageren/.jenkins/workspace/Vol_Chat/build/ExportOptions.plist, NSUnderlyingError=0x7fb479178710 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
解决

在/Users/(用户名)/.jenkins/workspace/(项目名称)/build/路径新建ExportOptions.plist文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

compileBitcode method development provisioningProfiles (APP bundle ID) (描述文件名称) signingCertificate iPhone Developer signingStyle manual stripSwiftSymbols teamID (组织编号) thinning <none>