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, and basic command-line use. It covers what a Salesforce DX project must contain before a pipeline can deploy it.
Why the project layout matters
A pipeline starts from a fresh checkout of your repository. It has no memory of your laptop, your default org, or your local settings. Everything it needs must be committed to Git, in source format. Source format splits metadata into small files, so Git diffs are readable and merges work. The older metadata format, with large combined files, is not suitable.
The files a pipeline reads
my-project/
├── sfdx-project.json
├── config/
│ └── project-scratch-def.json
├── force-app/
│ └── main/default/
│ ├── classes/
│ ├── triggers/
│ └── objects/
├── .forceignore
└── .gitignoresfdx-project.jsondeclares the package directories and the API version. The CLI uses it to find your source.force-app/is the default package directory that holds your metadata.config/project-scratch-def.jsondefines scratch orgs. You need it only if you test in scratch orgs..forceignorelists files the CLI must skip when deploying..gitignorekeeps local files out of Git.
Configure sfdx-project.json
{
"packageDirectories": [
{ "path": "force-app", "default": true }
],
"name": "my-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}Set sourceApiVersion explicitly. If it differs from what your org supports, deployments can fail or behave differently from your local runs.
Control what gets deployed
Add a .forceignore so the pipeline does not try to deploy files that are not metadata:
**/jsconfig.json
**/.eslintrc.json
**/__tests__/**
package.xmlAdd a .gitignore so credentials and local state never reach the repository:
.sf/
.sfdx/
node_modules/
authurl.txtThe .sf/ and .sfdx/ folders hold local org aliases and cached data. authurl.txt is the temporary file the later authentication chapter creates. Ignoring it prevents an accidental commit.
Check the project locally first
Before writing any workflow, confirm the project deploys cleanly from a clean state. Run a check-only deployment against a sandbox:
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 missing dependencies, such as a custom field that a class references but the repository lacks, before you automate.
What a pipeline run needs
Common errors
| Symptom | Likely cause |
|---|---|
| No source-tracked files found | `--source-dir` path does not match a package directory |
| Deployment works locally, fails in CI | Metadata exists in your org but not in the repository |
| Unexpected files in the deployment | `.forceignore` is missing patterns |
| Version mismatch errors | `sourceApiVersion` is missing or outdated |
Exercise
A deployment passes on your laptop but fails in the pipeline with an error that a custom field cannot be found. What is the most likely cause, and how do you confirm it?
Show answer
The class or other component references a field that exists in your org but is not committed to the repository, so the fresh checkout lacks it. Confirm by searching force-app for the field's metadata file, then retrieve it from the org and commit it.