What’s trending
UPCOMING EVENTS
Salesforce DevOps Automation: How to Use Bitbucket Pipelines
By Bassem Marji
In today’s rapidly evolving landscape of Salesforce development, DevOps pipelines represent a cornerstone of modern practices; they bridge the gap between operational stability and rapid application development.
Bitbucket Pipelines are at the heart of this evolution, setting the stage for a more efficient and streamlined development journey.
The Purpose
This article will equip you with adequate knowledge to build a robust Bitbucket pipeline comprising a series of Salesforce maintenance tasks that can be executed manually or on an automated schedule. The presented solution will offer you an array of benefits:
- Effortless Backups to Safeguard Your Salesforce Metadata: Such metadata snapshots generated at specific points in time will grant you the flexibility to revert your metadata to a previous state and to trace back changes performed over time.
- Automatic Extraction of Your Org Audit Trail Records: These records, especially helpful in organizations with multiple system administrators, will provide you great insights into modifications related to administration, security, sharing, data management, and others. Maintaining snapshots of the audit trail records will allow you to proactively monitor every change beyond the 180-day retention limit of the Salesforce audit trail data, which guarantees the adherence of your organization to restrictive compliance requirements.
As we journey through this article, you will gain the know-how to integrate the Bitbucket pipeline into your development arsenal.
The Toolbox
Let’s get started with a brief description of the tools we are going to use:
- Bitbucket Cloud: A Git-based code hosting and collaboration tool designed by Atlassian and optimized for teams using Jira.
- Bitbucket Pipelines: An integrated CI/CD service built into the Bitbucket platform that empowers developers to create automated workflows and configure common actions in a simplified manner. They are especially useful when working with third-party tools.
- Salesforce Client: A powerful, free and open-source command-line tool designed to facilitate development and build automation when working with Salesforce orgs.
The Prerequisites
- Log in to your Bitbucket account and create a private repository to host the project data.
- Collect your org authentication URL. With Salesforce CLI installed in your terminal, connect to your org and extract the authentication URL by executing the following:
$ sf force:auth:web:login --setalias --instanceurl --setdefaultusername
- A prompt to login to the targeted org is then shown on a new browser tab – enter your credentials, and once successfully authorized, close the browser tab.
- Then, run this command:
$ sf org display --target-org --verbose
- Maintain a copy of the authentication URL displayed immediately after: “Sfdx Auth Url”.
- Configure the Bitbucket repository by navigating to the Repository Settings menu on the left sidebar and executing the following steps:
- Select “Settings” and activate “Enable Pipelines”.
- Select “Repository Variables” and define a secured variable named “ORG_AUTH_URL” to store the previously collected authentication URL.
- Select “Settings” and activate “Enable Pipelines”.
With everything set up, let’s embark on our journey…
The Journey
To get you started, here’s an overview of the key components of a Bitbucket pipeline:
- Steps: Define the individual actions to be undertaken.
- Image: Tag a Docker container image to be applied for each step within a pipeline.
- Stage: The logical grouping of one or more pipeline steps.
- Pipelines: define the sequence of steps and the conditions for executing them.
- Schedules: Define a time interval to trigger a pipeline.
- Artifacts: Share files generated between steps like logs, reports and others.
- Caches: Speed up pipeline execution by caching resources between runs.
- Repository variables: Store secrets and environment-specific variables.
Putting all these together, let’s delve into our custom pipeline.
The basic setup of a Bitbucket pipeline requires creating a file called “bitbucket-pipelines.yml” placed at the root of the repository and considered the heart of the pipeline.
Copy the following code snippet into this file and commit the changes:

The Code Breakdown
- We configured the triggering events of our pipeline, which are:
- Scheduled: Based on a cron job that triggers at the beginning of each month.
- Manual: By adding a custom section to the pipeline configuration file.
- We created a list of variables:
- ORG_ALIAS: The alias to be assigned for the organization.
- USER_EMAIL: The email of the Bitbucket user.
- USER_NAME: The name of the Bitbucket user.
- Within the pipeline, we defined two steps:
- Authorizing and Collecting Org Metadata: This job gathers the org metadata, collects the setup audit trail for the past month, and places the results under a folder called “orgMetadata”.
- Pushing Changes to a New Branch: This job creates a new branch within the originating repository and pushes the collected data to it. This ensures continuous visibility across the changes performed.
It is worth noting that metadata retrieval is limited to 10,000 items per operation due to Salesforce Metadata API’s limitations.
The Outcome
Now, let’s initiate our pipeline and visualize its output.
On your repository main page, navigate to the Pipelines sections and run the pipeline “custom: collect_org_metadata” while selecting the “main” Branch:

Following the pipeline execution, you can check its output status and review the time spent and the running logs of each step at a granular level, as shown below:

Now, let’s look into the changes introduced to our repository.
You will notice that a new branch was created in our repository containing our org metadata and an extract of audit trail records for the last month.
Summary
DevOps pipelines are becoming a crucial piece of the puzzle. They stand out as a game-changing tool, paving the ground for smoother, faster, and more established development journeys.
We hope this article helped you explore the world of Bitbucket Pipelines while delving into their intricate details, unparalleled features, and the amazing benefits they bring to the development table.