As your Salesforce Organization undergoes heavy customization and frequent builds, moving changes from one Sandbox to other sandboxes starts taking longer time and effort. Also, in normal Salesforce project, there are chances that you will have minimum three sandboxes likely Developer Sandbox, QA Sandbox and UAT Sandbox. After some time you will be in need of some solution which can reduce your effort.
Almost 5 years back, when I was working on .net along with Salesforce, I posted article on Continuous Integration of ASP.net and subversion as a code management with MSBuild on code project.
This time its Salesforce using Jenkins. In this article I will walk through solution of above problem using Jenkins. Don’t forget to watch Video at end of this article, where I provided demo of everything explained in this article.
Prerequisite Software:
- ANT
- Salesforce Migration Tool
- Git
- Eclipse or PuTTYgen to generate SSH key
I am assuming you already know about below concepts:
- How to use Git with Salesforce
- Using ANT migration tool in Salesforce
- Generating SSH key
- Automated Daily Backup of Salesforce Using ANT Migration Tool and GIT (Optional)
I will suggest to get familiar with first three topics before jumping to Continuous Integration.
There are two ways to start working with Jenkins
- Install Jenkins on your local system
- Use Cloudbees online
Below diagram shows complete flow of Continuous Integration we are going to setup in this article.
For this tutorial, I will install Jenkins on my local system. To install it, Navigate to Jenkins homepage and download installer.
During installation, you may be prompted to install Jenkins as “Windows Service”, click yes for that option. Once installed navigate to “http://localhost:8080”, and you should be able to Jenkins homepage.
Installing Git plugin
By default Jenkins does not provide support for Git, so we will need to install it separately.
To install Git Plugin, Navigate to “Jenkins Homepage | Manage Jenkins | Manage Plugins | Available” and search for Git. From available list, install “Git Plugin”. I would suggest to install “Bitbucket Plugin” as well.
Installing chatter Plugin
Once Git plugin is installed, we will need to install chatter plugin as well from superfell’s github repository. Assuming you already have Maven installed on your system, download above repository as a zip and extract it on your system. After extract, run command “mvn” in that directory. It will create folder by name “target” and plugin file by name “ChatterPlugin.hpi” would be available.
If you don’t want to perform maven build then you can bypass above step and download file from here, however it is always suggested to build file from latest repository code. If you are downloading “ChatterPlugin.hpi” from my website then don’t forget to rename file to “ChatterPlugin.hpi”.
Once you have “ChatterPlugin.hpi” file on your system, you need to install it into Jenkins. Navigate to “Jenkins Homepage | Manage Jenkins | Manage Plugins | Advanced | Upload Plugin”. Select “ChatterPlugin.hpi” and upload it.
Salesforce migration ANT script
Now, we need to create “build.xml” to retrieve changes from one Org and Deploy to other Org. Assuming, you already know how to use ANT Migration tool I will directly show code snippet of my “build.xml” file.
<project name="Code Backup Without Folders" default="retrieve" basedir="." xmlns:sf="antlib:com.salesforce"> <property file="build.properties"/> <property environment="env"/> <target name="Deploy"> <sf:deploy username="${Sandbox2.username}" password="${Sandbox2.password}" serverurl="${sfSandbox.serverurl}" deployRoot="${Sandbox2.retrieveTarget}" runAllTests="true" /> </target> <target name="retrieve"> <sf:retrieve username="${Sandbox1.username}" password="${Sandbox1.password}" serverurl="${sfSandbox.serverurl}" retrieveTarget="${Sandbox1.retrieveTarget}" unpackaged="${Sandbox1.unpackaged}"/> <echo message="Commiting all changes with message ${gt.commitMessage}" /> <git command="add" dir="${Sandbox1.gitDirectory}"> <args> <arg value="." /> </args> </git> <git command="commit" dir="${Sandbox1.gitDirectory}"> <args> <arg value="-am ${gt.commitMessage}" /> </args> </git> </target> <macrodef name="git"> <attribute name="command" /> <attribute name="dir" /> <element name="args" optional="true" /> <sequential> <echo message="Executing Command in folder @{dir}" /> <echo message="git @{command}" /> <exec executable="git" dir="@{dir}"> <arg value="@{command}" /> <args/> </exec> </sequential> </macrodef> </project>
Username, Passwords and other values are coming from “build.properties” file. As you can see in above code, Changes are fetched from “Sandbox1” and deployed to “Sandbox2”.
Setting-up Bitbucket account
If you are following my blog, you may already know that I give preference to Bitbucket over Github because it allows me to setup private repository free of cost.
First you will need to generate SSH key for authentication, for this you can either use Eclipse or PuTTYGen. In Video demo of this article, i have used PuTTYGen. Once SSH key is generated, upload Public key in Bitbucket by navigating to “Manage Account | Securtiy | SSH Keys | Add Key”. Save Private key with extension “ppk” somewhere on your local system.
You will need to commit all your changes of “Sandbox1” to this Bitbucket account so that it can be deployed to “Sandbox2”. For this you can either use Eclipse or TortoiseGit.
Create new project in Jenkins
Navigate to “Home page | New Item”. Give appropriate item name and then choose “Freestyle project”.
In “Source Code Management”, choose “Git”. In Repository URL, provide repository URL of Bitbucket for SSH protocol. In Credentials, click on Add, choose “SSH Username with private key”. Provide username and paste private key saved in “ppk” file. If there is any Passphrase then provide it and save it.
Build Triggers
There may be any combination which will cause Jenkins to trigger build. For example if code is pushed to your Git repository, or poll Git repository after some predefined interval to see if there is any change or run build periodically. You can have one or more than one combination to trigger builds. In this article I have selected “Poll SCM” option. It will check my git repository after every 15 minutes for changes and if it finds that repository has been changed then it will start building. If you select “Pol SCM”, then text box will appear where we need to provide crone statement to decide its scheduled time. I have provided “H/15 * * * *” which means, it will run every 15 minutes.
Build
In this section choose “Invoke Ant”. In Targets, provide Ant target names separated by space. provide location of folder where “build.xml” is present in text box “Build File”.
Post-build Actions
In this section of project configuration, we need to setup task that can be executed after build. There are number of options available, however we will choose “Chatter results”. This task will be available only if you have installed “Chatter plugin” as explained above. Here, we need to provide Username and Password to connect to Salesforce. If you want to connect to Sandbox or My Domain then need to setup value in text box “Override Login Server” else keep it blank.
After all above settings, save this information and we will be good to go. You can see log of attempt by Jenkins to determine if there is any change in git repository, it would be available at left menu by name “Git Polling Log”.
We can also manually trigger build by clicking on “Build Now” link visible in above image. All build results will be available below “Git Polling Log”.
Video Tutorial
Please share your feedback in comments section.
Leave a Reply