Sub topic 2
Preparing a Salesforce DX project for CI
This chapter assumes you know basic Git and GitHub usage, Salesforce metadata and Apex tests, command-line basics, and YAML syntax. A pipeline can only deploy what it can find and read. This chapter covers the project layout and configuration files that make a repository deployable by a machine with no human to fix things up.
What a pipeline expects from the repository
A Salesforce DX project is a Git repository whose metadata is stored in source format. Source format splits metadata into small files. An Apex class is a .cls file with a .cls-meta.xml companion, and a custom object becomes a folder of field and layout files. Metadata format is the older layout that Metadata API retrieves and change sets use. It has large XML files and a package.xml manifest.
Source format suits CI because Git diffs stay small and merge conflicts stay local. A pull request that changes one field touches one file.
A typical project looks like this:
my-project/
├── config/
│ └── project-scratch-def.json
├── force-app/
│ └── main/
│ └── default/
│ ├── classes/
│ ├── objects/
│ ├── lwc/
│ └── triggers/
├── .forceignore
├── .gitignore
├── package.json
└── sfdx-project.jsonThe pipeline only needs three things from this layout. sfdx-project.json must be at the repository root. The folders it lists must exist. Everything the org needs must be in version control.
sfdx-project.json
This file tells the CLI where source lives and which API version to use.
{
"packageDirectories": [
{
"path": "force-app",
"default": true
}
],
"name": "my-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "62.0"
}These fields matter most for CI:
| Field | Purpose | Common mistake |
|---|---|---|
| `packageDirectories` | Folders the CLI treats as deployable source. Exactly one entry has `"default": true`. | Adding a folder on disk but not listing it. The CLI ignores it. |
| `sourceApiVersion` | API version used for deploys and retrieves. | Leaving an old version after your org upgrades, so new metadata fields are rejected or dropped. |
| `sfdcLoginUrl` | Login host for authentication. | Using the production URL for sandboxes. Sandboxes use `https://test.salesforce.com`, unless you authenticate with an auth URL, which stores the host. |
| `namespace` | Namespace for managed package development. | Setting it for an org-based project that has no namespace. |
If your repository has several package directories, for example force-app and unpackaged, deploying with --source-dir force-app skips the others. Decide whether the pipeline deploys one directory or all of them. Then make the --source-dir flag say so. You can pass several directories by repeating the flag.
Controlling what gets deployed with .forceignore
.forceignore uses .gitignore syntax. It excludes files from deploys, retrieves, and source tracking. Files that are valid in Git but invalid in an org will fail a pipeline.
**/jsconfig.json
**/.eslintrc.json
**/__tests__/**
**/*.dup
package.xmlTypical entries are editor config files, JavaScript test folders for Lightning Web Components, and any metadata you deliberately manage by hand in the org. Keep this file in version control, because the runner starts from a clean checkout. Any ignore rule that exists only on your laptop does not apply there.
Be careful with metadata such as profiles and permission sets. They are large, they depend on every other component, and different orgs often need different values. Teams often exclude some of them or deploy them separately. Whatever you choose, record the decision in .forceignore so that every pipeline run applies it.
.gitignore and local state
The CLI writes local state into .sf/ and .sfdx/. These folders hold org aliases, tracking data, and test results. Never commit them. Auth tokens and org details can end up in these folders, and the runner creates its own anyway.
.sf/
.sfdx/
node_modules/
authurl.txt
*.logThe authurl.txt entry guards against committing the temporary credential file used in the workflows later in this tutorial.
Pinning the CLI version with package.json
A pipeline that installs "latest" can break on a day when you changed nothing. To avoid that, declare the CLI as a development dependency and commit the lock file:
npm init -y
npm install --save-dev @salesforce/cli
npm install --save-dev sfdx-git-delta
git add package.json package-lock.jsonThe runner then calls npm ci, which installs exactly the locked versions, and runs commands with npx sf. You upgrade the CLI deliberately, in a pull request that the pipeline itself validates. Later chapters install the CLI globally to keep the examples short. Pinning is the stricter alternative for teams that need repeatable runs. Add sfdx-git-delta only if you plan to use delta deployments.
A scratch org definition for test runs
If the pipeline will test in scratch orgs, the repository needs a definition file. config/project-scratch-def.json describes the org to create:
{
"orgName": "CI scratch org",
"edition": "Developer",
"features": [],
"settings": {
"lightningExperienceSettings": {
"enableS1DesktopEnabled": true
}
}
}A scratch org starts empty, so any feature or setting your code depends on must be listed here. If a test passes in your sandbox and fails in a scratch org with a missing-feature error, the definition file is the first place to look.
Making the project self-sufficient
CI fails most often because the repository does not contain everything the deployment needs. Check these before wiring up a workflow:
- Every dependency of your code is in source control. A class that references a custom field not in the repository deploys on your sandbox, because the field exists there, but fails on a clean target.
- Apex tests create their own data. Tests that read existing org records pass in one org and fail in another.
- Test classes are in the deployed directories. With
RunLocalTests, the org runs every test class that is not in a managed package. Missing tests lower coverage, and a missing class can fail the whole deploy. - Metadata that the target org lacks is deployed first or created by hand. Examples are record types, a required feature, or a Experience Cloud site.
The diagram shows how these files feed a pipeline run.
Checking the project locally before CI
Run the same command the pipeline will run, against a sandbox you can afford to break. A check-only deployment changes nothing in the org:
npm ci
npx sf project deploy start \
--dry-run \
--source-dir force-app \
--test-level RunLocalTests \
--target-org my-sandbox \
--wait 60If this fails on your machine, it will fail in the pipeline. Fix it here first, where feedback is faster.
Common errors at this stage
- "No source-format files found" or an empty deployment. The
--source-dirpath does not match a listed package directory, or the files are in metadata format. Convert them withsf project convert mdapi. - "Entity of type X named Y cannot be found". The repository is missing a dependency. Retrieve it from the org and commit it.
- Deploys differ between laptop and runner. The
.forceignore,package.json, orsourceApiVersionis not committed, or the CLI versions differ. - Unexpected files in the deployment.
.forceignorepatterns are too narrow. Test them against a path withgit check-ignore-style thinking: the same glob rules apply.
Exercise
A deployment succeeds from your laptop to a sandbox but fails in CI on a clean org with "Entity of type CustomField named Invoice_c.Region_c cannot be found". Name the likely cause and one fix.
Show answer
The field exists in your sandbox but is not committed to the repository, so the clean target does not have it. Retrieve the field into the project, commit it under a listed package directory, and make sure .forceignore does not exclude it.
Exercise
Why commit package-lock.json and use npm ci instead of installing the latest Salesforce CLI on every run?
Show answer
The lock file fixes exact versions, so the runner uses the same CLI as your last validated pull request. An unpinned install can pick up a new release that changes behavior and break a pipeline you did not touch.