Sub topic 5
Beta versus released versions and promotion
This chapter covers the two states a package version can be in and what changes when you promote one. It assumes you can create a package version with sf package version create. It also assumes you know how the MAJOR.MINOR.PATCH.BUILD number is built and can read sfdx-project.json. Examples use the Utils unlocked package from earlier chapters.
Two states, one irreversible step
Every package version that you create starts as a beta version. A beta version is a work in progress. You can build as many of them as you like, install them in test environments, and discard them. When a version passes your testing, you promote it. Promotion marks the version as released, and a released version is the only kind you can install in production.
Promotion does not rebuild or repackage anything. It changes a flag on an existing version, so the 04t ID you tested is the same 04t ID that you ship.
| Aspect | Beta | Released |
|---|---|---|
| How it is created | `sf package version create` | `sf package version promote` on an existing beta |
| Where it can be installed | Scratch orgs, sandboxes, and Developer Edition orgs | Any org, including production |
| Can it be deleted | Yes | Managed: no. Unlocked: only while it is not in use |
| Can it be a dependency of a released version | No | Yes |
| Can another version reuse its MAJOR.MINOR.PATCH | Yes, until one is released | No, only one release per MAJOR.MINOR.PATCH |
The exact list of org types that accept betas has changed over time. Treat the table as the working rule: betas are for non-production testing. If an install of a beta fails against a production org, that is expected behavior and not a bug.
The lifecycle
The state diagram below shows how a version moves from creation to release. Failed validation and deletion are the two exits from the beta state.
Creating a beta
A normal create command produces a beta. The --code-coverage flag matters here because promotion depends on it.
sf package version create --package Utils --installation-key-bypass --wait 20 --code-coverage --target-dev-hub myDevHubWhen the command finishes, it reports a subscriber package version ID that starts with 04t. It also records an alias in sfdx-project.json such as [email protected]. The -1 is the build number that the NEXT keyword resolved to. You can build again with the same 1.1.0.NEXT and get [email protected], [email protected], and so on. All of them are betas of the same MAJOR.MINOR.PATCH.
Checking what you have
Before promoting, confirm the state and the coverage result. The report command shows a single version.
sf package version report --package "[email protected]" --target-dev-hub myDevHubIn the output, look at these fields:
IsReleasedisfalsefor a beta andtrueafter promotion.HasPassedCodeCoverageCheckistrueif the version met the coverage requirement.CodeCoverageshows the percentage that was measured.ValidationSkippedistrueif you created the version with--skip-validation.
To see the whole history of a package, use list. Add --released to show only released versions.
sf package version list --packages Utils --target-dev-hub myDevHub
sf package version list --packages Utils --released --target-dev-hub myDevHubPromotion requirements
sf package version promote fails unless the version meets all of these conditions:
- The version is still a beta.
- It was created with code coverage and met the minimum. For unlocked and second-generation managed packages, that minimum is 75 percent Apex coverage across the package.
- It was not created with
--skip-validation. Skipping validation makes builds faster, and those versions are for quick iteration only. They can never be promoted. - No other version with the same MAJOR.MINOR.PATCH has already been released.
- Every package it depends on is itself a released version. A version that depends on a beta cannot be promoted.
- You are authenticated to the Dev Hub as a user who has permission to promote.
Requirement 5 matters most when you work with several packages. Promote the lower-level package first, then update the dependency in the dependent package and build it.
Run the promotion like this:
sf package version promote --package "[email protected]" --target-dev-hub myDevHubThe CLI asks you to confirm because the action cannot be reversed. In scripts, add --no-prompt to skip the question. Use that flag only in a pipeline that has already checked the version.
The one-release-per-number rule
Suppose [email protected] is released. You then build [email protected] with a bug fix. You can still create it, because creating is allowed, but you can never promote it. The Dev Hub already has a released version for 1.1.0.
The way out is to change the version number in sfdx-project.json before you build the next version. Patch changes for a released version are written like this:
{
"path": "util",
"package": "Utils",
"versionName": "Utils 1.1 patch",
"versionNumber": "1.1.1.NEXT",
"default": false
}The next create run then produces [email protected]. Build numbers restart for each new MAJOR.MINOR.PATCH.
A practical habit follows from this rule. Decide the target number before you start a development cycle. Build as many betas as you need under that number. Promote only the one that passed your full test run. Then bump the number for the next cycle.
Using betas in a delivery process
Betas let you run the same artifact through several environments before it reaches users. A common flow looks like this:
- A pull request builds a beta of the changed package.
- The pipeline installs that beta into a fresh scratch org and runs the tests.
- After merge, the pipeline builds a new beta from the main branch and installs it in a sandbox for user testing.
- Once approved, the pipeline promotes that exact version.
- The pipeline installs the released version in production.
Step 4 promotes the version tested in step 3 and does not build a new one. That is the benefit of the beta and released split: what you tested is what you ship.
The --branch and --tag flags on create help you track which source produced which beta. The Dev Hub stores them with the version, and list can show them, so you can match a beta back to a commit.
sf package version create --package Utils --installation-key-bypass --wait 20 --code-coverage --branch main --tag "build-4521" --target-dev-hub myDevHubCleaning up betas
Betas pile up quickly, one per pipeline run. Delete the ones you no longer need so the version list stays readable.
sf package version delete --package "[email protected]" --target-dev-hub myDevHubYou can delete a beta whenever you like. A released managed version cannot be deleted. Deleting a version that other packages depend on will break their builds, so check dependents first.
Common mistakes
- Expecting
NEXTto bump the minor number.NEXTonly picks the next build number. Edit MAJOR, MINOR, or PATCH by hand. - Promoting a version built with
--skip-validation. The promote command rejects it. Rebuild without that flag. - Forgetting
--code-coverage. Without it, the version has no coverage result. You must create a new version, because coverage cannot be added later. - Trying to install a beta in production. Use a sandbox or scratch org, or promote and then install.
- Promoting a dependent before its dependency. The promote fails until the dependency is released.
Exercise
You released [email protected]. A teammate fixes a bug and your pipeline builds [email protected] successfully, but sf package version promote on it fails. What is the cause, and what do you change to get a releasable version?
Show answer
Only one version per MAJOR.MINOR.PATCH can be released, and 1.1.0 already has a released version (build 1). Build 2 is just another beta of 1.1.0. Change versionNumber in sfdx-project.json to 1.1.1.NEXT (or a higher minor or major), create a new version with --code-coverage and without --skip-validation, test it, and promote the resulting [email protected].
Exercise
Sales App depends on Utils. [email protected] is still a beta. Why will promoting a Sales App version that depends on it fail, and in what order should you do the work?
Show answer
A version can be promoted only if all the packages it depends on are released. Promote [email protected] first (after it passes testing), make sure Sales App's dependency points to that released version, build a new Sales App version, test it, and then promote Sales App.