使用Jenkins本地构建iOS项目

系统环境

Mac OS 10.15.6
Xcode 12.4

安装Jenkins

macOS Installers for Jenkins LTS

按官网步骤用命令行安装:

brew install jenkins-lts

如果显示Updating Homebrew...卡太久,就用Control+C跳过,过会就会开始下载

Updating Homebrew...
^C==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/openjdk%401
Already downloaded: /Users/${UserName}/Library/Caches/Homebrew/downloads/fb20efadec6110a61cc259dad9bc0ea16c12ad32e0d778578354d2d8166fc674--openjdk@11-11.0.8.catalina.bottle.tar.gz
==> Downloading http://mirrors.jenkins.io/war-stable/2.249.1/jenkins.war
==> Downloading from http://ftp-chi.osuosl.org/pub/jenkins/war-stable/2.249.1/je
######################################################################## 100.0%
==> Installing dependencies for jenkins-lts: openjdk@11
==> Installing jenkins-lts dependency: openjdk@11
==> Pouring openjdk@11-11.0.8.catalina.bottle.tar.gz
==> Caveats
For the system Java wrappers to find this JDK, symlink it with
  sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk

openjdk@11 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have openjdk@11 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/openjdk@11/bin:$PATH"' >> ~/.zshrc

For compilers to find openjdk@11 you may need to set:
  export CPPFLAGS="-I/usr/local/opt/openjdk@11/include"

==> Summary
🍺  /usr/local/Cellar/openjdk@11/11.0.8: 650 files, 295.3MB
==> Installing jenkins-lts
==> /usr/local/opt/openjdk@11/bin/jar xvf jenkins.war
==> Caveats
Note: When using launchctl the port will be 8080.

To have launchd start jenkins-lts now and restart at login:
  brew services start jenkins-lts
Or, if you don't want/need a background service you can just run:
  jenkins-lts
==> Summary
🍺  /usr/local/Cellar/jenkins-lts/2.249.1: 7 files, 67.3MB, built in 6 seconds
==> `brew cleanup` has not been run in 30 days, running now...
Removing: /Users/${UserName}/Library/Logs/Homebrew/lame... (64B)
Removing: /Users/${UserName}/Library/Logs/Homebrew/wget... (64B)
Removing: /Users/${UserName}/Library/Logs/Homebrew/libpng... (64B)
Removing: /Users/${UserName}/Library/Logs/Homebrew/sox... (64B)
Removing: /Users/${UserName}/Library/Logs/Homebrew/mad... (64B)
Removing: /Users/${UserName}/Library/Logs/Homebrew/opusfile... (64B)
==> Caveats
==> openjdk@11
For the system Java wrappers to find this JDK, symlink it with
  sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk

openjdk@11 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have openjdk@11 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/openjdk@11/bin:$PATH"' >> ~/.zshrc

For compilers to find openjdk@11 you may need to set:
  export CPPFLAGS="-I/usr/local/opt/openjdk@11/include"

==> jenkins-lts
Note: When using launchctl the port will be 8080.

To have launchd start jenkins-lts now and restart at login:
  brew services start jenkins-lts
Or, if you don't want/need a background service you can just run:
  jenkins-lts

安装成功,启动jenkins-lts:

brew services start jenkins-lts

家里WiFi连不太上Github,报如下错误:

fatal: unable to access 'https://github.com/Homebrew/homebrew-services/': LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 
Error: Failure while executing; `git clone https://github.com/Homebrew/homebrew-services /usr/local/Homebrew/Library/Taps/homebrew/homebrew-services --origin=origin` exited with 128.
Error: Failure while executing; `/usr/local/bin/brew tap homebrew/services` exited with 1.

于是连手机4G热点,很快就成功了:

==> Tapping homebrew/services
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'...
remote: Enumerating objects: 28, done.
remote: Counting objects: 100% (28/28), done.
remote: Compressing objects: 100% (20/20), done.
remote: Total 1149 (delta 12), reused 8 (delta 8), pack-reused 1121
Receiving objects: 100% (1149/1149), 334.35 KiB | 256.00 KiB/s, done.
Resolving deltas: 100% (488/488), done.
Tapped 1 command (41 files, 424.0KB).
==> Successfully started `jenkins-lts` (label: homebrew.mxcl.jenkins-lts)

Jenkins后台配置

服务启动成功后,在浏览器中打开 http://localhost:8080

按提示到该路径复制密码并粘贴
点击`安装推荐的插件`即开始自动安装
等待安装完成
安装完成后,创建第一个管理员用户,保存并完成
实例配置,保存并完成
Jenkins已就绪!开始使用Jenkins
Jenkins主面板

安装插件

安装以下插件:

  • Keychains and Provisioning Profiles Management(密钥链和配置文件管理)
Keychains and Provisioning Profiles Management

上传钥匙串及配置文件

  1. 安装完Keychains and Provisioning Profiles Management插件后,可在“系统管理”底部找到该插件,点击进入配置:


    安装完该插件后,可在“系统管理”底部找到该插件,点击
  2. 准备文件

  • 钥匙串文件路径:~/Library/Keychains/login.keychain-db
  • 配置文件路径:~/Library/MobileDevice/Provisioning Profiles/
  1. 上传文件
    本地Keychain文件后缀导游-db,直接上传是无效的,需要把 login.keychain-db复制出来重命名去掉后缀-db,然后上传。
  • Password:输入本机密码。

  • Code Signing Identity:去钥匙串找到开发者证书名称并填写。

  • Provisioning Profiles Directory Path:填写默认路径(注意将${UserName}替换成你Mac的用户名):/Users/${UserName}/Library/MobileDevice/Provisioning Profiles

上传钥匙串及配置文件,然后Save

新建构建任务

不通版本的Jenkins的汉化程度不一样,中英文混杂,自己理解。


输入任务名,选择构建一个自由风格的项目,确定
添加Git仓库地址,指定分支
点击添加-Jenkins,可以通过账号密码或ssh来登录git
勾选这两项,点击保存,再回来这里就会自动填充内容

构建脚本

如果用到cocoapods,要注意将-project.xcodeproj改为-workspace.xcworkspace
还要在编译前执行pod install --verbose --no-repo-update

# 自定义变量
USE_COCOAPODS=false

# 编译参数
BUILD_CONFIGURATION="DEBUG"

# 生成时间
BUILD_TIME=$(date +%Y%m%d_%H-%M-%S)

# 名称
USER_NAME="xxxx"
PROJECT_NAME="CSSphinx"
TARGET_NAME="CSSphinxExample"

# 蒲公英Key
PGY_USER_KEY="xxxx"
PGY_API_KEY="xxxx"

# Path
INFO_PLIST_PATH="./${TARGET_NAME}/Info.plist"
PROJECT_PATH="/Users/${USER_NAME}/.jenkins/workspace/${PROJECT_NAME}"
OUTPUT_PATH="${PROJECT_PATH}/output/${BUILD_TIME}"
EXPORT_PATH="${OUTPUT_PATH}/${TARGET_NAME}"
ARCHIVE_PATH="${OUTPUT_PATH}/${TARGET_NAME}.xcarchive"
EXPORT_OPTIONS_PATH="${ARCHIVE_PATH}/Info.plist"

# 进入工程目录
cd ${PROJECT_PATH}

# 清空工程
xcodebuild clean
#xcodebuild clean -project ${PROJECT_NAME}.xcodeproj -scheme ${TARGET_NAME} -configuration ${BUILD_CONFIGURATION}

# 生成xcarchive
if [ "$USE_COCOAPODS" == true ]; then
pod install --verbose --no-repo-update
xcodebuild archive -workspace ${PROJECT_NAME}.xcworkspace -scheme ${TARGET_NAME} -configuration release -archivePath ${ARCHIVE_PATH}
else
xcodebuild archive -project ${PROJECT_NAME}.xcodeproj -scheme ${TARGET_NAME} -configuration release -archivePath ${ARCHIVE_PATH}
fi

# 导出ipa包
xcodebuild -exportArchive -archivePath ${ARCHIVE_PATH} -exportPath ${EXPORT_PATH} -exportOptionsPlist ${EXPORT_OPTIONS_PATH}

# 上传蒲公英
curl -F "file=@${EXPORT_PATH}/${TARGET_NAME}.ipa" -F "uKey=${PGY_USER_KEY}" -F "_api_key=${PGY_API_KEY}" https://qiniu-storage.pgyer.com/apiv1/app/upload
增加构建步骤-执行shell-添加构建脚本
点击立即构建,如果显示红色,则点击该构建流水进入查看错误日志

参考资料

macOS Installers for Jenkins LTS
iOS自动化构建--Jenkins

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,911评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,014评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 142,129评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,283评论 1 264
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,159评论 4 357
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,161评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,565评论 3 382
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,251评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,531评论 1 292
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,619评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,383评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,255评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,624评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,916评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,199评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,553评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,756评论 2 335

推荐阅读更多精彩内容