# Trigger iOS CI Builds on Pull Request Events

Canonical: https://figment.so/blog/mac-ci-pr-trigger-ios-build

Trigger a pull_request workflow on opened, synchronize, reopened and ready_for_review, add paths-ignore and a concurrency group, then mark the job name as a required status check in branch protection.

**Trigger a `pull_request` workflow on `opened`, `synchronize`, `reopened` and `ready_for_review`, skip docs-only changes with `paths-ignore`, cancel stale runs with a concurrency group, and mark the job as a required status check.** This page gives the complete GitHub Actions workflow for an iOS repo on a self-hosted Mac runner, plus the branch protection setting that makes the check block the merge button.

A PR build that runs on every push, never cancels stale jobs, and fires for a README edit wastes Mac runner minutes and teaches engineers to ignore CI.

## Which pull request events should start the build?

`opened`, `synchronize`, `reopened` and `ready_for_review`. By default, a `pull_request` workflow runs only on the first three ([GitHub Actions events docs](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows)). Add `ready_for_review` explicitly so promoting a draft starts a build:

```yaml
on:
  pull_request:
    types: [opened, synchronize, reopened, ready_for_review]
    branches: [main, 'release/**']
```

Without `ready_for_review`, promoting a draft does not trigger a build and the developer has to push an empty commit to unblock CI.

## How do I skip docs-only changes?

Use `paths-ignore`. It stops the workflow from starting when only excluded files changed. For an iOS repo, exclude documentation, App Store metadata, and tool configuration that cannot affect the build:

```yaml
on:
  pull_request:
    types: [opened, synchronize, reopened, ready_for_review]
    branches: [main, 'release/**']
    paths-ignore:
      - '**.md'
      - '**.txt'
      - 'docs/**'
      - '.github/CODEOWNERS'
      - 'fastlane/metadata/**'
      - 'fastlane/screenshots/**'
```

A PR that only touches `README.md` skips the workflow, and the merge button stays green if the last code commit passed. `paths-ignore` and `paths` cannot be used together at the same level. If you need include-and-exclude logic, write explicit `paths` include patterns instead.

## How do I cancel stale runs?

Add a concurrency group. When a developer pushes several commits in quick succession, each push queues a run, and without a policy you pay for builds nobody will read.

```yaml
concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: ${{ github.event_name == 'pull_request' }}
```

The group key uses the PR number for pull requests (one group per PR) and falls back to `github.ref` for push events on `main`, so a merge to `main` never cancels an in-flight release build. `cancel-in-progress` is true only for PR events.

## How do I skip draft PRs?

Add a job-level `if` condition. Event types alone also work, because `ready_for_review` runs once when the draft is promoted, but the `if` is easier to audit because the intent is visible in the job definition:

```yaml
jobs:
  ios-build-test:
    name: iOS Build & Test          # must match the branch protection check name
    runs-on: [self-hosted, macOS]
    if: github.event.pull_request.draft == false
```

With event types alone, pushing to a PR that was converted back to draft still fires `synchronize`. The `if` condition blocks that.

## What does the complete workflow look like?

```yaml
# .github/workflows/pr-build.yml
name: PR iOS Build

on:
  pull_request:
    types: [opened, synchronize, reopened, ready_for_review]
    branches: [main, 'release/**']
    paths-ignore:
      - '**.md'
      - '**.txt'
      - 'docs/**'
      - 'fastlane/metadata/**'
      - 'fastlane/screenshots/**'

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
  ios-build-test:
    name: iOS Build & Test          # must match the required check name in branch protection
    runs-on: [self-hosted, macOS]
    if: github.event.pull_request.draft == false

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Select Xcode
        run: sudo xcode-select --switch "${XCODE_PATH}"
        env:
          XCODE_PATH: ${{ vars.XCODE_PATH }}        # e.g. /Applications/Xcode_16.2.app/Contents/Developer

      - name: Resolve simulator UDID
        id: sim
        run: |
          set -euo pipefail
          UDID=$(xcrun simctl list devices available -j | python3 -c "
          import sys, json
          devs = json.load(sys.stdin)['devices']
          print(next(d['udid'] for runtime in devs.values()
                     for d in runtime if 'iPhone' in d['name']))")
          echo "udid=${UDID}" >> "$GITHUB_OUTPUT"

      - name: Build and test
        env:
          XCODE_WORKSPACE: ${{ vars.XCODE_WORKSPACE }}
          XCODE_SCHEME:    ${{ vars.XCODE_SCHEME }}
        run: |
          set -euo pipefail
          xcodebuild test \
            -workspace "${XCODE_WORKSPACE}" \
            -scheme    "${XCODE_SCHEME}" \
            -configuration Debug \
            -destination "id=${{ steps.sim.outputs.udid }}" \
            -resultBundlePath build/results.xcresult \
            -jobs 4 \
            ONLY_ACTIVE_ARCH=YES \
            | xcbeautify

      - name: Upload test results
        uses: actions/upload-artifact@v4
        if: always()
        with:
          name: test-results-${{ github.run_id }}
          path: build/results.xcresult
          retention-days: 7
```

Fill `XCODE_WORKSPACE` and `XCODE_SCHEME` from your project. The file does not know your scheme name, and an invented one fails with "scheme not found" on the first run. The scheme must be a shared scheme checked into `*.xcodeproj/xcshareddata/xcschemes/` or `*.xcworkspace/xcshareddata/xcschemes/`.

## How do I make the check block the merge button?

Require it in branch protection ([GitHub protected branches docs](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/defining-the-mergeability-of-pull-requests/about-protected-branches)):

1. Open **Settings › Branches › Add branch ruleset** (or Branch protection rule) for `main`.
2. Enable **Require status checks to pass before merging**.
3. Search for and select `iOS Build & Test`, the exact job `name:` value.
4. Optionally enable **Require branches to be up to date before merging** to block stale-base merges.

GitHub's docs warn that job names must be unique across workflows, because two workflows with a job called `build` produce ambiguous check results. Prefix with the area: `iOS Build & Test`, not `build`.

## How do I keep the PR build under 10 minutes?

A 25-minute PR build gives developers time to context-switch and forget what the PR was about. Aim for 10 minutes or less. Options, in order of impact:

| Approach | Typical impact | Tradeoff |
|----------|---------------|----------|
| Separate unit and UI test schemes; run only unit on PRs, full suite nightly on `main` | Large | UI regressions caught at merge, not before |
| Test-impact analysis: run only targets whose sources changed | Large | Requires tooling and a dependency graph |
| Incremental build with cached DerivedData | Medium | Stale cache can mask failures, so invalidate on Podfile/Package.resolved change |
| `ONLY_ACTIVE_ARCH=YES`, `-jobs <cores>` | Small | None for PR builds; release builds need all archs |
| Pre-boot the simulator before `xcodebuild test` | Small | Removes cold-boot delay from the measured window |

The two-scheme split is usually the fastest path to under 10 minutes without adopting new tooling.

## What is the equivalent setup in GitLab and Buildkite?

For GitLab, use merge request pipelines:

```yaml
# .gitlab-ci.yml
ios-build-test:
  rules:
    - if: $CI_MERGE_REQUEST_IID
      changes:
        - "**/*.swift"
        - "**/*.xcodeproj/**"
        - Podfile.lock
      when: on_success
  script:
    - xcodebuild test -workspace "$XCODE_WORKSPACE" -scheme "$XCODE_SCHEME" -destination "$DEST"
```

`$CI_MERGE_REQUEST_IID` is set only for MR pipelines, not branch pipelines. `changes:` is an allowlist (like `paths:` in GitHub Actions), not a denylist, so list what should trigger the job, not what to skip.

For Buildkite, gate the step with `if: build.pull_request.id != null` and use a pre-command hook or a `monorepo-diff` plugin to skip when only docs changed.

## What size Mac handles your pull request build volume?

Match the plan to your Xcode version and PR volume. The [Apple Silicon Mac VM](/mac-hosting#apple-silicon-vm) ($39 a month, or $6 a day) runs current Xcode and the Simulator, and suits a single team's PR runner. Use a [Dedicated M4 Pro 48 GB](/mac-hosting#dedicated-m4-pro) ($349 a month) when PRs run in parallel or the suite is heavy. The [Intel Mac VM](/mac-hosting#intel-vm) ($25 a month) works only if Xcode 26.3 or earlier on macOS Sequoia is enough for your target; Xcode 26.4 and later need an Apple silicon plan. Time `xcodebuild test` on your own branch, because no speed claim substitutes for that. You install and manage the runner.

*Related guides:*
- [Build a React Native iOS App in CI](/blog/mac-react-native-ios-ci-build)
- [Build a Flutter iOS App in CI](/blog/mac-flutter-ios-ci-build)
- [Xcode on Windows: Remote Mac Setup](/blog/mac-xcode-on-windows-remote-setup)
