Jenkins iOS 自动打包上传蒲公英流水线教程
一、前提条件
- Jenkins 环境准备
- 已安装 Jenkins,并能访问 Web 界面。
-
已在 Jenkins 全局工具配置中安装:
- Git
- Python3
- CocoaPods (
pod) - Flutter SDK
- Xcode
-
Jenkins 插件
- Git Plugin(拉取代码)
- SSH Agent Plugin(管理 SSH 私钥,拉取私有仓库)
-
Pipeline Plugin(支持声明式流水线)
-
准备文件
ExportOptions.plist(导出 IPA 的配置文件)upload.py(上传蒲公英的 Python 脚本)- 项目源码仓库(Flutter 项目)
二、环境变量配置
environment {
PATH = "/opt/homebrew/bin:${env.PATH}"
LANG = "en_US.UTF-8"
LC_ALL = "en_US.UTF-8"
FLUTTER_ROOT = '/Users/<用户名>/development/flutter' // Flutter SDK 路径
EXPORT_OPTIONS_PLIST = '/Users/<用户名>/jenkins/ios_project/ios/ExportOptions.plist'
IPA_EXPORT_DIR = '/Users/<用户名>/jenkins/ios_project/ios' // 导出 IPA 的目录
UPLOAD_SCRIPT = '/Users/<用户名>/jenkins/ios_project/ios/upload.py' // 上传脚本路径
}
⚠️ 提示:请把 <用户名> 替换成你自己的 macOS 用户名。
三、流水线执行步骤
1. 清理工作区
stage('Clean Workspace') {
steps {
sh 'rm -rf ios_project'
}
}
2. 拉取代码
stage('Checkout Code') {
steps {
sshagent(credentials: ['git-ssh-credential-id']) {
sh "git clone -b ${params.BRANCH_NAME} git@your.git.repo/ios_project.git"
}
}
}
3. 检查 Pod 版本
stage('Check Pod Version') {
steps {
sh '''
echo "Pod path: $(which pod)"
pod --version
'''
}
}
4. Flutter 依赖安装
stage('Flutter Pub Get') {
steps {
dir('ios_project') {
sh "${FLUTTER_ROOT}/bin/flutter pub get"
}
}
}
5. iOS Pod Install
stage('Pod Install') {
steps {
dir('ios_project/ios') {
sh '''
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
pod install
'''
}
}
}
6. 构建 Flutter iOS Release
stage('Build Flutter iOS Release') {
steps {
dir('ios_project') {
sh "${FLUTTER_ROOT}/bin/flutter build ios --release --no-codesign"
}
}
}
7. 打包并导出 IPA
stage('Archive IPA') {
steps {
dir('ios_project/ios') {
sh '''
xcodebuild -workspace Runner.xcworkspace -scheme Runner -configuration Release -archivePath build/Runner.xcarchive archive
xcodebuild -exportArchive -archivePath build/Runner.xcarchive -exportPath ${IPA_EXPORT_DIR}/ipa -exportOptionsPlist ${EXPORT_OPTIONS_PLIST}
'''
}
}
}
8. 上传蒲公英
stage('Publish IPA using Python Script') {
steps {
script {
def ipaPath = "${IPA_EXPORT_DIR}/ipa/YourApp.ipa"
sh "python3 ${UPLOAD_SCRIPT} ${ipaPath}"
}
}
}
四、完整 Jenkinsfile 示例
pipeline {
agent any
environment {
PATH = "/opt/homebrew/bin:${env.PATH}"
LANG = "en_US.UTF-8"
LC_ALL = "en_US.UTF-8"
FLUTTER_ROOT = '/Users/<用户名>/development/flutter' // Flutter SDK 路径
EXPORT_OPTIONS_PLIST = '/Users/<用户名>/jenkins/ios_project/ios/ExportOptions.plist'
IPA_EXPORT_DIR = '/Users/<用户名>/jenkins/ios_project/ios' // IPA 导出的目标目录
UPLOAD_SCRIPT = '/Users/<用户名>/jenkins/ios_project/ios/upload.py' // 上传脚本路径
}
parameters {
string(name: 'BRANCH_NAME', defaultValue: 'test', description: 'Git 分支名')
}
stages {
stage('Clean Workspace') {
steps {
sh 'rm -rf ios_project'
}
}
stage('Checkout Code') {
steps {
sshagent(credentials: ['git-ssh-credential-id']) {
sh "git clone -b ${params.BRANCH_NAME} git@your.git.repo/ios_project.git"
}
}
}
stage('Check Pod Version') {
steps {
sh '''
echo "Pod path: $(which pod)"
pod --version
'''
}
}
stage('Flutter Pub Get') {
steps {
dir('ios_project') {
sh "${FLUTTER_ROOT}/bin/flutter pub get"
}
}
}
stage('Pod Install') {
steps {
dir('ios_project/ios') {
sh '''
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
pod install
'''
}
}
}
stage('Build Flutter iOS Release') {
steps {
dir('ios_project') {
sh "${FLUTTER_ROOT}/bin/flutter build ios --release --no-codesign"
}
}
}
stage('Archive IPA') {
steps {
dir('ios_project/ios') {
sh '''
xcodebuild -workspace Runner.xcworkspace -scheme Runner -configuration Release -archivePath build/Runner.xcarchive archive
xcodebuild -exportArchive -archivePath build/Runner.xcarchive -exportPath ${IPA_EXPORT_DIR}/ipa -exportOptionsPlist ${EXPORT_OPTIONS_PLIST}
'''
}
}
}
stage('Publish IPA using Python Script') {
steps {
script {
def ipaPath = "${IPA_EXPORT_DIR}/ipa/YourApp.ipa"
sh "python3 ${UPLOAD_SCRIPT} ${ipaPath}"
}
}
}
}
post {
always {
cleanWs()
}
}
}