3

I've got a bunch of scripts that get called when the appcenter-pre-build.sh is called. For example, one of them is a simple check to see if the current branch tag already exists on the repository.

  #!/usr/bin/env bash

  set -e # Exit immediately if a command exits with a non-zero status (failure)

  # 1 Fetch tags
  git fetch --tags

  # See if the tag exists
  if git tag --list | egrep -q "^$VERSION_TAG$"
  then
    echo "Error: Found tag. Exiting."
    exit 1
  else
    git tag $VERSION_TAG
    git push origin $VERSION_TAG
  fi

If the tag is found, I want to abort the build in AppCenter and fail it. This worked perfectly fine when I was running everything through Xcode Server but for some reason, I cannot figure out how to abort the build upon failure of my script. I'm not seeing much documentation on this particular subject and the AppCenter folk over at Microsoft are taking their sweet time getting back to me.

Anyone have experience with this and/or know how to fail an AppCenter build from their scripts? Thanks in advance for your thoughts!

Craig Fisher
  • 371
  • 2
  • 15

1 Answers1

5

Okay, figured it out. Looks like sending a curl request to cancel the build using the env variable "$APPCENTER_BUILD_ID" takes care of the issue. Exiting your script with a non-zero is NOT working inside AppCenter.

Here's a sample of what to do. I just put it in a special "cancelAppCenterBuild.sh" script and called it in place of my exits.

  API_TOKEN="<YourAppToken>"
  OWNER_NAME="<YourOwnerOrOrganizationName>"
  APP_NAME="<YourAppName>"

  curl -iv "https://appcenter.ms/api/v0.1/apps/$OWNER_NAME/$APP_NAME/builds/$APPCENTER_BUILD_ID" \
  -X PATCH \
  -d "{\"status\":\"cancelling\"}" \
  --header 'Content-Type: application/json' \
  --header "X-API-Token: $API_TOKEN"

Pro tip: If you've ever renamed your app, AppCenter servers are having issues referencing the new name. I was getting a 403 with a forbidden message. You might have to change your app name to whatever the original name was or just rebuild the app from scratch within AppCenter.

Craig Fisher
  • 371
  • 2
  • 15
  • Thank you for your answer! how do I failing the build (not cancelling it)? could not find it in their api – chenop Aug 29 '22 at 05:27