Jenkins打包iOS App(xcodebuild)

2022-03-25  本文已影响0人  JonyTse

为什么要用xcodebuild 命令行打包?因为不会受到 Jenkins xcode 插件的影响。最近就是因为升级了Jenkins xcode插件,导致所有xcode项目无法打包。

先来看看 xcodebuild 命令是怎样打包和发布 ios 项目的

一 、xcodebuild 打包解析

1、xcodebuild 常用命令

2、打开钥匙串访问, 导入证书

security unlock-keychain "-p" password "/Users/***/Library/Keychains/login.keychain-db"

3、清理项目

xcodebuild -workspace ***.xcworkspace -scheme *** -configuration Release clean

4、编译 archive

xcodebuild archive -workspace $project_workspace \
    -scheme $project_scheme \
    -configuration $Configuration \
    PLATFORM_NAME=iphoneos \
    -archivePath $archive_path \
    BUILD_DIR="$build_dir" \
    CODE_SIGN_IDENTITY="$CODE_SIGN_IDENTITY"  \
    PROVISIONING_PROFILE_SPECIFIER="$CPROVISIONING_PROFILE_NAME" \
    PROVISIONING_PROFILE="${PROFILE_UUID}" \
    PRODUCT_BUNDLE_IDENTIFIER="${BUILD_ID}" \
    -quiet

参数解析

5、以版本号命名 .ipa文件

/usr/libexec/PlistBuddy -c "Print :ApplicationProperties:CFBundleVersion" ${archivePath}/Info.plist
/usr/libexec/PlistBuddy -c "Print :ApplicationProperties:CFBundleShortVersionString" ${archivePath}/Info.plist

6、输出 IPA 包文件

xcodebuild -exportArchive -archivePath $archive_path -exportOptionsPlist $exportOptionsPlist -exportPath $export_path

!错误解决

error: Missing private key for signing certificate. Failed to locate the private key matching certificate "Apple Development: *********" in the keychain. To sign with this signing certificate, install its private key in your keychain. If you don't have the private key, select a different signing certificate for CODE_SIGN_IDENTITY in the build settings editor. (in target 'ALWMallGuideDevelop' from project 'ALWMallGuide')

删除 CODE_SIGN_IDENTITY="$CODE_SIGN_IDENTITY" 参数即可

二、 jenkins配置范例

需要的插件

git,emall, Inject environment variables(环境变量注入)

1、仓库配置

image-20210603112044481.png

2、构建脚本

先添加 shell 将变量写入文件

# 打包的环境,正式环境为空,可选 {'Develop';  'Release';  ''}
APP_ENV="Develop"

#工程名字(Target名字)
Project_Name="ALWMallGuide"


# build 路径
project_workspace="${Project_Name}.xcworkspace"
project_scheme="${Project_Name}${APP_ENV}"

# 获取app版本号
APP_VERSION=`/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "${WORKSPACE}/ALWMallGuide/Resources/${project_scheme}-Info.plist"`

export_path="/Users/admin/project/build/${Project_Name}"
export_name="${export_path}/${project_scheme}_${APP_VERSION}.ipa"

# archive 目标路径
build_dir="${WORKSPACE}/build"
archive_path="${build_dir}/${project_scheme}.xcarchive"

#plist文件
exportOptionsPlist="${build_dir}/${project_scheme}.plist"


# 构建变量写入文件
echo "APP_VERSION=${APP_VERSION}
project_workspace=${project_workspace}
project_scheme=${project_scheme}
export_path=${export_path}
export_name=${export_name}
build_dir=${build_dir}
archive_path=${archive_path}
exportOptionsPlist=${exportOptionsPlist}" > BuildVariable

添加环境变量注入

image-20210603112408495.png
# 打包的环境,正式环境为空,可选 {'Develop';  'Release';  ''}
APP_ENV=Develop

#配置环境,Release或者Debug
Configuration=Release

#AdHoc版本的Bundle ID
BundleID=com.xxxx.mall

#证书名#描述文件
CODE_SIGN_IDENTITY=Apple Development: **************
CPROVISIONING_PROFILE_NAME=***********Mall_dep
PROFILE_UUID=9f4b27dc-f3f7-45e3-bb0c-816c7586560f

添加打包脚本(Execute shell)

#!/bin/bash

#author Jony

func_export_plist(){
echo '<?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">
<plist version="1.0">
<dict>
        <key>signingStyle</key>
        <string>manual</string>
        <key>method</key>
        <string>development</string>
        <key>signingCertificate</key>
        <string>iOS Developer</string>
        <key>provisioningProfiles</key>
        <dict>
                <key>'${BundleID}'</key>
                <string>'${PROFILE_UUID}'</string>
        </dict>
        <key>iCloudContainerEnvironment</key>
        <string>Development</string>
        <key>stripSwiftSymbols</key>
        <true/>
        <key>compileBitcode</key>
        <true/>
</dict>
</plist>' > ${exportOptionsPlist}
}
 
func_build(){
    # 证书解锁
    security unlock-keychain -p 123456 ~/Library/Keychains/login.keychain-db
    # 清理
    xcodebuild clean -workspace $project_workspace -scheme $project_scheme -configuration $Configuration
    # 构建 archive 包
    xcodebuild archive -workspace $project_workspace \
    -scheme $project_scheme \
    -configuration $Configuration \
    PLATFORM_NAME=iphoneos \
    -archivePath $archive_path \
    BUILD_DIR="$build_dir"  \
    PROVISIONING_PROFILE_SPECIFIER="$CPROVISIONING_PROFILE_NAME" \
    PROVISIONING_PROFILE="${PROFILE_UUID}" \
    PRODUCT_BUNDLE_IDENTIFIER="${BundleID}" -quiet
    # 输出版本号
    /usr/libexec/PlistBuddy -c "Print :ApplicationProperties:CFBundleVersion" ${archivePath}/Info.plist
    /usr/libexec/PlistBuddy -c "Print :ApplicationProperties:CFBundleShortVersionString" ${archivePath}/Info.plist
    #输出ipa包
    xcodebuild -exportArchive -archivePath $archive_path -exportOptionsPlist $exportOptionsPlist -exportPath $export_path
}

cd ${WORKSPACE}
mkdir -p build
# 编译
func_export_plist
func_build

添加发布脚本

# 生成download.plist文件
echo '<?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">
<plist version="1.0">
<dict>
    <key>items</key>
    <array>
        <dict>
            <key>assets</key>
            <array>
                <dict>
                    <key>kind</key>
                    <string>software-package</string>
                    <key>url</key>
                    <string>${url}/mallguide/'${project_scheme}'_'${APP_VERSION}'.ipa</string>
                </dict>
                <dict>
                    <key>kind</key>
                    <string>display-image</string>
                    <key>url</key>
                    <string>${url}/mallguide/image.57x57.png</string>
                </dict>
                <dict>
                    <key>kind</key>
                    <string>full-size-image</string>
                    <key>url</key>
                    <string>${url}/mallguide/image.512x512.jpg</string>
                </dict>
            </array>
            <key>metadata</key>
            <dict>
                <key>bundle-identifier</key>
                <string>com.xxxx.mall</string>
                <key>bundle-version</key>
                <string>'${APP_VERSION}'</string>
                <key>kind</key>
                <string>software</string>
                <key>title</key>
                <string>App名称</string>
            </dict>
        </dict>
    </array>
</dict>
</plist>' > ${export_path}/${project_scheme}_${APP_VERSION}_Download.plist

mv ${export_path}/${project_scheme}.ipa ${export_name}

最后添加邮件发送

上一篇下一篇

猜你喜欢

热点阅读