美文网首页
使用Jenkins本地构建iOS项目

使用Jenkins本地构建iOS项目

作者: Aaron升 | 来源:发表于2021-04-05 23:53 被阅读0次

系统环境

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

相关文章

网友评论

      本文标题:使用Jenkins本地构建iOS项目

      本文链接:https://www.haomeiwen.com/subject/oqxqkltx.html