The first step to weaponizing a Saleforce scratch org is to automate its creation and maintenance. Since scratch orgs expire every few days, automation needs to do a few key operations:
- Detect when a scratch org either does not exist or exists and has expired.
- Delete a scratch org.
- Create a scratch org.
The rest of this post describes how we automate our scratch org creation maintenance using Apache/Ant and Jenkins.
This post is the second post in this series.
- Introduction: How to Weaponize Scratch and Developer Orgs
- How to Populate a Scratch Org Using Automation
Step 1: Build Apache/Ant Macros for sfdx Actions
To create and and maintain scratch orgs, only three basic sfdx functions are needed:
- Determine if a scratch org is active.
- Create a scratch org given a definition file and user alias.
- Delete a scratch org (if it exists)
The following ant script creates the needed macros and the only tricky part is that it depends on another Apache/Ant file to load Apache/Ant contribution libraries.
The previous script depends on another Apache/Ant script for loading Ant Contrib libraries. It also, is a pretty simple script.
Step 2: Create a Scratch Org Automation Apache/Ant Script
Now that you have the basic Apache/Ant tasks in place, the next step is to build your first scratch org. What you will need is:
- A scratch org json configuration file.
- A user alias to assigned to the scratch org.
The following example Apache/Ant script builds a scratch org we use for unit testing. Note that the tasks which install custom meta data and populate the scratch org with data are left out. They are discussed in another post.
In the example:
- The sfdx configuration file is CopyStormUnitTest.json
- The user alias for the scratch org will be CopyStormUnitTest.
Since you are reading this post, I suspect that you know how to create a scratch org configuration file already. However, for the record, here is the CopyStormUnitTest.json file used in the example.