Continuous integration using Jenkins

For administrators working with a single company, change sets are a great way to move updates between sandboxes and a parent org. However, when working on the enterprise tier, we often need to transfer components between various test and developer environments, which are outside the scope of change sets.

To support multiple developers working across multiple environments on various features and fixes, a proven path is to use an automated continuous integration system to simplify deployments and promote transparency.

Mirketa_Salesforce_Continuous_integration_using_Jenkins_1

 

 

Continuous integration

The concept behind a continuous integration, or CI, tool is that a constant flow of development changes and unit testing will be done to detect conflicts and errors within the development cycle itself. It allows you to perform a baseline of quality assurance without much reliance on any manual processes (at least until you get that notification that your code just broke the build). The flow is something like:

Prerequisite Software:

  1. Salesforce instance – Source and destination cloud
  2. Eclipse with force.com IDE: This used to pull the desired code and components to be deployed at other Salesforce.com instance.
  3. GitHub repository as source control
  4. Jenkins: It control the deployment
  5. JDK
  6. JRE
  7. Force.com Migration tool
  8. Apache Ant

Installation

1.      Install ANT

1)       Download and install JRE 6: Verify the correct installation by executing the following command: java –version.

2)      Download and Install Ant (version 1.6 or higher) zip file from http://ant.apache.org/bindownload.cgi and set environment variable.

3)       Download Force.com Migration Tool zip folder from Salesforce Org for which the path is as follows: Develop->Tools->Force.com Migration Tools.

4)      Copy the “ant-salesforce.jar” file from the above extracted folder and paste it in the lib directory of the installed Ant folder.

5)      Set the environment Variables as follows:

->System Variables->new> Variable Name: ANT_HOME

Variable Value: C:\Program Files\apache-ant-x.xx.x

->System Variables->new> Variable Name: JAVA_HOME

Variable Value: C:\Program Files\Java\jdk1.8.0

->System Variables->edit->PATH ->In PATH, add %JAVA_HOME%\bin and %ANT_HOME%\bin….. SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;…. ;%JAVA_HOME%\bin;%ANT_HOME%\bin; …..

6)      Verify if ‘C:\Program Files\Java\jdk_x.xx.x\lib’ contains ‘Tools.jar’ file.

7)        Run the following command in cmd to verify the correct installation of ANT: ant -version

Update

sf.src = ../SFDC_CI_DEMO/src:

8)      Update src path in build.xml and build.properties files.

Example: Please note that “SFDC_CI_DEMO” is my project name created in eclipse.

In build.xml set basedir=”../SFDC_CI_DEMO/src” path at first line of code.

In build.properties set “sf.src = ../SFDC_CI_DEMO/src” path at last line of code.

9)       Install Eclipse with Force.com and Egit IDE (EGit IDE comes with eclipse. Check if it their else install GIT from Help -> Install new software

2.      Install GIT 

3.      Install GITHUB (GitHubSetup.exe) – This is required to install because if you are using public GitHub repository then you will have you use this to upload build.xml and build.properties files on cloud.

Note: Identify your GitHub repository and keep credentials and repository path handy. It will be required while pushing code from Eclipse to GitHub.

4.      Install Salesforce.com ANT Migration tool

 

 

 

5.   Install Jenkins
You need to start Jenkins engine if you’ve installed using the .war file. To do this, open command prompt and navigate to the folder where ‘jenkins.war’ file is located. Run the following command:

C:/…>java -jar jenkins.war

 

If you’ve installed using windows installer, there’s no need to start Jenkins engine.

 

To run Jenkins, open your browser and connect to ‘localhost:8080’ Once you see the message “Jenkins is fully up and running” means you are ready to go on.

Now open the browser and enter http://localhost:8080/ . Configure Jenkins and configure Job to start using it.

 

 

Configuration

Configure Jenkins:

Once your Jenkins is up, goto jenkins > manage jenkins > configure system.

·Update the JDK and ANT sections with their respective version and path.

·Install GIT plugin from Manage Jenkin -> Manage plugins-> Available & choose GIT plugin to install.
Configure Job:

Go to http://localhost:8080/ and Click on Create New item (using Build a free-style software project option). Enter Item name (This is your project name example SFDC_Deployment) and save.

·Select GIT from “Source Code Management” section.

·Enter Repository URL (Example https://github.com/ABCDEMO/ REPOSITORY1) and provide GITHUB repository password.

·Goto ‘Build’ section in the configuration page. Select Ant version.

·In the Targets field, enter the library and property file path.

 

Eg:

-lib

“C:\Program Files (x86)\apache-ant-1.9.2\lib\ant-salesforce.jar”

-Propertyfile

“D:\Users\Username\.jenkins\jobs\JenkinsJobName\workspace\CI\build.properties”

·Click on advanced button. In the ‘Build File’ field, enter the the path Ant’s for build.xml

 

D:\Users\ Username \.jenkins\jobs\SFDC_Deployment\workspace\CI\build.xml

 

So here is the trick to get best of out of these build.xml and build.properties files copied from Salesforce ANT tool installation Zip file. Update src path in build.xml and build.properties files.

Example: Please note that “SFDC_CI_DEMO” is my project name created in eclipse.

In build.xml set basedir=”../SFDC_CI_DEMO/src” path at first line of code.

In build.properties  set “sf.src = ../SFDC_CI_DEMO/src” path at last line of code.

After setting these paths, will upload these files to GitHUB repository and then will sync it will local repository using GITHUB tool installed.

To do this you need to login into GITHUB local tool and navigate to your repository. Now go to settings and open in explorer. It open an explorer window. Create a folder named “CI” and then copy paste build.xml and build.properties updated files in this folder. Now check-in using GITHUB tool providing comment.

·In the ‘Properties’ field, enter the salesforce parameters used by Ant in build.xml

 

sf.username = user@xyz.com

sf.password = PasswordSecurityToken

sf.serverurl = login.salesforce.com

sf.checkOnly = false

sf.runAllTests = false

sf.logLevel = none

·       Click Save. Your deployment job is ready to go!

 

We have seen all the technical details of setting up Jenkins that includes configuration of Jenkins and other software requirement.

 

Let’s see how to process this take the advantage of Jenkins. As discussed in beginning, it starts with fetching the code in Eclipse.

 

Let’s start using Eclipse to fetch the code and components locally and to select right code and components for deployment.

 

 

 

 

 

 

 

Using Eclipse to push code to repository

1.       Add a force.com project by giving correct URL and credentials and download the project data    at your local PC.

2.       Now right click on the project name -> team -> share project

3.       Select GIT from pop up window and click next.

4.       Select a path (folder) on your system and click “create repository”. It will create a local copy of the repository. Now finish closing the pop up. Now you are ready with a local copy.

5.       Now it’s time to push your code and deploy sfdc components to cloud repository. (STEP 2 as per image above)

6.       To push your code and components right click on the project name then Team -> Commit. Before starting the deployment, we should know that what we are going to deploy. Let’s say we take Classes, VF pages, Custom objects, Related page layouts, Custom profile, Report and Reports Types and Workflows.

So, select the appropriate files and components to push to repository and specify comment and click “Commit and Push”. It will ask repository URL and authentication details to push the code at correct location. URL should end with git so copy and paste proper URL from repository. Example https://github.com/ABCDEMO/ REPOSITORY1.git

Note: ANT checks package.xml of the project src file to know what all things have to migrate. So on every change org check-in this updated XML should be checked into repository as well.

 

So now your selected code is pushed to repository and Jenkins is already configured. In Jenkins we have not given any time period to start the build process automatically, it will not start the build automatically. We have to build manually as per convenience. To build the build go to Jenkins job & click ‘Build Now’ to run the deployment job.

 

To configure Jenkins auto build the changes pushed in source control at any given time we can configure Jenkins by defining it at “Build Triggers” section in Poll SCM field. If we want that Jenkins should try to build after every 15 min., then we will write H/15 * * * *

 

Regards,

Sameer Tyagi

 

 

Sameer Tyagi

I am a certified Salesforce developer. I have built complex enterprise solutions in the Salesforce ecosystem. I am always sharing what I know, and I love to learn new things on Salesforce. Contact me if you have a Salesforce.com related technical question.
Posted in salesforce administrator.

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *