There is a new feature in the Salesforce CLI version 7.176.1
documented in the release notes.
Replace snippets of your metadata source files with specific values right before you deploy the files to an org with the force:source:deploy|push
commands! Note works with only the force:source:deploy or push commands of the CLI
To create a replacement you will need a section named replacements
in your sfdx-project.json
"replacements": [
{
"filename": "force-app/main/default/classes/myClass.cls",
"stringToReplace": "replaceMe",
"replaceWithEnv": "THE_REPLACEMENT"
"replaceWhenEnv": [{
"env": "DEPLOY_DESTINATION",
"value": "PROD"
}]
}
]
The above implied Replace THE_REPLACEMENT in the Apex class myClass with the string replaceMe when there is an environment variable named DEPLOY_DESTINATION with value PROD.
You can specify these keys in the replacements property:
filename: Single file that contains the string to be replaced.
glob: Collection of files that contain the string to be replaced. Example: **/classes/*.cls.
stringToReplace: The string to be replaced.
regexToReplace: Regular expression that specifies a string pattern to be replaced.
replaceWithEnv: Specifies that the string be replaced with the value of the environment variable.
replaceWithFile: Specifies that the string be replaced with the contents of a file.
replaceWhenEnv: Specifies a condition, using environment variables, for when a string replacement occurs.
Currently it does not support a file format of .env
so you would need a true environment variable in your operating system.