This brief post illustrates how Ant scripts can be used in a continuous integration scenario, i.e. where metadata is held in a source code control (SCC) repository such as Subversion. In a CI scenario developers would typically be working in isolated developer orgs with periodic commits of unit tested code to SCC following peer-review (hopefully). The act of committing changes to SCC will trigger a full deployment of the metadata state held in SCC to a dedicated integration org – with full execution of unit tests and hopefully automated acceptance tests (i.e. Selenium or similar). The whole point of this process is to introduce rigour into the development process around code commits and to ensure build errors are captured whilst the developer is in the moment and can remedy the problem quickly. CI is an agile practice related to technical excellence.
Ok, enough theory – the example (simplistic) script below shows a common case where the head revision is checked-out to a local folder and deployed to a Salesforce org – with unit tests running. In practice this script would be automated by a Hudson or Jenkins job that would be monitoring the SCC repository for commit operations.
Build Properties [build.properties]
[sourcecode language=”xml”]
# build.properties
# Contains properties referenced by all deployment scripts
# May be replaced by configuration parameters when invoked from a Hudson/Jenkins Job.
# local root folder.
metadata.root=metadata
# Salesforce task configuration properties.
sf.target.org.serverurl=https://test.salesforce.com
sf.target.org.username=mark@force365.com
sf.target.org.password=welcome2U
sf.target.org.forcetests=true
sf.target.org.checkonly=false
sf.target.org.deploy.maxPoll=20
sf.target.org.logType=Debugonly
sf.target.org.deploy.waiting.time=500000
# SvnAnt task configuration properties.
svnant.latest.url=svn://localhost/myproject
svnant.repository.user=mark
svnant.repository.passwd=welcome2U
[/sourcecode]
Build File – Retrieve Metadata from SCC and Deploy to Org [build.xml]
[sourcecode language=”xml”]
<project name="Subversion to Org" default="deploy" basedir="." xmlns:sf="antlib:com.salesforce">
<property file="build.properties" />
<property environment="env" />
<!– path to the svnant libraries. Usually they will be located in ANT_HOME/lib –>
<path id="svnant.classpath">
<fileset dir="${ant.home}\lib">
<include name="**/svn*.jar"/>
</fileset>
</path>
<!– load the svn task –>
<typedef resource="org/tigris/subversion/svnant/svnantlib.xml" classpathref="svnant.classpath" />
<target name="checkoutLatest">
<svn username="${svnant.repository.user}" password="${svnant.repository.passwd}">
<checkout url="${svnant.latest.url}"
revision="HEAD"
destPath="${metadata.root}" />
</svn>
</target>
<target name="deploy" depends="checkoutLatest">
<echo message="deploying from ${metadata.root}" />
<sf:deploy username="${sf.org.username}"
password="${sf.org.password}"
serverurl="${sf.org.serverurl}"
deployroot="${metadata.root}"
singlePackage="true"
runAllTests="${sf.org.forcetests}" />
</target>
</project>
[/sourcecode]
The script above can be executed manually from a standard (non-Force.com) project within Eclipse. I typically run with an Eclipse workspace per-client and maintain a deployment project within the workspace for all the scripts I use.
Pre-requisites are the Force.com Migration Tool and svnAnt task being added to the Ant classpath. Install instructions linked below.