Sub topic 17
Package workflows in CI/CD
This chapter assumes you can already create a package version, install it, and promote it by hand with the Salesforce CLI. It also assumes you keep your project in Git and know how to authenticate the CLI to a Dev Hub. Here you will turn those manual steps into a pipeline.
The shape of a package pipeline
A package pipeline splits into two stages. Validation runs on every pull request. Release runs when changes merge to the main branch.
Authenticating the pipeline
The pipeline needs a login to the Dev Hub. Store an SFDX auth URL as a secret in your CI system and never commit it. Log in with a file that exists only during the job.
echo "$DEVHUB_AUTH_URL" > devhub.txt
sf org login sfdx-url --sfdx-url-file devhub.txt --alias MyDevHub --set-default-dev-hub
rm devhub.txtValidating a pull request
Build a version on every pull request. Use --code-coverage so the build proves the package meets the 75% requirement. Without it, you can't promote that version later.
sf package version create \
--package Core \
--installation-key-bypass \
--code-coverage \
--wait 30 \
--target-dev-hub MyDevHub \
--jsonThe --json output includes the subscriber package version ID, which starts with 04t. Capture it with a tool such as jq and pass it to later steps. A non-zero exit code fails the job, so a failed build or failed Apex test blocks the merge.
Building a version can be slow on large packages. Do not use --skip-validation to speed up pull request builds if you plan to promote those versions. A version created that way can't be promoted.
Releasing from main
On merge, create the beta version, install it in a sandbox, and run your checks. Only then promote it.
sf package install --package "$VERSION_ID" --target-org QaSandbox --wait 20 --no-prompt
sf package version promote --package "$VERSION_ID" --target-dev-hub MyDevHub --no-prompt
sf package install --package "$VERSION_ID" --target-org MyProdOrg --wait 20 --no-prompt--no-prompt stops the CLI from asking for confirmation, which would hang a non-interactive job. Install packages in dependency order. With several packages, run the pipeline in the order listed in sfdx-project.json.
Practical tips
- Build only the packages whose directories changed. This keeps build times down and avoids version numbers that change with no real change.
- Pin dependencies to a specific version in release builds.
LATESTis convenient in development but makes builds harder to reproduce. - Store the version ID as a build artifact so the same build moves from sandbox to production.
- Check how many package versions your Dev Hub allows per day, because frequent pipeline builds count toward that limit.
Exercise
Why should the production install step use the same version ID that you tested in the sandbox, instead of building a new version?
Show answer
A new build is a different version, even from the same source. Installing the tested version ID means production gets exactly the artifact that passed testing and was promoted.