How to monitor build status, handle errors, and cancel builds programmatically

Last updated: February 3, 2026

If you've integrated Codemagic using build start and status callback endpoints, you may need additional functionality to monitor builds in real-time, handle build failures, and cancel running builds. Here's how to implement these features using current API capabilities.

Tracking Build Success and Failure Status

To programmatically determine if a build succeeded or failed, you can implement a marker-based approach:

Step 1: Add a success marker

At the end of your workflow as a post-build step, add this step to mark successful builds:

- name: Build finished successfully
  script: touch ~/SUCCESS

This creates a marker file (~/SUCCESS) only if the build finishes successfully.

Step 2: Check status in post-publishing script

Since publishing scripts always run (even after build failures), add a post-publishing script to check the final status:

publishing:
  scripts:
    - name: Report publishing status
      script: |
        if [ -f ~/SUCCESS ]; then
          STATUS=$(curl -s -H "Content-Type: application/json" \
            -H "x-auth-token: $CODEMAGIC_API_TOKEN" \
            --request GET "https://api.codemagic.io/builds/$CM_BUILD_ID" | \
            jq -r '.build.buildActions[]? | select(.type=="publishing") | .status')

          if [ "$STATUS" = "success" ]; then
            echo "✅ Build succeeded"
          else
            echo "❌ Publishing failed"
          fi
        else
          echo "❌ Build failed"
        fi

This script checks for the success marker and verifies publishing status to provide accurate build results.

Canceling Running Builds

You can stop a running build programmatically using the REST API:

POST /builds/<build_id>/cancel

For detailed information about this endpoint, refer to the Codemagic REST API documentation.

Accessing Build Error Logs

Build logs and error information can be accessed through the Codemagic API besides the option to view and download from the Codemagic UI. When a build fails, the error details will be available in the build response data that you can fetch using the build ID.