How to stop Xcode build from shell script - xcode

I am currently working on an app in Xcode where I've added a Build Phase that runs a shell script.
The script looks for resources from the Desktop and copies them to the app . If the files/folders don't exist, the script should cancel the build of the app.
I have tried various things to stop the build such as xcodebuild clean but I can't quite figure it out. Here is what I have:
if [ -d ~/Desktop/MyFolder ]; then
cp -r ~/Desktop/MyFolder ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/MyFolder
else
#Stop the build
fi
Is there a way to have the script tell Xcode to stop the build? If so, how can I do it?

You need to return a non-zero exit code from the script:
exit 1

Another useful technique is for the script to produce an output with an "error: " prefix.
This will cause Xcode to show the error within the build logs, and if you Xcode settings stop building on failure - it will stop right there.
In addition to this, you could also trigger a warning by printing out "warning: " prefix.
Example:
if [ -d ~/Desktop/MyFolder ]; then
cp -r ~/Desktop/MyFolder ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/MyFolder
else
echo "error: Copy failed!"
fi

Related

Retry failed xcodebuild test

I am building custom Jenkins build script (sh) for iOS app build/test checks.
But sometimes UI test fails just because of timing issue, so I want it to re run few more times to make sure the issue is real.
for (( ATTEMPT=1; ATTEMPT<=2; ATTEMPT++ ))
do
xcodebuild [flags] test #add_result_saving_mechanism
#if failed, do smth to go to next attempt. Else - break
if SOME_KIND_OF_FAIL_CHECK; then
continue
else
break
fi
fi
I used xcpretty before, so was able to read $PIPESTATUS and react accordingly, but xcpretty is crashing for xcodebuild test for some reason, so looking ways to do without it
xcodebuild [flags] test | xcpretty
STATUS="${PIPESTATUS}"
if [ "$STATUS" != "0" ]; then
FAILURE_MSG="${TARGET} (${BUILD_NAME}) failed UI/Unit testing"
#try next attempt if available
continue
else
break
fi
How can I handle these retries without pipes/xcpretty?
From the jenkins perspective - it always used to bail early if a script encountered an error, so you could try this type of syntax to prevent that (in the jenkins config this is, not in the script itself)
jenkins_build_script.sh || true
# continue with things...
Also, if you're having trouble capturing the failure itself - try piping the xcodebuild output to a log file and then grep-ing for the errors you're anticipating.
Since you said it's a script run by Jenkins you can handle the retry from Jenkins pipeline instead of inside the shell script.
As in the example from the docs
pipeline {
agent any
stages {
stage('Deploy') {
steps {
retry(3) {
sh './flakey-deploy.sh'
}
}
}
}
}
You can read about it here
Hope this will help good luck.

Jenkins execute shell output mystery

I'm tyring to run some build script in MacOS 10.11 but get no information about errors occurred during it's running. Even I try
<hudson.tasks.Shell>
<command>echo "fucking Jenkins!!!"
#!/bin/bash -l
echo "fucking Jenkins!!!"
/bin/sh -e
echo "fucking Jenkins!!!"
./build.sh $BUILD_NUMBER "Jenkins test build"
echo "fucking Jenkins!!!"</command>
</hudson.tasks.Shell>
I get in the console output
git rev-list ...
and then failure or success depending I set -e choice or not. Even echo command writes nothing. But the fact of failure doesn't help at all if there's no more information. No more options I could find over the Web. What is the secret of the execute shell plugin to make it write something to the console output.

how to to run a script and get a result in boolean in shell script

I needs to run a CLI script to deploy a application and I should save the output of the script as a boolean variable to know whether the deployment is successful or not ?
Can you please help me to do the above scenario :
This is my CLI script :
/applic/jboss/jboss-eap-6.1/bin/jboss-cli.sh -c --controller=localhost:9999 --commands="deploy /applic/jboss/Project/deploy/sample.ear --force, deployment-info --name=sample.ear, quit"
It will give an output like below :
NAME RUNTIME-NAME PERSISTENT ENABLED STATUS
sample.ear sample.ear true true OK
So, I needs to run a script which should give me the output like, whether my deployment is successful or not ?
I have created one shell script to do that. But, no luck.
#!/bin/bash
AA="[ -e /applic/jboss/Project/script/new_deploy.sh ]"
if $AA
then
echo "deployment successful"
else
echo "deployment unsuccessful"
fi
echo "done"
Can anyone let me know, how to modify the script to display the output?
Does /applic/jboss/jboss-eap-6.1/bin/jboss-cli.sh exit with a non-zero status if the deployment somehow fails? That would be the easiest method:
if /applic/jboss/jboss-eap-6.1/bin/jboss-cli.sh -c --controller=localhost:9999 --commands="deploy /applic/jboss/Project/deploy/sample.ear --force, deployment-info --name=sample.ear, quit"
then
echo deployment succeeded
else
echo deployment failed
fi
If you have to parse the output to determine the level of success, then things get a bit thornier. But answer the first question first.

Hudson is not exec-ing my bash-based build script

i have a simple "free-style" test job in huson.
it checks out a file from git (it does that part successfully)
it is also supposed to exec a script that appends to that file.
the script looks like:
#!/bin/sh -ex
echo "$0 was run on " `date` >> /tmp/failme.log
#echo "$0 was run on " `date` >> $HUDSON_HOME/failme.log
echo "this should fail"
echo "this went to stderr" >&2
exit -1
I put the 2nd line to test if the script is even run.
/tmp/failme.log is missing after a "successful" build
i can run the script as the hudson user (after allowing it to login) and the script behave properly.
I'm at a loss. I've read several inquiries here and in other forums and blogs about using hudson variables in scripts. none of them talk about anything special that i have to do to get hudson to exec the script.
thanks in advance.
nodnarb (strike that, reverse it)
yes, i'm answering my own question.
I attempted the same configuration with a new job, and the script runs as expected. I have no reason for this. I have attempted to dup the failure above 3 times, and cannot duplicate this issue. So, I am resolving this issue. maybe something "hiccuped" when the original job was created.
Thank you to all who commented.
B

How to abort build if run script phase gives an error

I would like to use a run scripts target to tag my revision in mercurial and upload it to a server. I created a new run scripts target that depends on the other target building my app, then I added two run script phases directly one after another.
Now my question: how can I prevent executing run script phase #2 if run script phase #1 gives an error (return code of script is unequal 0)?
The second script would upload the files to a server, so I only want to execute this phase if everything went right until then.
Your first script can abort the build with "exit 1", like this:
if [ error ] then
echo "There are some errors, etc..."
exit 1
fi
A solution would be to generally stop building when errors occur:
XCode -> Preferences -> Building -> Build options "Continue building after errors".
If you uncheck that, the build will stop if your script returns something unequal 0 and the second script will not be executed. If you use xcodebuild, the name for the setting is "PBXBuildsContinueAfterErrors".
exit 1 aborts the build.
echo "error: will flag it up as an error within Xcode.
echo "error: Everything has failed!"
exit 1

Resources