How to Create, Promote, and Install a Salesforce 2GP (Second-Generation Managed Package) Using Salesforce CLI
Raptbot Technologies Private Limited
Elevate Your Business with Raptbot: Your Premier Salesforce Partner for Innovation, Integration, and Unmatched Expertise
In this guide, you’ll learn how to use Salesforce CLI to create, promote, and install a Second-Generation Managed Package (2GP) from your Salesforce Dev Org to another. We’ll also cover the steps required to enable Dev Hub, Second-Generation Managed Packages, and Unlocked Packages in your Dev Org.
Prerequisites:
Before you can create and manage 2GP packages, make sure you have the following setup:
Dev Hub Org: This will be the central org to manage your packages.
Target Org: The org where you will install the package.
Navigate to Setup → Dev Hub → Enable Dev Hub.
Navigate to Setup → Dev Hub → Enable Unlocked Packages and Second-Generation Managed Packages
`
Ensure you have Salesforce CLI installed on your local machine. You can download it from the official Salesforce website:Download Salesforce CLI.
Steps to Create, Promote, and Install a 2GP (Second-Generation Managed Package)
Once you’ve enabled the required features, you can proceed to the steps for creating, promoting, and installing the package.
Step 1: Authorize Your Dev Hub Org
First, authorize your Dev Hub Org in Salesforce CLI. This will enable you to manage your packages and create scratch orgs.
sfdx force:auth:web:login -d -a Package2Gp
After running this command, a web page will open where you’ll log in to your Dev Hub Org.
Step 2: Create the Package
Now that the Dev Hub Org is authorized, you can create a Managed Package (Second-Generation Package) that will contain your Salesforce components.
sfdx force:package:create --name "Package2GP" --path force-app --packagetype Managed --targetdevhubusername Package2Gp
This command creates the package and registers it with your Dev Hub Org.
领英推荐
Step 3: Create a Version of the Package
Once the package is created, the next step is to create a version. You can add tests and code coverage as part of the version creation.
sfdx force:package:version:create --package "Package2GP" --installation-key-bypass --wait 10 -v TestScratch --codecoverage
This command creates a new version of your package, and you’ll receive a version ID.
Step 4: Promote the Package Version
Before releasing the package, it must be promoted from beta to released. This step makes it publicly available for installation.
sfdx force:package:version:promote -p versionname/versionId -v Package2Gp
Promoting the version will move it from beta to the released state, which means it can now be installed in other orgs.
Step 5: Install the Package
Once the version is promoted, you can install the package into your Target Org. You will get a link for the package, or you can use the following command to install it via Salesforce CLI:
sfdx force:package:install --package versionname --targetusername RoundRobin --installation-key test1234 --wait 10 --publishwait 10
After executing this command, the package will be installed into your Target Org.
Additional Tips:
You can automate the entire process with CI/CD tools like GitHub Actions or CircleCI, using Salesforce CLI to create, promote, and install packages automatically.
Before promoting a version, ensure you test it in a scratch org to verify the components work as expected.
If your package depends on other packages, install them first, ensuring the proper order of installation.
The namespace you choose for your package cannot be changed after package creation. Be careful when selecting the namespace, as it affects the entire package.
Use semantic versioning to keep track of package versions and ensure compatibility when upgrading packages in target orgs.