CI/CD using Bitbucket Pipeline for Salesforce — Part 2

mahesh chouhan
5 min readJun 2, 2020

--

Before moving forward, let’s review the tools we will be using:

Docker

Docker provide virtual environments similar to our local environment.Whenever we create a pipeline, it always run on an virtual environment which is defined by a docker image. You can create your own docker image or just use the one, I got from one of the Salesforce Demos.If you look at the docker image, we will notice that It adds all the tools required for our pipeline like java and apache ANT.

Force.com Migration Tool

It is a very powerful tool to retrieve and deploy metadata from Salesforce orgs.We can also run Apex Tests using Ant Migration Tool. Install it from this link. It comes with an build.xml where all the Ant Targets are defined. Apache ANT itself is a widely using Automation tool.

Bitbucket Pipelines

Bitbucket Pipelines is an integrated CI/CD service, built into Bitbucket. It allows you to automatically build, test and even deploy your code, based on a configuration file in your repository. To create pipeline, we need to configure bitbucket-pipelines.yml in the root of the repository.

  • Navigate to our Dev Repository and click on Pipelines on the left panel then click Docker and commit file. This will commit a sample pipeline file. You can see that a build has started in pipeline section but it will fail. We will fix this later.
  • Click on Branches in left pane. Click on Create Branch at the top right.
  • Specify Branch Name as feature/pipeline-setup, leave other fields as it is and click Create button. The name of the branch is with respect to git flow which I will cover in some other post.
  • Click branch locally using Source Tree or Git CLI. I am more of a CLI guy.
  • Open bitbucket-pipelines.yml using any text editor and paster following content into this file.

image:
name: mklinski/salesforce
pipelines:
branches:
feature/*:
— step:
script:
— ant -buildfile build/build.xml checkDeploy -Dsfdc.username=$SFDC_USERNAME -Dsfdc.password=$SFDC_PASS$SFDC_TOKEN -Dsfdc.serverurl=https://$SFDC_SERVERURL
master:
— step:
script:
— ant -buildfile build/build.xml deployCode -Dsfdc.username=$SFDC_USERNAME -Dsfdc.password=$SFDC_PASS$SFDC_TOKEN -Dsfdc.serverurl=https://$SFDC_SERVERURL

  • The image tag specifies the docker image which will be used for running pipeline. Docker provides a virtual environment where we will execute our commands.
  • You can see from the above code that we are running two types of ant commands. For feature types of branches, we are running checkDeploy and master branch, we are running deployCode. These commands are defined by Force.com Ant Migration Tool.
  • Although we have used the build.xml in our bitbucket-pipelines.yml file, we have not actually committed our build.xml. Create a build folder and add build.xml and paste following code.

<project name=”Deploy SFDC metadata” default=”deployEmptyCheckOnly” basedir=”..” xmlns:sf=”antlib:com.salesforce”>

<taskdef uri=”antlib:com.salesforce”
resource=”com/salesforce/antlib.xml”
classpath=”${basedir}/build/lib/ant-salesforce.jar”/>

<property file=”${basedir}/build/build.properties”/>
<property environment=”env”/>

<target name=”deployCode”>
<echo level=”info”>Performing the deploy</echo>
<sf:deploy
username=”${sfdc.username}”
password=”${sfdc.password}”
serverurl=”${sfdc.serverurl}”
deployRoot=”${basedir}/src”
pollWaitMillis=”${sfdc.pollWaitMillis}”
maxPoll=”${sfdc.maxPoll}”/>
</target>

<target name=”checkDeploy”>
<echo level=”info”>Testing the deploy</echo>
<sf:deploy
checkOnly=”true”
logType=”Debugonly”
username=”${sfdc.username}”
password=”${sfdc.password}”
serverurl=”${sfdc.serverurl}”
deployRoot=”${basedir}/src”
pollWaitMillis=”${sfdc.pollWaitMillis}”
maxPoll=”${sfdc.maxPoll}”
testLevel=”RunLocalTests”>
</sf:deploy>
</target>
</project>

  • Create build.properties and paste following code.

sfdc.maxPoll = 100
sfdc.pollWaitMillis = 100000

  • Create a new folder lib inside build and paste ant-salesforce.jar there that you must have received on downloading Foce.com Migration Tool.
  • Now it’s time to add the actual Salesforce Metadata that we want to deploy. Create an src folder and the src folder from this repository.I have written a class for Stack Implementation and test class for that.
  • Our repository should look like this.
  • Now, just commit the changes and push it to remote repository.It will trigger a new build, which will fail with this error Attribute ‘username’ cannot be an empty string It occurred because we have not defined the $SFDC_USERNAME variables that we are using in our pipeline file. These variables need to be defined at Repository level.
  • Navigate to Dev Repository and Click on Repository Settings and then Repository Variables.
  • Add SFDC_SERVERURL, SFDC_TOKEN, SFDC_PASS and SFDC_USERNAME variables and populate their values.Since this is Dev Repository, I am going to connect it to Dev Sandbox.
  • Navigate to Pipeline page and click on Rerun button on top. Your Build will pass successfully.
  • You can also verify the same by loggin into your dev sandbox and checking deployment status in Quick Find box.
  • As our build, we can go ahead and merge this feature branch into master. Since we need to promote our changes to UAT, we need to merge into master branch of UAT Repository but first, we need to define repository variables on UAT repository.
  • Now create a pull request to merge feature branch from Dev to master branch of UAT.
  • Assign any reviewer if you want and then merge the pull request. After that, we can one build start in pipeline section but this time actual code will be deployed to UAT and not just deployment check.
  • Congratulations ! We have successfully promoted our code to UAT. Similarly after testing is done, we can create another pull request from feature branch of dev to master of production to deploy our code to Salesforce Production Environment.

--

--

mahesh chouhan

Salesforce Developer || 3 x Salesforce Certified || 1 x Copado Certified || Trailhead Ranger || Artist