11

When I archive from the command line using xcodebuild archive, how can I get the path to the newly created archive? I'd like to follow on with an -exportArchive command to create an adhoc distribution.

I know that I can define an -archivePath, however if I do that then Organizer doesnt know where about this archive, so that's no good.

Thoughts?

toofah
  • 4,455
  • 4
  • 31
  • 42

3 Answers3

16

you can simply create a variable holding the path of the archive you want to generate. Then use the same path when you want to export

$ARCHIVE_PATH="<path_of_your_archive>" # can be something like "build/app_name.xcarchive"

# ARCHIVING

xcodebuild archive \
    -workspace "${APP_NAME}.xcworkspace" \
    -configuration $CONFIGUATION \
    -scheme $SCHEME \
    -archivePath $ARCHIVE_PATH

# EXPORTING

xcodebuild -exportArchive \
     -archivePath $ARCHIVE_PATH \
     -exportPath $OUTPUT_DIRECTORY \
     -exportOptionsPlist exportPlist.plist

Hope this helps you in any way!

Kieran Bond
  • 84
  • 1
  • 8
Danny Yassine
  • 661
  • 7
  • 9
  • 2
    Thanks you. The problem that I run into with this solution is that by defining the `-archivePath` parameter you take away the ability for Organizer to know where this archive is. That's what I am running into. – toofah Jun 08 '17 at 04:40
  • 5
    I did talk to Apple engineers today in the WWDC labs about this question and there is no good solution. They recommended using my own path as you suggested above and then copying the archive into the `archives` folder that Xcode uses. It looks like Xcode looks through the entire archives folder, so you dont have to depend on a folder structure. Xcode Preferences even lets you define a custom location for your archives. I think that's what I'll end up doing...I just hate moving away from the default strategy since Apple could change the way they do it at any moment. – toofah Jun 08 '17 at 04:45
1

There's $ARCHIVE_PATH variable that's only available in archive post-actions scriptsenter image description here

Edit: $ARCHIVE_PATH returns a kind of generic path that doesn't account for the date, time, or duplicate suffixes.

Use $ARCHIVE_PRODUCTS_PATH instead. Or actually REAL_ARCHIVE_PATH=$(dirname ${ARCHIVE_PRODUCTS_PATH}).

Yousef Hamza
  • 347
  • 1
  • 3
  • 13
0

Assuming that you have a standard archives path on your machine, I use this python code to get me the current archive path for builds.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import datetime

class Xcodebuild:

    @staticmethod
    def current_archive_path():
        # The default archives path is as follows
        #
        #    /Users/<USER>/Library/Developer/Xcode/Archives/<YYYY>-<MM>-<DD>
        #
        home = os.path.expanduser("~")
        archives_path = os.path.join(home, "Library/Developer/Xcode/Archives")

        if not os.path.isdir(archives_path):
            exit("error: Xcode archives directory not found at {}".format(archives_path))

        dirname = str(datetime.date.today())
        archive_path = os.path.join(archives_path, dirname)

        if not os.path.exists(archive_path):
            os.makedirs(archive_path)

        return archive_path