How can I stop an Xcode Archive build if my git working copy is dirty?

I do not want an Archive build to succeed unless my working directory is clean. Therefore, I want to add a “Run Script” build phase (or something similar) to my Xcode project such that:

IF I am performing an Archive build…

  • How do I avoid issues with “Protected Mode” using IEDriverServer in Selenium?
  • How to put build cause data into build list on jenkins?
  • TFS 2015 build server with git line endings
  • How to promote a specific build number from another job in Jenkins?
  • Gitlab CI - How to trigger a build only if changes happen on particular set of files
  • Connect Github to secured Jenkins via HTTP Post-Commit Hook
  • AND there are uncommitted changes in my source directory

    THEN the build fails with an error.

    I’m working with Xcode 4 and git 1.7.

    What’s a good, concise, reusable script that will do the job?

  • Why are git-stash options not prefixed with dashes?
  • How do I add my project from Android Studio to my GitHub page?
  • What is the git equivalent of hg heads / hg parents?
  • Using Git in R-studio: cannot stage modified code files
  • Using both Git and Clearcase: How can we check in Git code changes to a new Clearcase version?
  • Should I “add” each new file in git if I did “add .” at the beginning?
  • One Solution collect form web for “How can I stop an Xcode Archive build if my git working copy is dirty?”

    Here’s the script I’ve come up with:

    if [ -z "$(git status --porcelain)" ]; then
        TAG=`date +xcarchive-%Y%m%d-%H%M%S`
        echo "Working directory clean, creating tag ${TAG}"
        git tag -a -m "Xcode Archive created" ${TAG}
        exit 0
        echo "error: Working directory dirty, stopping build"
        exit 1

    As a bonus, it creates a tag if the working copy is clean.

    The clean/dirty check is based on this question (which I forgot I had proposed an answer for).

    If you don’t want to create a tag, remove the git tag line.

    If you don’t want to stop the build, remove the exit 1 line.

    To install this in a project:

    1. Put this in a file in your project directory (I called it and make sure the executable bit is set (chmod +x)
    2. In your Xcode Project, add a new “External Build System” target
      • Name: “Archive Housekeeper” (or whatever you like)
      • Build Tool: ./
    3. On the Xcode menu, select Product -> Edit Scheme…
    4. In the Build section, add the new target, then uncheck all the boxes except the one in the Archive column. This ensures the script will only be run on Archive. (See this question for an explanation and a nice screenshot.)
    5. Now try to create an archive, and watch it fail because you haven’t checked in these changes!

    It would be nice if Xcode 4 Pre- and Post- actions could be used for this (so you don’t need to create a “fake” target), but they don’t seem able to affect the build, and also I have no idea in what directory they are executed, what environment variables are available, or where their output goes.

    Git Baby is a git and github fan, let's start git clone.