I'm on the MacBuildCloud team; we run Mac build machines. This week we wrote a GitHub Action that starts a build on a remote machine and waits for it, and these are the things we had to get right. None of it depends on our product: it applies to any workflow that kicks off an iOS build, on a GitHub-hosted macOS runner or somewhere else.
Why not just build on every push?
For most apps, a full iOS build on every push is more than you need. What people usually want is:
- a nightly build, so someone notices when the main branch stops compiling,
- a button to build any branch on demand,
- a build when a release is published.
GitHub Actions has a trigger for each. Each one also has a catch.
1. Nightly: schedule
on:
schedule:
- cron: '0 18 * * 1-5'
Things the docs say that are easy to miss:
-
It only runs on the default branch, on its latest commit. You can't schedule a nightly build of
developfrom a workflow that only lives ondevelop. -
The time is UTC by default. The example above is 18:00 UTC. GitHub now lets you set an IANA
timezoneper cron entry if you would rather think in local time. -
Runs can be late. GitHub says scheduled runs may be delayed when load is high, and the start of every hour is a high-load time. Under heavy load some queued runs can be dropped. Pick an odd minute like
17instead of0. -
The shortest interval is every 5 minutes, and the
@dailystyle shortcuts aren't supported. - In a public repository, scheduled workflows switch themselves off after 60 days with no repository activity. A quiet side project's nightly build can simply stop.
2. On demand: workflow_dispatch
on:
workflow_dispatch:
This adds a Run workflow button on the Actions tab, where you can pick a branch. The catch: the workflow file has to be on the default branch before the button appears. Adding it on a feature branch and wondering where the button went is a rite of passage.
3. On release: release
on:
release:
types: [published]
Without types, every release activity triggers the workflow, so drafts and edits start builds too. published is usually the one you want.
4. Don't build the same push twice
This is the one that would have cost our users money.
A lot of build services, ours included, can already build every push through a GitHub App or a webhook, and report back as a check on the commit. If you then also add a workflow that starts a build on: push, every push is built twice, and on a metered service, billed twice. Nothing fails; you only find out from the bill.
So decide where push builds come from:
- If the service already builds every push, keep your workflow to
schedule,workflow_dispatchandrelease. - If you want Actions to decide, for example only build after your tests pass with
needs:, turn off the push builds on the service side.
Our own example repositories ship a workflow with only workflow_dispatch for exactly this reason.
5. If the workflow starts a remote build, make it behave like a local one
If your workflow hands the build to an external service rather than running xcodebuild itself, three things matter:
Wait and fail properly. Starting the build and finishing the job green is the default mistake. Poll until the remote build ends, and exit non-zero if it failed, so the run turns red and branch protection means something. Print a link to the remote build page so whoever is looking at the red run can find the log.
Cancel what you started. If someone cancels the workflow run, or the job hits its timeout, the remote build keeps going unless you cancel it yourself. On a service billed by time, that is a machine running for nobody. Trap the cancellation in your step and call the service's cancel endpoint, and set your own time limit on the wait as well.
Expect empty secrets on forks. GitHub doesn't pass secrets (other than GITHUB_TOKEN) to workflows triggered by pull requests from forks. Your API token will be an empty string there. Check for that and fail with a clear message instead of a confusing 401 three steps later.
A minimal shape
name: iOS build
on:
schedule:
- cron: '17 18 * * 1-5'
workflow_dispatch:
release:
types: [published]
jobs:
build:
runs-on: ubuntu-latest # the Mac work happens on the remote side
timeout-minutes: 70
steps:
- name: Start remote build and wait
env:
BUILD_TOKEN: ${{ secrets.BUILD_TOKEN }}
run: |
if [ -z "$BUILD_TOKEN" ]; then
echo "BUILD_TOKEN is empty (fork pull request, or the secret is missing)"; exit 1
fi
./scripts/start-and-wait.sh # start, poll, cancel on TERM, exit non-zero on failure
If you build on a GitHub-hosted macOS runner instead, drop the remote part. The triggers and the gotchas in sections 1 to 4 are the same.
Checklist
- Nightly uses
schedule, on the default branch, at an odd minute, in a time zone you set on purpose. - The Run workflow button exists because the file is on the default branch.
- Release builds use
types: [published]. - Push builds come from exactly one place.
- A remote build is waited for, fails the run when it fails, and is cancelled when the run is.
- Fork pull requests get a clear "token is empty" message.
This article was written with the help of AI and checked against the GitHub Actions documentation and our own work on the action described above.
Top comments (0)