Skip to content

3. Initialize the SFDX project

Initialize the SFDX project

  • Create a new git branch named cicd under your lowest major branch (usually integration)
  • Run the command Configuration -> Create new sfdx project (sf hardis:project:create) and select the options to create a new sfdx-hardis project

Use the sfdx-hardis command to create the new project, not the default Salesforce one.

  • Open the file manifest/package.xml and replace its content with the following code
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<version>65.0</version> <!-- use the current Salesforce API version -->
</Package>
  • If you use GitLab CI and sandboxes only (no scratch orgs), open gitlab-ci-config.yml at the root of the repository and set the variable USE_SCRATCH_ORGS to "false"
  • With GitHub, Azure, Bitbucket or Jenkins, the scratch org jobs (create a scratch org or take one from the scratch org pool, run the Apex tests on it, delete it) are off by default: set the variable USE_SCRATCH_ORGS to "true" to run them. To keep scratch orgs ready in a pool, see Set up a scratch org pool.
  • Depending on your Git provider, keep the related workflow files and delete the others

    • GitLab
      • gitlab-ci.yml
      • gitlab-ci-config.yml
    • Azure
      • azure-pipelines-checks.yml
      • azure-pipelines-deployment.yml
      • azure-pipelines-scratch-pool.yml (only if you use a scratch org pool)
      • azure-pipelines-major-branch-check.yml (only if you use the major branch deployment check)
    • GitHub
      • Folder .github/workflows
    • Bitbucket
      • bitbucket-pipelines.yml
  • During the CI/CD setup, find the variable SFDX_DISABLE_FLOW_DIFF in the pipeline of your Git provider and set its value to true, to avoid generating too many Pull Request comments during the setup. At the end of the setup, set the variable back to false.

Some workflow files contain additional configuration instructions: read the comments at the beginning of the files.

Upgrade the CI/CD files

The CI/CD files are copied once, when the project is created: a new version of sfdx-hardis does not update them. Each one starts with the URL of the sfdx-hardis template it comes from, for example:

# Source of this file: the sfdx-hardis default template
#   https://github.com/hardisgroupcom/sfdx-hardis/blob/main/defaults/ci/.gitlab-ci.yml

To upgrade a file, by hand or with a coding agent, compare it with the latest version of its template and bring in the changes, keeping what is specific to your project: branch names, secrets, runner tags, image, custom jobs. .gitlab-ci-config.yml holds the values of your project: only add its new keys. The changelog says what changed between versions.

Cancelled pipelines

When a new commit is pushed on a Pull Request, the Git provider cancels the previous pipeline. Cancelling it does not stop its deployment simulation in the Salesforce org: Salesforce keeps it running, and the next simulations on the same org wait for it.

The GitLab, GitHub, Azure and Jenkins workflow files call hardis:project:deploy:cancel when a deployment simulation job is cancelled, so the simulation is cancelled in the org too. Only the simulation of the cancelled job is cancelled, never the one of another Pull Request, and never a real deployment.

This holds when each CI job runs in its own container, as in the default templates. When several jobs run at the same time on the same host without a container (GitLab shell executor, self-hosted GitHub or Azure runners) and share the same home folder, a cancelled job can cancel the simulation of another Pull Request.

Git provider Cancellation hook Requirement
GitLab after_script of a job that did not succeed GitLab 17.0 or later, GitLab Runner 16.11 or later
GitHub step with if: cancelled() -
Azure step with condition: canceled() -
Jenkins post { unsuccessful } of the Validation stage Pipeline: Job plugin 2.42 or later (abortPrevious)
Bitbucket Not available -

Bitbucket runs no script when a step is stopped or times out (after-script only runs after a success or a failure), and a final step starts in a new container that cannot see the job id recorded by the stopped step. A simulation of a stopped Bitbucket step keeps running in the org until it ends.

On Jenkins, a new commit on a Pull Request aborts the build of the previous commit (disableConcurrentBuilds(abortPrevious: env.CHANGE_ID != null)), while the builds of the other branches wait for each other.

On GitLab, a new commit only cancels the previous pipeline while none of its non-interruptible jobs has started. To cancel its interruptible jobs in any case, uncomment the workflow: auto_cancel: on_new_commit: interruptible block at the beginning of .gitlab-ci.yml (GitLab 16.10 or later).

On GitLab, a job that reaches its timeout runs no after_script. The template stops the script 5 minutes before (RUNNER_SCRIPT_TIMEOUT: 145m for a job timeout of 150 minutes), so the hook can still cancel the simulation. If you raise the timeout of a job, raise RUNNER_SCRIPT_TIMEOUT too, or the job stops at 145 minutes.

Real deployments, after a merge, are not cancelled by the next merge. On GitLab, GitHub and Jenkins they run one after the other. On Azure and Bitbucket, two merges close together can deploy at the same time unless you apply the settings below.

With Quick Deploy or delta deployments after merge, every merge must run its own deployment: the next one only deploys its own Pull Request, so the metadata of a skipped merge may never reach the org.

GitLab: the deployment checks and deploy_to_org jobs share a resource group for each target branch. Only one simulation or deployment can use that branch's org at a time. GitLab runs waiting jobs in any order by default, so an older merge could be deployed after a newer one. Set the resource group to run them in pipeline order, once per major branch (integration, uat, preprod, main...):

curl --request PUT --header "PRIVATE-TOKEN: <your_access_token>" \
  --data "process_mode=oldest_first" \
  "https://gitlab.example.com/api/v4/projects/<project_id>/resource_groups/deploy-<branch>"

The resource group exists after the first check or deployment job for the branch has run.

GitHub: a workflow keeps only one waiting run per branch. When a third merge arrives while a deployment runs and a second one waits, the waiting run is cancelled before it starts. Its pre-deployment and post-deployment actions are not run and, with Quick Deploy or delta deployments, its metadata may never reach the org. To deploy every merge, uncomment queue: max in process-deploy.yml: up to 100 runs wait for their turn.

Azure: do not tick Batch changes while a build is in progress in the triggers of the pipeline, as a batched run only deploys the last merge. To run the deployments one at a time, set up the optional one job at a time per org.

Bitbucket: to run the deployments one at a time, first in, first out, set up the optional one job at a time per org.

Jenkins: queued builds of a branch can be folded into one build of its latest commit. With Quick Deploy or delta deployments, check that no merge was skipped, or deploy in full.

If your project was created with an older version of sfdx-hardis, bring these parts into your workflow files: see Upgrade the CI/CD files.

One job at a time per org (optional)

Salesforce runs one deployment at a time in an org. When the deployment check of a Pull Request starts while another check or a deployment runs in the same org, Salesforce keeps it Pending until the other one ends. Its CI job is already running: the wait counts in the job timeout and in SFDX_DEPLOY_WAIT_MINUTES, and a long queue can make the job fail.

This setting is optional. It is on by default on GitLab only: on GitHub, Azure, Bitbucket and Jenkins, the templates hold it commented out, and nothing changes until you turn it on as described below.

With one job at a time per org, the deployment checks, the major branch deployment check and the deployments of an org share a lock named deploy-<branch>. A job waits for the lock before it starts, so it never waits in the org. A job cancelled while it waits, by a new commit on its Pull Request for example, never reaches the org.

To turn it on, follow the page of your platform. Each one also gives the lines to add to pipeline files created with an older version of sfdx-hardis.

You can now go to step 4. Configure CI authentication.

Comments