Skip to content

Instantly share code, notes, and snippets.

@oriolete
Forked from benahm/deployDestructiveChange.sh
Last active November 28, 2019 13:15
Show Gist options
  • Save oriolete/5f72cccaf40b1e5d3e29c40fe1378f03 to your computer and use it in GitHub Desktop.
Save oriolete/5f72cccaf40b1e5d3e29c40fe1378f03 to your computer and use it in GitHub Desktop.
Deploy a destructive change using the Salesforce CLI
#!/bin/sh
#
# author : benahm
# description : deploy a destructive change
#function to exit after input validations
die () {
echo >&2 "$@"
exit 1
}
# inputs
echo "************************** DESTRUCTIVE DEPLOYMENT ************************** "
read -p "Enter target environment user or alias: " TARGET_ENV
[ ! -z "$TARGET_ENV" ] || die "Target environment not provided!!!"
read -p "Enter path of source code: " SOURCE_PATH
[ ! -z "$SOURCE_PATH" ] || die "Source code path not provided!!!"
read -p "Enter current api version: " API_VERSION
[ ! -z "$API_VERSION" ] || die "API version not provided!!!"
read -p "Enter specific test class to run, this provide faster deployment: " TEST_CLASS
echo "Clean existing destructivePackage folder"
rm -rf deployment/destructivePackage &>/dev/null
mkdir -p deployment/destructivePackage &>/dev/null
echo "Converting Source format to Metadata API format"
sfdx force:source:convert -r "${SOURCE_PATH}" -d deployment/destructivePackage
# copy package.xml to desctructiveChanges.xml
cp deployment/destructivePackage/package.xml deployment/destructiveChanges.xml
# generate an empty (containing only the api version tag) package.xml
cat <<EOT > deployment/package.xml
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<version>${API_VERSION}</version>
</Package>
EOT
# deploying to the target org -l RunSpecifiedTests
echo "Deploying a destructive change to ${TARGET_ENV}"
echo "In Progress..."
if [ -z "$TEST_CLASS" ]
then
sfdx force:mdapi:deploy -d deployment -u ${TARGET_ENV} -l RunLocalTests -w -1
else
sfdx force:mdapi:deploy -d deployment -u ${TARGET_ENV} -l RunSpecifiedTests -r ${TEST_CLASS} -w -1
fi
#show result
if [ $? -eq 0 ]
then
# green color
echo -en "\e[32m************************** Success **************************"
else
# red color
echo -en "\e[31m************************** Error **************************"
fi
#Clean deployment folder
rm -rf deployment &>/dev/null
echo "************************** END DEPLOYMENT ************************** "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment