How do I programmatically check which environment the app is in?

From the app JS I want to detect whether the app is running in Testing, Staging, etc. Is this possible?

In the runtime / app editor

One way to accomplish this is to write a function in your shared JS as follows:

function sharedEnv() {
    if(journey && journey.server) {
        return journey.server  
    } else return null;
}

This will return the url of the environment, e.g. https://run-testing.journeyapps.com.

To make this useful, you can use the following function in your view to determine if you are in e.g. testing or staging.

 if(sharedEnv().indexOf('testing') > -1) {
    //do logic or show a button for testing only
 }

In CloudCode

CloudCode also exposes the environment in a variable, but directly:

export async function run() {
    //returns the evironment the function is running in; testing, staging, production
    console.log(`${this.env}`)
}

The above function returns the following:

![The environment is returned (red box added for emphasis)](upload://7FjsUcHX1EcLOJwBPXmWunUFhBe.png)

2 Likes

What is a good way to detect the environment if I have two environments for staging? For instance, staging for development and staging for a client.

@AntonVanZyl The built-in this.backend returns the instance ID. Perhaps you could create a map in your shared or secrets file between your instances and the common names, e.g:{'instance1id' : 'dev','instance2id' : 'staging-dev','instance3id' : 'staging-customer','instance4id' : 'production'}Would this work for your use case?

Thanks @JasonBordelon, this is a great solution :slight_smile: