Work with (Environment) Variables

Introduction

With environment variables, you can have variables whose contents depend on the environment or stage you work in. Rulecube has these environments:

  • test This is the development environment where you create and maintain rulesets.

  • demo This is the quality assurance environment where testers and users test rulesets.

  • app This is the live environment where users work with rulesets.

And this is the correlated development process you usually follow for your rulesets:

  1. You create and maintain your rulesets in the test environment.

  2. When ready, you export your ruleset and import it into the demo environment.

  3. The persons responsible for quality assurance test the ruleset in the demo environment.

  4. You correct the issues they find in test and deploy them to demo until you solved all issues, and the ruleset is ready to go live.

  5. You export your ruleset and import it into the app environment where users work with it.

A variable with a different value for each environment

Now, let's assume that a ruleset calls an external API for financial data, and the corresponding URL varies per environment:

EnvironmentAPI URL

test

dev.findata.com

demo

test.findata.com

app

api.findata.com

One solution to this is to create a constant for the URL and change the constant value each time you deploy your ruleset to a different environment. But this is cumbersome and error-prone. So it's much better to use an environment variable because you only have to set the environment-specific values for the same variable once. And when you deploy your ruleset, Rulecube automatically chooses the correct variable value for the environment it's running in.

For example, you create an environment variable named FinDataURL and assign it the appropriate values in each environment:

EnvironmentFinDataURL

test

https://dev.findata.com

demo

https://test.findata.com

app

https://api.findata.com

Now, when you run in test, Rulecube automatically picks the dev.findata.com URL, and when you run in app, Rulecube selects the api.findata.com URL. Fully automated, without any intervention from your side.

Procedure

Let's bring the example into practice:

Step 1: Create the environment variable in test and use it in the ruleset

  1. If you're not in your Rulecube test environment, move over to test.

  2. Create a ruleset named EnvExample.

  3. Click Variables from the top menu. ↳ The general Environment variables page opens.

  4. Click Add variable. ↳ Rulecube adds a new environment variable row.

  5. In the Name field, enter FinDataURL.

  6. In the Value field, enter https://dev.findata.com.

  7. Click Save.

  8. Now, open the ruleset where you want to use the environment variable.

  9. Click Environment variables from the menu at the left. ↳ The ruleset Environment variables page opens:

  10. Select the FinDataURL checkbox.

  11. Create a rule named ShowURL and set its Expression to:

    FinDataURL

    ↳ This rule just returns the content of the environment variable, which is fine for illustrating how environment variables work, but in practice, a rule Expression like this is more realistic:

    Http.request(FinDataURL + "?region=europe").data.data

  12. Click Save.

  13. Click Try it out.

  14. Click Execute. ↳ The Sandbox output shows "https://dev.findata.com".

Step 2: Create the environment variable in demo

  1. Move over to your demo environment.

  2. Click Variables from the top menu.

  3. Click Add variable.

  4. In the Name field, enter FinDataURL.

  5. In the Value field, enter https://test.findata.com.

  6. Click Save.

Step 3: Create the environment variable in app

  1. Move over to your app environment.

  2. Click Variables from the top menu.

  3. Click Add variable.

  4. In the Name field, enter FinDataURL.

  5. In the Value field, enter https://api.findata.com.

  6. Click Save.

Step 4: Deploy your ruleset

  1. Move back to your test environment.

  2. Open the EnvExample ruleset.

  3. Click Export. ↳ The Export pop-up opens.

  4. Click OK. ↳ Rulecube exports the ruleset as a JSON file into the default download folder on your workstation.

  5. Move over to your demo environment.

  6. Click Import. ↳ The Import ruleset pop-up opens.

  7. Open your file manager and navigate to your default download folder.

  8. Drag the exported ruleset JSON file and drop it on the Upload (or drag) ruleset data file section of the Import ruleset pop-up.

  9. Click Upload. ↳ Rulecube imports the ruleset and shows the General settings page for the ruleset.

  10. Click Try it out.

  11. Click Execute. ↳ The Sandbox output now shows "https://test.findata.com" as the value of the FinDataURL environment variable.

And you can do the same deployment steps for your app environment.