forcedotcom / sfdx-bamboo-org

Bamboo examples with org development
Apache License 2.0
11 stars 18 forks source link

Question: Parsing CLI Output #4

Open william-powell opened 4 years ago

william-powell commented 4 years ago

Thanks for putting this together. I stumbled on this after cobbling this all together myself.

Any ideas on how to parse the CLI output to fail the build or deploy on any SFDX error?

In otherwords, if the deploy command responds with "ERROR running force:mdapi:deploy: The metadata deploy operation failed.", how do I mark the task as failed in bamboo?

Thanks again!

scromiecaremore commented 3 years ago

I don't know if this is still a problem for you but it may help others:

Here's how I handled it (I'm using this with a scratch org so not using force:mdapi:deploy)

-- parse the result with jq into a variable. export PUSHRESULT="$(eval sfdx force:source:push --json | jq '.status')"

-- switch on the result variable. if [ "$PUSHRESULT" == 0 ]; then echo Push Succeeded elif [ "$PUSHRESULT" == "1" ]; then { echo "Error in push"; exit $PUSHRESULT; } elif [ "$PUSHRESULT" == "2" ]; then { echo "Error in push - bad syntax"; exit $PUSHRESULT; } elif [ "$PUSHRESULT" == "127" ]; then { echo "Error in push - File not found"; exit $PUSHRESULT; } else { echo "Unknown error in push - Check log"; exit $PUSHRESULT; } fi

--Note: This will only work for those CLI commands that have an option to view results as JSON.

Here's another option:

sfdx force:org:create --setdefaultusername --definitionfile config/enterprise-scratch-def.json --wait 10 --durationdays 1 if [ ${#} != 0 ]; then echo "Scratch org could not be created"; exit $?; fi

Also - this assumes you have a runner set up to run the script. In that runner batch file, you run your script like this:

docker run --cap-add=NET_ADMIN --name=$1 salesforce

if [ "$?" != 0 ]; then echo "Error running the deploy script in the salesforce container. Check the log" exit -1; fi

This will allow you to check if the docker run command was successful or not. If the result of the docker run is unsuccessful, $? will not be equal to 0 and you can have it exit. This will cause bamboo to stop and you can check your log.