Continuous integration in Salesforce Using Jenkins and Git | Movie Tutorial
As your Salesforce Organization undergoes powerful customization and frequent builds, moving switches 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.
This time its Salesforce using Jenkins. In this article I will walk through solution of above problem using Jenkins. Don’t leave behind to see Movie at end of this article, where I provided demo of everything explained in this article.
I am assuming you already know about below concepts:
I will suggest to get familiar with very first three topics before leaping to Continuous Integration.
There are two ways to commence working with Jenkins
Below diagram shows accomplish flow of Continuous Integration we are going to setup in this article.
Continuous integration in Salesforce Using Jenkins and Git (Bit Bucket)
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.
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 directive “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 leave behind 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 switches from one Org and Deploy to other Org. Assuming, you already know how to use ANT Migration instrument I will directly demonstrate code snippet of my “build.xml” file.
Username, Passwords and other values are coming from “build.properties” file. As you can see in above code, Switches 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 permits me to setup private repository free of cost.
Very first you will need to generate SSH key for authentication, for this you can either use Eclipse or PuTTYGen. In Movie 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 switches of “Sandbox1” to this Bitbucket account so that it can be deployed to “Sandbox2”. For this you can either use Eclipse or TortoiseGit.
Create fresh project in Jenkins
Navigate to “Home page | Fresh Item”. Give adequate item name and then choose “Freestyle project”.
Create Fresh Item in Jenkins – Free Style 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.
Jenkins SSH Username with private key Credentials
There may be any combination which will cause Jenkins to trigger build. For example if code is shoved to your Git repository, or poll Git repository after some predefined interval to see if there is any switch 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 fifteen minutes for switches and if it finds that repository has been switched then it will embark building. If you select “Pol SCM”, then text box will show up where we need to provide crone statement to determine its scheduled time. I have provided “H/15 * * * *” which means, it will run every fifteen minutes.
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”.
Jenkins build – Invoke Ant task
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.
Jenkins Post build Act – Post to Salesforce Chatter
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 switch in git repository, it would be available at left menu by name “Git Polling Log”.
Salesforce – Jenkins Git Polling Log
We can also by hand trigger build by clicking on “Build Now” link visible in above pic. All build results will be available below “Git Polling Log”.
Please share your feedback in comments section.