hardis:scratch:pool:refresh
Description
Command Behavior
Refreshes a scratch org pool by creating new scratch orgs to fill the pool and deleting expired ones.
This command is designed to maintain a healthy and adequately sized scratch org pool, ensuring that developers and CI/CD pipelines always have access to ready-to-use scratch orgs. It automates the lifecycle management of scratch orgs within the pool.
Key functionalities:
- Expired Org Cleanup: Identifies and deletes scratch orgs from the pool that are nearing their expiration date (configurable via
minScratchOrgRemainingDaysin.sfdx-hardis.yml). - Pool Replenishment: Creates new scratch orgs to replace expired ones and to reach the
maxScratchOrgsNumberdefined in the pool configuration. - Parallel Creation: New scratch orgs are created in parallel using child processes, optimizing the replenishment process.
- Authentication Handling: Authenticates to scratch orgs before deletion or creation, ensuring proper access.
- Dashboard Update (
gitProviderstorage): Records the refresh, the failed creations and the scratch org limits of the Dev Hub, then rebuilds the "Scratch Orgs Pool Dashboard" of the git provider. - Housekeeping (
gitProviderstorage): In a CI job, removes the entries encrypted with a previous key before the pool is counted, and deletes their scratch orgs from the Dev Hub whenSFDX_HARDIS_POOL_PREVIOUS_ENCRYPTION_KEYproves they are genuine. A refresh run from a computer keeps them and stops. Comments that the key of the pool did not sign are ignored. Removes the entries of the scratch orgs already taken.
Technical explanations
The command's technical implementation involves:
- Configuration Loading: It retrieves the
poolConfigfrom the project's.sfdx-hardis.ymlfile to get parameters likemaxScratchOrgsNumber,maxScratchOrgsNumberToCreateOnce, andminScratchOrgRemainingDays. - Pool Storage Interaction: It uses
listPoolOrgs,removePoolOrgsandaddScratchOrgToPoolto interact with the configured storage service (dashboard of the git provider, or Salesforce Custom Object on the Dev Hub). With thegitProviderstorage, the encryption key of the pool (SFDX_HARDIS_POOL_ENCRYPTION_KEY) is required. - Expiration Check: It calculates the remaining days for each scratch org in the pool using moment and flags those below the
minScratchOrgRemainingDaysthreshold for deletion. - Scratch Org Deletion: For expired orgs, it authenticates to them using
authenticateWithSfdxUrlStoreand then executessf org delete scratchviaexecCommand. - Scratch Org Creation: To replenish the pool, it spawns new child processes that run the
sf hardis:scratch:create --poolcommand. This allows for parallel creation of multiple scratch orgs. - Error Handling: It includes error handling for scratch org creation failures, logging them and updating the pool storage accordingly.
- Logging: Provides detailed logs about the status of scratch orgs (kept, deleted, created, failed creations) and a summary of the refresh operation.
Agent Mode
Supports non-interactive execution with --agent:
sf hardis:scratch:pool:refresh --agent
In agent mode, all interactive prompts are skipped and default values are used.
Parameters
| Name | Type | Description | Default | Required | Options |
|---|---|---|---|---|---|
| agent | boolean | Run in non-interactive mode for agents and automation | |||
| debug -d |
boolean | Activate debug mode (more logs) | |||
| flags-dir | option | Import flag values from a directory. | |||
| json | boolean | Format output as json. | |||
| skipauth | boolean | Skip authentication check when a default username is required | |||
| target-dev-hub -v |
option | Username or alias of the Dev Hub org. Not required if the target-dev-hub configuration variable is already set. |
true | ||
| websocket | option | Websocket host:port for VsCode SFDX Hardis UI integration |
Examples
$ sf hardis:scratch:pool:refresh
$ sf hardis:scratch:pool:refresh --agent