25 Sep 2022 ~ 2 min read

Building Xcode applications on the CLI for personal distribution


I recently started to automate the build and release of OctoBlast using Github Actions. In this post, I will show you how to build your application on the command line.

Building our application

We will be using xcodebuild to create an archive of our application. This is the same command that is used by Xcode (I'm using versions 13 and 14) when building applications. The script below creates an archive and then exports the application as an .app file. You might want to run xcodebuild -list to see the available schemes. We will be using the Release scheme.

xcodebuild clean archive -scheme BuildingAppsOnTheCli -configuration Release -archivePath builds/archives/BuildingAppsOnTheCli.xcarchive

The above command will create an archive in the builds/archives directory.

builds
└── archives
   └── BuildingAppsOnTheCli.xcarchive

Before we can export the archive, we need to create a Info.plist file. This will tell xcodebuild how to export the application. You can add a new Property List file to the project via the UI.

If we try to export the archive now we will get an error about "teamID" not being specified. By default it will try to build an app to be distributed on the AppStore but we want to distribute it ourselves. To fix this, add the key method and value mac-application to the Info.plist file.

Empty property list

Now running the following command again will export the archive to an .app file.

xcodebuild -exportArchive -archivePath builds/archives/BuildingAppsOnTheCli.xcarchive -exportPath builds/exports/BuildingAppsOnTheCli -exportOptionsPlist BuildingAppsOnTheCli/Info.plist

Now our ./builds directory will look like this:

builds
├── archives
│  └── BuildingAppsOnTheCli.xcarchive
└── exports
   └── BuildingAppsOnTheCli
      └── BuildingAppsOnTheCli.app

If we locate and run the build application, we will see that it works as expected.

Our Mac app running

Bringing it all together

rm -rf builds
xcodebuild clean archive -scheme BuildingAppsOnTheCli -configuration Release -archivePath builds/archives/BuildingAppsOnTheCli.xcarchive
xcodebuild -exportArchive -archivePath builds/archives/BuildingAppsOnTheCli.xcarchive -exportPath builds/exports/BuildingAppsOnTheCli -exportOptionsPlist BuildingAppsOnTheCli/Info.plist

Source code for this post can be found here. See Part 2 where we'll be automating a way to set the app version.


Headshot of Jason Watson

Hi, I'm Jason. I'm a software engineer and architect. You can follow me on Twitter, see some of my work on GitHub, or read more about me on my website.