mattoddie.dev

Xcode Cloud workflows and the App Store Connect API

Xcode Cloud is the only CI that Proxly, Pocketflare and Ground Crew use. There's no GitHub Actions, no Fastlane and no signing certificates in a repo. This post covers what Xcode Cloud replaces, the workflows that ship Proxly, how a release goes from a pull request to the App Store, what the App Store Connect API lets you do with Xcode Cloud from outside Xcode, and an app built on top of it.

Replacing GitHub Actions

The usual way to build an iPhone app on every pull request is GitHub Actions on a macOS runner. It works, but a lot of the workflow file isn't about your app. It's an elaborate impression of a developer's Mac: picking an Xcode, installing a signing certificate, fetching provisioning profiles, bumping the build number and uploading to TestFlight with an API key. Each of those needs a secret in the repo's settings and a step that breaks whenever Apple changes something, which Apple does.

Xcode Cloud does each of those jobs itself, because it's part of the same system that signs and distributes the app:

JobGitHub ActionsXcode Cloud
A Mac with the right XcodeA macOS runner image, then xcode-select or a setup action to choose a versionPick the Xcode and macOS version in the workflow
SigningCertificate, password and profiles as secrets, often managed with Fastlane MatchHandled for you with your team's cloud-managed certificates
Build numbersA script or Fastlane step, usually based on the run numberSet on every archive
Upload to TestFlightAn App Store Connect API key as a secret, plus Fastlane or altoolThe archive action uploads it, and a post-action hands it to testers
Tests on several devicesA matrix of xcodebuild -destination jobsA list of simulators on the test action, run in parallel
Skip docs-only changespaths-ignoreA files and folders rule on the start condition
Cancel outdated runsconcurrency with cancel-in-progressAuto-cancel builds
Block a bad mergeA required status checkThe same: Xcode Cloud reports each action to GitHub as a check

That last row is what makes the switch painless. Xcode Cloud posts each action on a pull request back to GitHub as a check run, such as Proxly | Pull Request | Test - iOS, so a branch protection rule can require it exactly as it would a GitHub Actions job. It sits in the same list of checks on the pull request as any other.

So Proxly's repo has no .github folder at all. There are no secrets to rotate and no YAML to keep in step with Xcode. Bliss.

It's cheaper too, at this size. Apple Developer Program membership includes 25 compute hours of Xcode Cloud a month, which is plenty for a few pull requests a day. macOS runners are the most expensive kind on GitHub, and a private repo pays for them once the free minutes are gone.

The trade-offs are real. Workflows live in App Store Connect rather than in the repo, so they aren't reviewed like code and there's no history of changes. Turning Xcode Cloud on for a new app needs Xcode on a Mac. Builds start from scratch more often than on a machine you own, so a big project with lots of dependencies spends more of those hours compiling. And Xcode Cloud only builds Apple platforms: if the same repo also has an Android app, a server or a library to publish, those still need GitHub Actions or something like it. For a repo that's just an iPhone app and its website, nothing else is needed.

The workflows

Proxly has three workflows, and I'd start any app with the same set:

WorkflowStarts onActions
Pull RequestPull requests into mainTest on an iPhone and an iPad simulator
TestFlightChanges on mainTest, then archive for TestFlight (Internal Testing Only)
ReleaseTags starting with vClean build, test, then archive for TestFlight and App Store

The split between the last two is deliberate. Every merge reaches internal testers through TestFlight within minutes, but only a tag makes a build that can go to the App Store. Both have a TestFlight Internal Testing post-action, so their builds go straight to the team's devices without waiting for someone to add them in App Store Connect. If you have outside beta testers, add TestFlight External Testing to Release only: internal-only builds can't go to them.

A few settings make these much better than the defaults.

Ignore changes that can't affect the build. Each start condition can have a files and folders rule. Proxly's repo also holds its website, App Store screenshots and Markdown docs, so the pull request and TestFlight workflows skip commits that only touch website/, AppStore/ or *.md. A typo fix on the website doesn't cost twenty minutes of compute.

Auto-cancel builds. Branch and pull request start conditions can cancel a running build when a newer commit arrives. If you push three "fix typo" commits to a pull request in five minutes, you only want the last one tested.

Test on more than one device. A test action can run on several simulators at once. An iPhone and an iPad catch most layout problems, and they run in parallel, so it costs compute hours but little extra time.

Choose the archive's audience on purpose. An archive action is either TestFlight (Internal Testing Only) or TestFlight and App Store. An internal-only build is limited to your own team: it can't go to external testers or be submitted to App Review. That's exactly what you want from main, because nothing can be shipped by accident. Only the Release workflow makes builds the App Store will take.

Make release builds clean. A clean build doesn't reuse anything from earlier builds. It's slower, so it's not worth it on every merge, but for the build you ship it rules out anything stale from the cache.

Use "Latest Release" for Xcode and macOS. Pinning a version means a workflow quietly falls behind. When a new Xcode needs a fix, the pull request workflow tells you before a release depends on it.

Build numbersXcode Cloud sets CFBundleVersion on every archive from its own counter, so don't bump CURRENT_PROJECT_VERSION by hand, and definitely don't write a script to do it. The counter is shared by all of an app's workflows, so a TestFlight build and a Release build never clash. If an app already has builds from elsewhere, set the next build number under Xcode Cloud → Settings → Build Number in App Store Connect, so the counter starts above them.

From pull request to App Store

With those three workflows, a release is a version bump, a tag and a few clicks:

  1. Open a pull request that changes MARKETING_VERSION, say to 1.1.0. The Pull Request workflow tests it.
  2. Merge it. TestFlight archives 1.1.0 for internal testing, and it's on your phone a few minutes later.
  3. When it's ready, tag that commit and push the tag: git tag v1.1.0 && git push origin v1.1.0. Release makes a clean, App Store-eligible build of it.
  4. Install the release build from TestFlight and check it. This is the build that will ship, so it's the one to test, not the earlier build from main.
  5. In App Store Connect, add version 1.1.0, choose that build, write the release notes and submit it for review.
A release in five steps: open a pull request and the Pull Request workflow tests it, merge and TestFlight makes an internal build, tag v1.1.0 and Release makes a clean build, check that release build on your phone, then submit it to App Review by hand. 1 2 3 4 5 Open a PR Merge Tag v1.1.0 Check it Submit Pull Request runs the tests TestFlight internal build Release clean build release build on your phone App Review by hand
FIG 1A release in five stops, with a human only at the last two.

The tag comes first, so git always records exactly which commit shipped. If the release build needs a fix, merge it, bump to 1.1.1 and tag again. Don't move a tag that's already been built. If you'd rather keep the version number until it ships, tag release candidates as v1.1.0-rc.1, v1.1.0-rc.2 and so on; they start with v too.

Submitting to App Review stays a manual step on purpose. It's the one point where I want to have looked at the build myself. As an app grows, one more workflow earns its place: a Nightly scheduled workflow that runs the tests and the analyser on main, to catch a new Xcode or a flaky test while nobody's waiting on it.

Custom scripts

If a build needs more than Xcode does on its own, Xcode Cloud runs three optional scripts from a ci_scripts folder next to the project: ci_post_clone.sh, ci_pre_xcodebuild.sh and ci_post_xcodebuild.sh. They see environment variables such as CI_WORKFLOW, CI_BRANCH, CI_TAG and CI_BUILD_NUMBER, plus any you add to the workflow. That's where you'd install a tool with Homebrew or write a secret into a config file.

Proxly doesn't have a ci_scripts folder. It uses Swift Package Manager, which Xcode Cloud resolves itself, and nothing in the build needs a secret. Every script is one more thing to keep working, and the best script is the one you never had to write.

Driving Xcode Cloud from the API

Everything you see in the Xcode Cloud tab of App Store Connect comes from the App Store Connect API, and most of it can be changed there too. You create a key under Users and Access → Integrations, download the .p8 file once, and sign a short-lived token with it for each request.

The token is a JWT signed with ES256. A team key puts its issuer ID in iss; an individual key leaves iss out and sends sub: "user" instead. Apple rejects tokens that live longer than 20 minutes. In Swift, CryptoKit does the signing, and the only catch is that the signature has to be the raw 64-byte form, not DER:

TokenSigner.swiftSwift
struct Header: Encodable { let alg = "ES256"; let kid: String; let typ = "JWT" }
struct Claims: Encodable {
    let iss: String?   // team keys
    let sub: String?   // "user" for individual keys
    let iat: Int
    let exp: Int       // at most 20 minutes after iat
    let aud = "appstoreconnect-v1"
}

let signingInput = header.base64URL + "." + claims.base64URL
let signature = try key.signature(for: Data(signingInput.utf8))
return signingInput + "." + signature.rawRepresentation.base64URL

The Xcode Cloud part of the API is a handful of resources. An app with Xcode Cloud turned on is a ciProduct. It has workflows, and each run of a workflow is a ciBuildRun made of ciBuildActions, which hold the issues, test results and logs:

The Xcode Cloud resources in the API: a ciProduct (your app) has ciWorkflows, each workflow has ciBuildRuns, each run has ciBuildActions, and each action holds issues, testResults and artifacts. ciProduct your app ciWorkflow Pull Request ciBuildRun build 42 ciBuildAction Test - iOS issues testResults artifacts
FIG 2Apps have workflows, workflows have runs, and the juicy details hang off each action.
ToRequest
List apps with Xcode CloudGET /v1/ciProducts?include=app
List an app's buildsGET /v1/ciProducts/{id}/buildRuns?sort=-number
See a build's actionsGET /v1/ciBuildRuns/{id}/actions
Read errors and warningsGET /v1/ciBuildActions/{id}/issues
Read test resultsGET /v1/ciBuildActions/{id}/testResults
Download logs and archivesGET /v1/ciBuildActions/{id}/artifacts
Start a build or rebuildPOST /v1/ciBuildRuns
Create, edit or delete a workflowPOST, PATCH or DELETE /v1/ciWorkflows

It's JSON:API, so related objects come back in an included array that you join up by type and ID, and lists are paged with links.next. Starting a build is a ciBuildRuns object pointing at a workflow and, optionally, a branch, tag or pull request:

POST /v1/ciBuildRunsJSON
{
  "data": {
    "type": "ciBuildRuns",
    "attributes": { "clean": false },
    "relationships": {
      "workflow": { "data": { "type": "ciWorkflows", "id": "…" } },
      "sourceBranchOrTag": { "data": { "type": "scmGitReferences", "id": "…" } }
    }
  }
}

A rebuild is the same request with a buildRun relationship instead, and it builds the same commit again. A workflow is one object with its start conditions, its actions, and links to the product, repository, Xcode version and macOS version, so you can copy one workflow into another app by reading it, swapping the links and posting it back.

What the API can't do

The gaps are worth knowing before you plan anything around the API:

  • Turning Xcode Cloud on for an app. There's no way to create a ciProduct or connect a repository. The first workflow has to be made in Xcode, from Product → Xcode Cloud → Create Workflow, which is also where you grant access to GitHub. After that, the API can add as many workflows as you like.
  • Cancelling a build. You can start one but not stop it. Auto-cancel on the start condition is the closest you get.
  • Environment variables and post-actions. A workflow's custom environment variables, its secrets, and post-actions such as sending a build to a TestFlight group or notifying Slack aren't in the API. A workflow created through the API has none, so add them in App Store Connect afterwards.

None of these get in the way day to day, but the first one matters if you're setting up a new app from your phone. You can't, yet, so dig out the Mac.

Ground Crew: Xcode Cloud on your phone

Apple doesn't make an Xcode Cloud app, and checking a build from a phone meant the App Store Connect website, which is fine for a status and painful for a test failure. So I built one. Ground Crew is an iPhone and iPad app that uses the API above, with your own key, to:

  • show every app's builds as they run, with each action's errors, warnings and test results
  • start a build on any branch, tag or pull request, or rebuild one, clean if you need to
  • create, edit, switch off and delete workflows
  • tell you when a build finishes, or only when one fails

Its Flight Plans are the workflows from this post as templates: pull request checks, TestFlight on main, App Store releases from tags, a nightly health check, releases on demand and feature branch builds. Pick one and Ground Crew fills in the scheme, project and branch from the app's existing workflow, so once Xcode has made the first one, the rest take a few taps.

The key is the same .p8 from earlier. Ground Crew keeps it in the device's Keychain and signs each request on the device, so there's no server in between and nothing to sign up for. The API's gaps apply too: it can't turn Xcode Cloud on for a new app or cancel a build, and it tells you so where it matters.

Ground Crew is free on the App Store. There's more at groundcrew.mattoddie.dev.

For how Proxly handles a server's credentials, Managing Proxmox from an iPhone, securely covers certificate pinning, tokens and the Keychain.

Written by · Markdown version