- Your project does not have any dependency on external metadata (unless that dependency is another package)
- All your unit tests are passing
- Enabled Packaging 2 feature is enabled in your Dev Hub org (Production Org)
- You have permissions to create scratch orgs
# for example: SALESFORCE-UTILS
$ git clone https://github.com/UCSF/SALESFORCE-UTILS.git
# run this command outside cloned project folder
# make sure the project name is the same as cloned repo name to avoid creating a new folder
$ sfdx force:project:create -n SALESFORCE-UTILS
# change working directory to cloned repo
$ cd SALESFORCE-UTILS
# run command to convert project
$ sfdx force:mdapi:convert -r src/
#
$ sfdx force:org:create -f config/project-scratch-def.json --durationdays 30 -a UTILS-DEV
$ sfdx force:package2:create --name UCSF-SALESFORCE-UTILS --description "Utility code for Salesforce projects" --containeroptions Unlocked --nonamespace --targetdevhubusername DevHub
Successfully created a second-generation package (package2). 0HoA00000008OI5KAM 033A0000000Eu6JIAS
=== Ids
NAME VALUE
───────────────────── ──────────────────
Package2 Id 0HoA00000008OI5KAM
Subscriber Package Id 033A0000000Eu6JIAS
Follow the steps documented in following link under "Create the Package Version and Install It in Your Scratch Org" section: Reference
- Update sfdx-project.json as explained in prevoius link
{
"packageDirectories": [
{
"path": "force-app",
"default": true,
"id": "0HoA00000008OI5KAM",
"versionName": "Version 1.0",
"versionNumber": "1.0.0.NEXT"
}
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "42.0"
}
- Create package version
# create package version
$ sfdx force:package2:version:create --installationkey "8kiGTyMM6XGeovTRm4J%4pAE" --directory force-app --wait 10
# after few minutes see result of packge version creation
Successfully created the package2 version [08cA00000008OI5IAM]. Package2 Version Id: 05iA00000008OI5IAM. Subscriber Package2 Version Id: 04tA0000000FfNEIA0.
Package Installation URL: https://login.salesforce.com/packaging/installPackage.apexp?p0=04tA0000000FfNEIA0
As an alternative, you can use the "sfdx force:package:install" command.
- Install the package in the scratch org
# use the Subscriber Package2 Version Id: 04tA0000000FfNEIA0 (created from previous command)
$ sfdx force:package:install -i 04tA0000000FfNEIA0 -u UTILS-DEV --wait 10 --publishwait 10
Waiting for the package install request to complete. Status = IN_PROGRESS
Waiting for the package install request to complete. Status = IN_PROGRESS
Waiting for the package install request to complete. Status = IN_PROGRESS
Successfully installed package [04tA0000000FfNEIA0]
- Open the scratch org and view the package
$ sfdx force:org:open -u UTILS-DEV
Wait for your default browser to open the scartch org and then navigate to Setup -> Installed Packages
Following command will make this package version available for production orgs
$ sfdx force:package2:version:update -i 05iA00000008OI5IAM --setasreleased
Are you sure you want to release package version 05iA00000008OI5IAM? You can't undo this action. Release package (y/n)?: y
Successfully updated the package version. ID: 05iA00000008OI5IAM.
Now try to install in a developer org or a Trailhead Playground
# install package version
$ sfdx force:package:install -i 04tA0000000FfNEIA0 -u MyTPO --wait 10 --publishwait 10
Waiting for the package install request to complete. Status = IN_PROGRESS
Waiting for the package install request to complete. Status = IN_PROGRESS
Waiting for the package install request to complete. Status = IN_PROGRESS
Waiting for the package install request to complete. Status = IN_PROGRESS
Waiting for the package install request to complete. Status = IN_PROGRESS
Waiting for the package install request to complete. Status = IN_PROGRESS
Successfully installed package [04tA0000000FfNEIA0]
# open the org and avlidate that package version is installed
$ sfdx force:org:open -u MyTPO