160

I have a shell script that I would like to run at the end of my target's build phase. However, I would like this script to only run when I build with the release configuration. How can this be done? Thanks!

mfaani
  • 33,269
  • 19
  • 164
  • 293
Reed Olsen
  • 9,099
  • 4
  • 37
  • 47

2 Answers2

337
if [ "${CONFIGURATION}" = "Release" ]; then
  echo Do something really release-like
fi

The script will run at the end of every configuration, but it won't do anything in this case unless the configuration is Release (assuming everything it does is contained within the test block).

Jason Coco
  • 77,985
  • 20
  • 184
  • 180
  • 1
    I just discovered it does not work with multi-worded configuration names. It works with "Release" but does not with "Internal Beta". – Hedin Mar 03 '11 at 18:15
  • 5
    @Hedin - that is just bash; if you have multi-word variable, you need to surround the variable in quotes. So ${CONFIGURATION} becomes "${CONFIGURATION}". I'll update the answer to include that. – Jason Coco Mar 03 '11 at 20:30
  • So I tried this with a script that uploads to crittercism the dysm file. Something's not working and xcode gets stuck on the phase when I add the wrapper of the "if". Any ideas? if [ "${CONFIGURATION}" = "Hockey Distribution" ]; then APP_ID="xxxxxxxx" API_KEY="xxxxxxxxx" source ${SRCROOT}/GlideiPhone/Vendor/CrittercismSDK/dsym_upload.sh fi – SAR622 Dec 18 '13 at 15:41
  • 18
    I have 'Debug', 'Release' (for Instruments), 'Ad Hoc' and 'App Store' configs (with different code signing), so I changed it for `if [ "${CONFIGURATION}" != "Debug" ]; then...`. – Nicolas Miari Mar 14 '14 at 01:32
  • 2
    For the if condition i want to use a #define TRUE/FALSE stored in the .pch file.. how would i reference it in the script code? – newton_guima Apr 07 '15 at 01:44
  • @skyline75489 : Bash uses `=` and `==` interchangeably. – Sandy Chapman Sep 15 '16 at 02:00
  • 2
    Can this same thing be accomplished with pre-processor flags? – Justin Vallely Sep 19 '16 at 22:17
  • 1
    This snippet was not working. I needed to change the "==" if [ "${CONFIGURATION}" == "Release" ] – drasick Mar 15 '19 at 10:05
  • if [ "${CONFIGURATION}" = "Debug" ]; then ... works for me in Xcode 14 – SM Abu Taher Asif Oct 31 '22 at 11:29
22

The easiest way to do this, is checking the "Run script only when installing" checkbox.

Menno
  • 1,232
  • 12
  • 23
  • +1 https://stackoverflow.com/questions/5913199/xcode-run-script-build-phase-run-script-only-when-installing-option – darkheartfelt Feb 15 '19 at 17:50
  • 10
    "Debug-installing on a real device (build & run from Xcode) does not count as "installing". The run script only runs when archiving (tested with Xcode 9)." https://stackoverflow.com/questions/5913199/xcode-run-script-build-phase-run-script-only-when-installing-option#comment80890236_21917496 – darkheartfelt Feb 15 '19 at 18:00
  • 7
    It won't work if you want to build your app in `Release` configuration without archiving it. The question was explicitly about `Release` configuration. – Legonaftik May 30 '19 at 13:37
  • 2
    Good comments, I think someone searching for this question will likely find either one useful. – Justin Meiners Jul 20 '22 at 20:23