1

To automate our salesforce development flow, we are using Git + Jenkins + Ant to deploy all committed changes to target organization. It works fine! But the only big problem is that all metadata files will be built and deployed every time. Ant use "Package.xml" as deployment list and this file includes all metadata by default. So "package.xml" should be auto-updated based on git commits and it will only contain the changed files. I guess some shell script is needed, but i really don't know how to do it.

Does anyone have a complete solution for this?

0

After learning some simple shell commands, I wrote some shell scripts to generate package.xml dynamically based on git commit and it works well! Now we can deploy codes only when they are changed!

#!/bin/bash
echo "Shell Start to generate package.xml ..."
metaItems=(`git diff --name-status $GIT_PREVIOUS_COMMIT $GIT_COMMIT | xargs`)
echo "Shell: generate empty deployment folder: codeDeployPkg ..."
pageString=""
componentString=""
clsString=""
triggerString=""
for i in ${!metaItems[@]}
do 
    metaName=${metaItems[i]}
    if [[ ${metaItems[i]} == *".page" ]] && [[ -f "$metaName" ]]; then
        pageName=${metaName#src/pages/}
        pageString=$pageString"<members>"${pageName%.page}"</members>"
        cp -p "$metaName" "codeDeployPkg/pages"
        cp -p "$metaName-meta.xml" "codeDeployPkg/pages"
    elif [[ ${metaItems[i]} == *".component" ]] && [[ -f "$metaName" ]]; then
        componentName=${metaName#src/components/}
        componentString=$componentString"<members>"${componentName%.component}"</members>"
        cp -p "$metaName" "codeDeployPkg/components"
        cp -p "$metaName-meta.xml" "codeDeployPkg/components"
    elif [[ ${metaItems[i]} == *".cls" ]] && [[ -f "$metaName" ]]; then 
        className=${metaName#src/classes/}
        clsString=$clsString"<members>"${className%.cls}"</members>"
        cp -p "$metaName" "codeDeployPkg/classes"
        cp -p "$metaName-meta.xml" "codeDeployPkg/classes"
    elif [[ ${metaItems[i]} == *".trigger" ]] && [[ -f "$metaName" ]]; then
        triggerName=${metaName#src/triggers/}
        triggerString=$triggerString"<members>"${triggerName%.trigger}"</members>"
        cp -p "$metaName" "codeDeployPkg/triggers"
        cp -p "$metaName-meta.xml" "codeDeployPkg/triggers"
    fi
done
if [ "$pageString" != "" ]; then
    pageString="<types>$pageString<name>ApexPage</name></types>"
fi
if [ "$componentString" != "" ]; then 
    componentString="<types>$componentString<name>ApexComponent</name></types>"
fi
if [ "$clsString" != "" ]; then 
    clsString="<types>$clsString<name>ApexClass</name></types>"
fi
if [ "$triggerString" != "" ]; then
    triggerString="<types>"$triggerString"<name>ApexTrigger</name></types>"
fi
packageString="<?xml version=\"1.0\" encoding=\"UTF-8\"?><Package xmlns=\"http://soap.sforce.com/2006/04/metadata\">$pageString$componentString$clsString$triggerString<version>36.0</version></Package>"
echo $packageString > codeDeployPkg/package.xml
echo "Shell: package.xml is ready!"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.