Continuous integration in Salesforce Using Jenkins and Git | Video Tutorial

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:

  1. ANT
  2. Salesforce Migration Tool
  3. Git
  4. Eclipse or PuTTYgen to generate SSH key

I am assuming you already know about below concepts:

  1. How to use Git with Salesforce
  2. Using ANT migration tool in Salesforce
  3. Generating SSH key
  4. 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

  1. Install Jenkins on your local system
  2. Use Cloudbees online

Below diagram shows complete flow of Continuous Integration we are going to setup in this article.

Continuous integration in Salesforce Using Jenkins and Git
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.

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”.

Create New Item in Jenkins - Free Style Project
Create New 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
Jenkins SSH Username with private key Credentials

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”.

Jenkins build Invoke Ant task
Jenkins build – Invoke Ant task

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.

Jenkins Post build Action - Post to Salesforce Chatter
Jenkins Post build Action – 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 change in git repository, it would be available at left menu by name “Git Polling Log”.

Salesforce - Jenkins Git Polling Log
Salesforce – Jenkins 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.

Related posts

  • Sudipta Deb

    Hi Jitenda, I am getting the below error while connecting to my bitbucket by ssh.

    Failed to connect to repository : Error performing command: /usr/local/git ls-remote -h git@bitbucket.org:suddeb/sudipta-personal.git HEAD

    Can you please help me to understand the problem

  • Nitin

    Hi Jitendra,

    Thanks for the great post on such an important topic.

    However I am facing an issue with these steps.

    I am trying to run it via the command prompt itself first without using jenkins.

    I am able to run the build.xml properly however I am getting a message after running the ‘retrieve’ target which says ‘nothing to commit’. However the build runs successfully without any issues.(I am able to retrieve the contents successfully from the source org)

    I am not sure how to configure/pass (to git) the path of the folder where I am retrieving the contents from my source org. I have created a folder which I have configured as a git repository which I am passing as a parameter to Sandbox1.gitDirectory in your build.xml.

    Thanks,
    Nitin.

    • Hi Nitin,
      In CI, first you have to retrieve metadata from Salesforce Org and then commit to GIT.
      in your case, when you are retrieving from your org, No metadata information is changed and that why you are getting message saying “Nothing to commit”.

  • Ankit Kumar

    Hi Jitendra,

    Many thanks for the post.
    However, i am getting the below error when running my build. It seems Jenkins is not able to clone the remote Git repository,

    ERROR: Error cloning remote repo ‘origin’

    Waiting for your response.

    Thanks,
    Ankit Kumar

  • Edgar Yucel Moran

    Hi it is very helpful, I just have a question, there is a way just for take what has changed and not the entire package.xml that include everything, I mean if I just add a field and salesforce for example, and then I push a commit with this change, its possible just take that change and deploy it? Thanks,

    • Hey Edgar, Thats something very advance and cannot done by Jenkins. However wait for my next post on solution of this, 1 or 2 day 🙂

      • Praveen

        Hi Jitendra,

        Thank you for the wonderful post.
        Is there any other way we can achieve what Edgar was asking for?

        • Yes, I have developed a solution for that and will post here very soon.

          • praveen

            Thank you.

            I will wait for your post.

          • saurabh

            can you help me with the above solution?

    • Paul Kelly

      Hi Edgar, did you find a solution for this issue. I think the problem is that you dont want to deploy all the contents of the repo, just the changes which would require using git diff, moving all the files, building a package.xml and deploying it to an org using ant.

      • Edgar Yucel Moran

        that make sense, I didn’t finish to implement this, there are many manual actions, so yes, diff would be the option to generate the package xml and I was thinking in metadata api to complement the process. Thanks,

  • Navor Nuñez Peredo

    Great post…. JIC now that JenkinsChatter plugin is able to publish the code coverage percent also.
    E.G.

    Build: SForce-CI-Project-job 2 is SUCCESS
    Coverage Result: 76.91% of code coverage, Risk status.
    Coverage Status: Danger (0% – 74%): 298 files. Risk (75% – 79%): 84 files. Acceptable (80% – 94%): 267 files. Safe (95% – 100%): 277 files.
    Test Result: 0 tests failing out of a total of 3,276 tests.

  • In this demo, your source is Salesforce Sandbox, your destination is another sandbox. How are you tagging commits to specific branch in Github. If you can explain, end-to-end flow in video, could boot it more. Induce a change in org, show the commit in github, then show the change in second org after successful deploy

  • Also, I am inquisitive to know, how about committing data in github after approval of pull request can trigger a deployment to destinations org(s).

  • Santhosh

    How do you generate package.xml for continuous integration?

  • Gokul

    I am getting login to fail error while trying to retrieve the package from developer org using jenkins

    The same set of file works perfectly using command line.Can you help me here?

    • You are not able to make internet connection. Are you using some proxy, if yes then proxy settings needs to be done in ANT.

      • Gokul

        I am not using any proxy.But if tried to retrieve the files using the command line its working fine.

  • João Felipe Cereja de Paula

    Hi Jitendrazza, thanks for sharing knowledge. I tried to PoC the retrieve and deploy concept but I failed. I got those erros messages below:
    1. The flows will only deploy in deactivated mode in the target org by default. They have to be manually activated in the target org after deployment.
    2. objectTranslations/Case-pt_BR.objectTranslation — Error: insufficient access rights on cross-reference id
    What is the best way to solve this issue? I’m trying to set a CI machine with the less human interation possible. Could you help me? Regards, Felipe from Brazil

    • Once you retrieve metadats from Salesforce, you will need to perform some extra steps. You can create custom “target” in ANT to perform text replacement in files. Example – to deactivate flow while deployment, you can replace contenet in XML using replace task – https://ant.apache.org/manual/Tasks/replace.html

      • Joao Felipe Cereja

        Thanks for the response, Jitendrazza! I will apply some replacement rules and test it again. I’m implementing Continuous integration on the company who I’m in governance area. We have a complex parallel development context and it should solve almost all issues we have. If you have some known issues please share with me. Regards.

      • Joao Felipe Cereja

        Hey Jitendra. I’m back again. I need help to design the process about using repository with salesforce. I’ve built all the ant jobs, I tried to do with deployRoot and zipfile(using ant task to zip the workspace) deploy but didn’t work. My PoC consists in develop in a Sandbox A commit to a branch( with jenkins retrieve and commit to bitbucket job), merge with master and then deploy to Sandbox SIT ( with jenkins checkout and deploy job). When I try to deploy the check out from the master with a delimited package.xml I got many errors about the files are not specified in package.xml. But I need to deploy only what is wrote in this manifest, not all workspace files. Can you brief me a process to fix that? Best Regards, Felipe from Brazil

        • Joao Felipe Cereja

          @JitendraZaa:disqus

  • Nitish Dharm

    Hi Jitendra,

    If there are multiple Developer sandboxes , then how the merging will happen on repository? For example same apex trigger is modified in 2 different orgs. Will the merging will automated or we have to do it manual?

    Thanks,
    Nitish