从命令行生成xcarchive到特定文件夹



为了CI的目的,我需要能够在我们的夜间构建中生成XCARCHIVE和IPA文件。IPA是给我们的测试人员的,用我们的特别密钥签名,XCARCHIVE是发送给客户端的,这样他们就可以将其导入Xcode并在他们满意时提交到应用商店。

生成IPA很简单,只需稍微搜索一下,但是如何生成. xcarchive文件却让我很困惑。我发现最接近的是:

xcodebuild -scheme myscheme archive

但是,这会将。xcarve存储在一些难以找到的文件夹中,例如:

/Users/me/Library/Developer/Xcode/Archives/2011-12-14/MyApp 14-12-11 11.42 AM.xcarchive

是否有一些方法来控制归档文件的位置,它的名字是什么,以及如何避免重新编译它?我想最好的结果是从DSYM和APP生成xcarve,当你做一个"xcodebuild build"-这是可能的吗?

Xcode 5现在支持-archivePath选项:

xcodebuild -scheme myscheme archive -archivePath /path/to/AppName.xcarchive

您现在还可以从刚刚构建的存档中导出已签名的IPA:

xcodebuild -exportArchive -exportFormat IPA -exportProvisioningProfile my_profile_name -archivePath /path/to/AppName.xcarchive -exportPath /path/to/AppName.ipa

从Xcode 4 Preview 5开始,有三个环境变量可以在方案存档的post-actions中访问。

ARCHIVE_PATH: The path to the archive.
ARCHIVE_PRODUCTS_PATH: The installation location for the archived product.
ARCHIVE_DSYMS_PATH: The path to the product’s dSYM files.

你可以移动/复制存档到这里。我想在CI脚本中对这个过程有更多的控制,所以我保存了一个临时文件,它可以很容易地在我的CI脚本中找到,其中包含了这些值。

BUILD_DIR=$PROJECT_DIR/build
echo "ARCHIVE_PATH="$ARCHIVE_PATH"" > $BUILD_DIR/archive_paths.sh
echo "ARCHIVE_PRODUCTS_PATH="$ARCHIVE_PRODUCTS_PATH"" >> $BUILD_DIR/archive_paths.sh
echo "ARCHIVE_DSYMS_PATH="$ARCHIVE_DSYMS_PATH"" >> $BUILD_DIR/archive_paths.sh
echo "INFOPLIST_PATH="$INFOPLIST_PATH"" >> $BUILD_DIR/archive_paths.sh

然后在我的CI脚本中,我可以运行以下命令:

xcodebuild -alltargets -scheme [Scheme Name] -configuration [Config Name] clean archive
source build/archive_paths.sh
ARCHIVE_NAME=AppName-$APP_VERSION-$APP_BUILD.xcarchive
cp -r "$ARCHIVE_PATH" "$BUILD_DIR/$ARCHIVE_NAME"

我刚刚解决了这个问题-只需将参数-archivePath添加到您的xcode构建命令行,给定最初的问题,这意味着:

xcodebuild -scheme myscheme archive

变得……

xcodebuild -scheme myscheme archive -archivePath Build/Archive

(注意:路径是相对的,我输出我的构建到$PWD/Build)

这将把你的。app文件夹放在:
Build/Archive.xarchive/Products/Application

如果您的构建目标中已经包含了您的签名证书和配置文件,那么您可以使用以下命令创建您的IPA文件,而无需重新签名:

xcrun -v -sdk iphoneos PackageApplication -v `pwd`'/Build/Archive.xarchive/Products/Application/my.app' -o `pwd`'/myapp.ipa'

(注意:xrun不喜欢相对路径,因此pwd)

-v参数转储了大量有用的信息—该命令可能无法正确签名,但仍然以代码0退出,唉呀!

如果你发现你不能运行构建的。ipa,这可能是一个签名问题,你可以使用:

做二次检查:
codesign --verify -vvvv myapp.app

如果符号正确且未被篡改,则输出将包含:

myapp.app: valid on disk
myapp.app: satisfies its Designated Requirement

如果没有,你会看到类似这样的内容:

Codesign check fails : /blahpath/myapp.app: a sealed resource is missing or invalid
file modified: /blahpath/ls-ios-develop.app/Assets.car

…这通常意味着您正在尝试使用中间输出目录,而不是正确的存档。

我目前的解决方案是重命名用户现有的存档文件夹,运行构建,并做一个'查找'来复制我想要的存档,然后删除存档文件夹并将旧文件夹重命名回原样,在我的ruby构建脚本中使用如下代码:

# Move the existing archives out of the way
system('mv ~/Library/Developer/Xcode/Archives ~/Library/Developer/Xcode/OldArchivesTemp')
# Build the .app, the .DSYM, and the .xcarchive
system("xcodebuild -scheme "#{scheme}" clean build archive CONFIGURATION_BUILD_DIR="#{build_destination_folder}"")
# Find the xcarchive wherever it was placed and copy it where i want it
system("find ~/Library/Developer/Xcode/Archives -name *.xcarchive -exec cp -r {} "#{build_destination_folder}" ";"")
# Delete the new archives folder with this new xcarchive
system('rm -rf ~/Library/Developer/Xcode/Archives')
# Put the old archives back
system('mv ~/Library/Developer/Xcode/OldArchivesTemp ~/Library/Developer/Xcode/Archives')

这有点粗糙,但我目前没有看到一个更好的解决方案。至少它保留了用户的"存档"文件夹和所有他们已经存在的存档。

——重要提示!——

我发现,我找到的存档和cp到我想要的文件夹的代码行不能正确地复制存档内的符号链接,从而破坏了应用程序中的代码签名。你会想要用'mv'或维护符号链接的东西来替换它。干杯!

这是我为我们的Jenkins CI系统编写的一些bash。这些命令应该在xcodebuild archive命令完成后立即在脚本中运行。

BUILD_DIR="${WORKSPACE}/build"
XCODE_SCHEME="myscheme"
# Common path and partial filename
ARCHIVE_BASEPATH="${HOME}/Library/Developer/Xcode/Archives/$(date +%Y-%m-%d)/${XCODE_SCHEME}"
# Find the latest .xcarchive for the given scheme
NEW_ARCHIVE=$(ls -td "${ARCHIVE_BASEPATH}"* | head -n 1)
# Zip it up so non-Apple systems won't treat it as a dir
pushd "${NEW_ARCHIVE%/*}"
zip -r "${BUILD_DIR}/${NEW_ARCHIVE##*/}.zip" "${NEW_ARCHIVE##*/}"
popd
# Optional, disk cleanup
rm -rf "${NEW_ARCHIVE}"

BUILD_DIR用于收集工件,这样就可以很容易地从Jenkins中使用glob(如build/*.ipa,build/*.zip

)将它们归档,这与其他类似,但可能更简单一些,因为我尝试记录.xcarchive文件的位置。(我也不会移动存档文件夹,所以如果你同时做多个构建,这会工作得更好。)

调用者构建脚本生成一个新的tempfile,并将其路径设置为一个名为XCARCHIVE_PATH_TMPFILE的环境变量。这个环境变量在我的方案的Archive post-action shell脚本中可用,然后该脚本将. xarchchive的路径写入该文件。在调用xcodebuild archive之后可以读取该文件的构建脚本。

动作后shell脚本

echo $ARCHIVE_PATH > "$XCARCHIVE_PATH_TMPFILE"

在Xcode 4.6中,可以指定将方案编译成xcarfile的构建后操作:

echo "ARCHIVE_PATH="$ARCHIVE_PATH"" > $PROJECT_DIR/archive_paths.sh

构建脚本可以用来检查运行xcodebuild后是否定义了$ARCHIVE_PATH,如果是这样,输出的xcarchile可以移动到指定的文件夹中。

如果项目中的目标数量很多,这种方法不是很可维护,因为对于每个目标,都需要将相应的方案标记为"共享"并添加构建后操作。

为了解决这个问题,我创建了一个构建脚本,通过提取与当前目标名称匹配的最后一个构建,以编程方式生成存档路径。只要机器上没有运行具有相同目标名称的多个构建,此方法就可以可靠地工作(这在运行多个并发构建的生产环境中可能是一个问题)。

#!/bin/bash
#
# Script to archive an existing xcode project to a target location.
# The script checks for a post-build action that defines the $ARCHIVE_PATH as follows:
# echo "ARCHIVE_PATH="$ARCHIVE_PATH"" > $PROJECT_DIR/archive_paths.sh
# If such post-build action does not exist or sourcing it doesn't define the $ARCHIVE_PATH   
# variable, the script tries to generate it programmatically by finding the latest build
# in the expected archiving folder
#
post_build_script=archive_paths.sh
build_errors_file=build_errors.log
OUTPUT=output/
XCODEBUILD_CMD='/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild'
TARGET_SDK=iphoneos
function archive()
{
    echo "Archiving target '$1'"
    # Delete $post_build_script if it already exists as it should be generated by a 
    # post-build action
    rm -f $post_build_script
    # Use custom provisioning profile and code sign identity if specified, otherwise
    # default to project settings
    # Note: xcodebuild always returns 0 even if the build failed. We look for failure in
    # the stderr output instead
    if [[ ! -z "$2" ]] && [[ ! -z "$3" ]]; then 
        ${XCODEBUILD_CMD} clean archive -scheme $1 -sdk "${TARGET_SDK}" 
        "CODE_SIGN_IDENTITY=$3" "PROVISIONING_PROFILE=$2" 2>$build_errors_file
    else
        ${XCODEBUILD_CMD} clean archive -scheme $1 -sdk "${TARGET_SDK}"
        2>$build_errors_file  
    fi
    errors=`grep -wc "The following build commands failed" $build_errors_file`
    if [ "$errors" != "0" ]
    then
        echo "BUILD FAILED. Error Log:"
        cat $build_errors_file
        rm $build_errors_file
        exit 1
    fi
    rm $build_errors_file
    # Check if archive_paths.sh exists
    if [ -f "$post_build_script" ]; then
        source "$post_build_script"
        if [ -z "$ARCHIVE_PATH" ]; then
            echo "'$post_build_script' exists but ARCHIVE_PATH was not set.
              Enabling auto-detection" 
        fi
    fi
    if [ -z "$ARCHIVE_PATH" ]; then
        # This is the format of the xcarchive path:
        # /Users/$USER/Library/Developer/Xcode/Archives/`date +%Y-%m-%d`/$1 
        # `date +%d-%m-%Y %H.%M`.xcarchive
        # In order to avoid mismatches with the hour/minute of creation of the archive and
        # the current time, we list all archives with the correct target that have been
        # built in the current day (this may fail if the build wraps around midnight) and
        # fetch the correct file with a combination of ls and grep.
        # This script can break only if there are multiple targets with exactly the same
        # name running at the same time.
        EXTRACTED_LINE=$(ls -lrt /Users/$USER/Library/Developer/Xcode/Archives/`date
          +%Y-%m-%d`/ | grep $1 `date +%d-%m-%Y` | tail -n 1)
        if [ "$EXTRACTED_LINE" == "" ]; then
            echo "Error: couldn't fetch archive path"
            exit 1
        fi
        # ls -lrt prints lines with the following format
        # drwxr-xr-x  5 mario  1306712193  170 25 Jul 17:17 ArchiveTest 25-07-2013
        #   17.17.xcarchive
        # We can split this line with the " " separator and take the latest bit:
        #   17.17.xcarchive
        FILE_NAME_SUFFIX=$(echo $EXTRACTED_LINE | awk '{split($0,a," "); print a[11]}')
        if [ "$FILE_NAME_SUFFIX" == "" ]; then
            echo "Error: couldn't fetch archive path"
            exit 1
        fi
        # Finally, we can put everything together to generate the path to the xcarchive
        ARCHIVE_PATH="/Users/$USER/Library/Developer/Xcode/Archives/`date 
          +%Y-%m-%d`/$1 `date +%d-%m-%Y` $FILE_NAME_SUFFIX/"
    fi
    # Create output folder if it doesn't already exist
    mkdir -p "$OUTPUT"
    # Move archived xcarchive build to designated output folder
    mv -v "$ARCHIVE_PATH" "$OUTPUT"
}

# Check number of command line args
if [ $# -lt 1 ]; then
    echo "Syntax: `basename $0` <target name> [/path/to/provisioning-profile]
      [<code sign identity]"
    exit 1
fi
if [ ! -z "$2" ]; then
    PROVISIONING_PROFILE="$2"
fi
if [ ! -z "$3" ]; then
    SIGN_PROVISIONING_PROFILE="$3"
else
    if [ ! -z "$PROVISIONING_PROFILE" ]; then
        SIGN_PROVISIONING_PROFILE=$(cat "$PROVISIONING_PROFILE" | egrep -a -o
          '[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}')
    fi
fi

archive "$1" "$PROVISIONING_PROFILE" "$SIGN_PROVISIONING_PROFILE"

完整的源代码和Xcode项目示例可以在这里找到:

https://github.com/bizz84/Xcode-xcarchive-command

最新更新