Skip to content

Azure DevOps

Create Pipelines

You need to create two pipelines in Azure DevOps, using the YAML files provided in your project.

Pipeline 1: Check Pull Request

This pipeline will run validation checks on every Pull Request.

  • Go to Pipelines
  • Select New pipeline
  • Select Azure Repos Git
  • Select your repository
  • Select Existing Azure Pipelines YAML file
  • Select path azure-pipelines-checks.yml (select your work git branch if needed)
  • Click on Continue
  • Click on the arrow next to "Run" and select Save
  • Rename the pipeline to Check Pull Request (click on the name to edit)

Pipeline 2: Deploy to org

This pipeline will deploy changes to the Salesforce org when a Pull Request is merged.

  • Go to Pipelines
  • Select New pipeline
  • Select Azure Repos Git
  • Select your repository
  • Select Existing Azure Pipelines YAML file
  • Select path azure-pipelines-deployment.yml (select your work git branch if needed)
  • Click on Continue
  • Click on the arrow next to "Run" and select Save
  • Rename the pipeline to Deploy to org (click on the name to edit)

Configure Triggers and Policies

Check Pull Request Pipeline

By default, Azure Pipelines might trigger on every commit to any branch. We want to restrict it so that it only runs for Pull Requests.

  1. Disable Continuous Integration trigger:

    • Go to Pipelines -> Select "Check Pull Request" pipeline -> Edit
    • Open the menu ⋮ (Triggers)
    • Tick Override the YAML continuous integration trigger from here
    • Select Disable continuous integration
    • Save
  2. Configure Branch Policy:

    • Go to Repos -> Branches
    • Click on the ... menu next to your major branches (e.g., integration, master, ...)
    • Select Branch policies
    • In Build Validation, click +
    • Select your "Check Pull Request" pipeline
    • Keep default settings and Save
    • (Optional but recommended) In Branch policies, enable Require a minimum number of reviewers and Check for linked work items if you want to enforce best practices.

Deploy to org Pipeline

This pipeline should run automatically when changes are pushed (merged) to major branches.

  1. Configure Branch Filters:
    • Go to Pipelines -> Select "Deploy to org" pipeline -> Edit
    • Open the menu ⋮ (Triggers)
    • In Branch filters, tick Override the YAML continuous integration trigger from here
    • Make sure Enable continuous integration is checked
    • Include all your major branches (e.g., integration, uat, production...)
    • Save

One job at a time per org (optional)

Salesforce runs one deployment at a time in an org. With this setting, the deployments of a major branch wait for each other in the CI queue, one at a time and in order, instead of waiting in the org while their job runs. More details in One job at a time per org.

It is off by default. On Azure, the lock is an Exclusive lock check on a variable group:

  1. In Pipelines -> Library, create one variable group per major branch, named deploy-<branch> (deploy-integration, deploy-uat...). Use the last part of the branch name: deploy-1.0 for release/1.0.
  2. Add one secret variable to each variable group, with any name and value. Azure applies checks to the secret variables of a variable group only.
  3. Open the menu ⋮ of each variable group, select Approvals and checks, and add an Exclusive lock check.
  4. In azure-pipelines-deployment.yml, uncomment lockBehavior: sequential and the group line of the variables. Without sequential, Azure only gives the lock to the latest waiting run, and the other waiting runs do not deploy their merge.

Microsoft documents the Exclusive lock between the runs of one pipeline. The deployment checks of Pull Requests and the major branch deployment checks run in other pipelines: they still wait in the org. The "Check Pull Request" pipeline could not take the lock anyway: the target branch of a Pull Request is not known yet when Azure reads the file, and the name of a variable group must be.

If your pipeline files were created with an older version of sfdx-hardis, add the lines below to them.

azure-pipelines-deployment.yml, at the top level of the file, and at the end of its variables list:

lockBehavior: sequential

variables:
  # ... the variables of the file
  - group: deploy-${{ variables['Build.SourceBranchName'] }}

Azure Pull Request notes

To avoid having to open job logs to see deployment errors, sfdx-hardis can post them as a thread on the Pull Request UI.

Global configuration

To use this capability:

  • You must allow Contribute and Contribute to Pull Requests on your Build Service (Settings -> Repositories -> Select your build service)

Screenshot

Tech Work Item

If you want to use Flow Diff (visual diff of flow differences):

  • Create an Azure Boards work item named exactly sfdx-hardis tech attachments. It will be used to link the uploaded images.

    • If you prefer to name it differently, define the variable AZURE_ATTACHMENTS_WORK_ITEM_ID with the number of your work item as value.
  • An Azure work item can only have 100 attached images, so regularly delete old image attachments, or delete the work item and recreate it with the same name.

Screenshot

Examples

Every time you create a Pull Request, the CI job posts its result as a comment.

  • Example with deployment errors

  • Example with failing test classes

  • Example with deployment success

Notes:

  • This integration works with the sfdx-hardis pipeline, but also with home-made pipelines: just call sf hardis:project:deploy:start instead of sf project:deploy:start.
  • This integration uses the following variables:
    • SYSTEM_ACCESSTOKEN: $(System.AccessToken)
    • CI_SFDX_HARDIS_AZURE_TOKEN: $(System.AccessToken)
    • SYSTEM_COLLECTIONURI: $(System.CollectionUri)
    • SYSTEM_JOB_DISPLAY_NAME: $(System.JobDisplayName)
    • SYSTEM_JOB_ID: $(System.JobId)
    • SYSTEM_PULLREQUEST_PULLREQUESTID: $(System.PullRequest.PullRequestId)
    • SYSTEM_TEAMPROJECT: $(System.TeamProject)
    • BUILD_BUILD_ID: $(Build.BuildId)
    • BUILD_REPOSITORY_ID: $(Build.Repository.ID)
    • AZURE_ATTACHMENTS_WORK_ITEM_ID (optional: identifier of the work item used to attach images)

Using Azure DevOps integration from Jenkins

When running on Jenkins, sfdx-hardis automatically detects the Jenkins environment and maps its variables to Azure DevOps equivalents. You only need to set:

Variable Description
CI_SFDX_HARDIS_AZURE_TOKEN An Azure DevOps PAT with Code Read & Write and Pull Request Threads Read & Write, stored as a Jenkins credential

The following variables are automatically derived from Jenkins built-in variables:

  • SYSTEM_COLLECTIONURI, SYSTEM_TEAMPROJECT, BUILD_REPOSITORY_ID: parsed from GIT_URL (git remote)
  • BUILD_REPOSITORYNAME: from BUILD_REPOSITORY_ID
  • BUILD_SOURCEBRANCHNAME: from GIT_BRANCH / CHANGE_BRANCH
  • BUILD_BUILDID, BUILD_BUILD_ID: from BUILD_NUMBER
  • SYSTEM_JOB_DISPLAY_NAME: from JOB_NAME
  • SYSTEM_JOB_ID: from BUILD_NUMBER
  • SYSTEM_PULLREQUEST_PULLREQUESTID: from CHANGE_ID (Jenkins Multibranch Pipeline)
  • Job URL: from BUILD_URL

Instructions for using Coding Agents

When using auto-fix with coding agents, the pipeline must be able to push a fix branch and create/update Pull Requests.

This works for both:

  • Azure DevOps Services (cloud)
  • Azure DevOps Server (on-premise)

Add this in your deployment/check script step before running sf hardis:* commands:

if [ -n "${SYSTEM_ACCESSTOKEN:-}" ]; then
   git config user.email "sfdx-hardis-bot@cloudity.com"
   git config user.name "sfdx-hardis Bot"
   echo "[sfdx-hardis] Azure push/PR auth enabled for coding agents via SYSTEM_ACCESSTOKEN"
else
   echo "[sfdx-hardis] Skipping coding-agent Azure auth setup: SYSTEM_ACCESSTOKEN is not set"
fi

Required secret/variable:

  • SYSTEM_ACCESSTOKEN (or CI_SFDX_HARDIS_AZURE_TOKEN):
    • In pipeline settings, enable Allow scripts to access OAuth token.
    • Alternative: create an Azure DevOps PAT with Code Read & Write and Pull Request Threads Read & Write, then store it as a secret variable and map it to CI_SFDX_HARDIS_AZURE_TOKEN.

Coding agents on a workstation

A coding agent that runs sfdx-hardis commands with --agent on a developer machine does not need an Azure DevOps Personal Access Token in a .env file.

When no Azure DevOps token variable is set, or when Azure DevOps refuses the one that is set, sfdx-hardis uses a Microsoft Entra ID token of the Azure CLI the developer is already logged in with, for the running command only. Nothing is written anywhere, and the token never appears in a log.

az login
sf hardis:project:connection:check --type git --agent --json

The variables of the environment come first, then the .env file, then az. The token lasts about one hour and belongs to the Microsoft Entra tenant az is logged in to: when the organization is in another tenant, run az login --tenant <tenant-id>. When Azure DevOps refuses the token, sfdx-hardis names the tenant of the organization.

An organization that uses Microsoft accounts and is not connected to Microsoft Entra ID accepts no az token: keep a Personal Access Token in CI_SFDX_HARDIS_AZURE_TOKEN for it.

See hardis:project:connection:check and the variables SFDX_HARDIS_PROVIDER_CLI_AUTH and SFDX_HARDIS_PROVIDER_TOKEN_CHECK in All environment variables.

Comments