Environment Variables

Updated 1 week ago by Copado Solutions

Introduction

An environment variable is a container that stores strings that are unique to a specific environment, such as Salesforce ids, integration endpoints, or usernames. This feature allows you to make commits, deployments, branch validation, and Selenium test cases environment agnostic.

How Do Environment Variables Work?

Variables have a name and a value-scope, which are unique in each environment. When you commit, the value provided for the source environment when defining the variable is replaced by the variable key (value-to-key replacement).

For example, having the following Environment Variables configured:

Environment

Variable

Value

Dev1

Org_URL

https://copado-dev1-ed.lightning.force.com/

UAT

Org_URL

https://copado-uat-ed.lightning.force.com/

When committing changes in Dev1, the following replacement will happen:

https://copado-dev1-ed.lightning.force.com → {!Environment.Org_URL}

Then, upon deployment, the variable key is replaced by the value provided for the environment which is the destination of the deployment (key-to-value replacement):

{!Environment.Org_URL} → https://copado-uat-ed.lightning.force.com/

To guarantee a proper replacement process in all scenarios, environment variables should have unique names and values-scope. For example, there cannot be variables in the same environment with the same value and scope.

Where Are Environment Variables Applied?

Environment variables are applied automatically in the following scenarios:

  • Commits
    When performing a Git commit, Copado searches the metadata files to match environment variable values and replaces them with the corresponding key.
    E.g. Environment variable: AdminUser = admin.user.test@company.com. In the metadata files, any references to ‘admin.user.test@company.com’ would be replaced with {!Environment.AdminUser} This then allows Copado to replace other environment variables in the metadata during a deployment.
  • Git Deployments and Promotions
    When performing a deployment from Git, Copado searches the metadata files for any matching environment variable keys and replaces them with the corresponding value.
    E.g. Environment variable: AdminUser = admin.user.uat@company.com. In the metadata files, any references to {!Environment.AdminUser} would be replaced with admin.user.uat@company.com.
  • Org to Org Deployments
    When performing an org to org deployment, Copado replaces all maps from the source environment variables with the destination environment.
    E.g. Source environment variable: AdminUser = admin.user.test@company.com Destination environment variable: AdminUser = admin.user.uat@company.com. In the metadata files, Copado will replace any references to admin.user.test@company.com with admin.user.uat@company.com’.
  • Custom Setting Deployment
    When performing a custom setting deployment step, Copado searches in the custom setting fields for any matching environment variable values and replaces them with the corresponding value of the destination environment. Check out the article Custom Settings Deployment for more information.

Environment variables can also be referenced, if required, in the following scenarios:

  • Git Snapshots
    When clicking on Take Snapshot Now in a Git snapshot, a popup appears with a checkbox called Apply Environment Variables. If checked, Copado will search the metadata files during the snapshot process for any matching environment variable values and replace them with the corresponding key.
    E.g., Environment variable: AdminUser = admin.user.test@company.com In the metadata files, any references to ‘admin.user.test@company.com’ would be replaced with {!Environment.AdminUser} This then allows Copado to replace other environment variables in the metadata during a deployment.
  • Apex Anonymous Deployment Step
    When performing an Apex Class step within a deployment, Copado searches in the script for any matching environment variable keys (e.g. {!Environment.AdminUser} ) and replaces them with the corresponding value (e.g., admin.user.test@company.com). 
    E.g. Environment variable: AdminUser = admin.user.test@company.com (value in the destination environment). In the Apex Class files, any references to {!Environment.AdminUser} would be replaced with 'admin.user.test@company.com'.
  • Data Deployment Step
    You can apply environment variables to Data deployments by adding ":applyEnv=true" to the step name.

For information about how to create environment variables, please refer to the Creating Environment Variables article.


How did we do?