[JavaScript] Environment Variables

3 minute read

How to read, delete and add an environment variable using JavaScript?

I have a cleanup script that deletes all the existing data that we use as part of our automation testing through an API. This script is written in Javascript. I will run this script on a demand basis when the amount of data is getting too much. But instead of running it on demand basis, I want to run it on schedule basis and let the automation takes control so that I can enjoy my morning tea without needing to worry about the data being accumulated over time.

So how can I do it? Basically, I need to modify the existing cleanup script so that it could be run on a scheduled basis from the pipeline. My recent project, we use Bamboo(https://www.atlassian.com/software/bamboo). So, I need to configure Bamboo to run the script. I think running the script on a weekly basis will be sufficient for now.

But wait, at the moment, all the variables are stored in the script. I hardcoded it. Why? because it is the easiest way to do it and I am a lazy person. I know it is not a good practice to do so for at least these two reasons. One, we should not hard-coded any confidential data in a script. Second, we should store configuration values in configuration fields. Any changes in the future, we can easily update the configuration fields with new data instead of making the changes in the code.

Therefore, I need to update the script so that it uses the value in configuration fields. Since I am using Bamboo CI/CD, then I can store the values Bamboo Plan Configuration Variables page.

How can I achieve it, the easiest method to achieve this is by modifying the code in the script to read the variables from the Environment variables and setting the value in Bamboo Plan configuration to store these environment variables. The script is then will be executed using the value from Bamboo itself.

The above describes my thought process what I plan to do in automating my cleanup script to be run on weekly basis. But how can Javascript read values from Environment variables using Node?

Node has this in-house module that provides the capability to work with environment variable. It is called process module.

To get started, I launch my favourite IDE (Visual Studio Code). Create a new Javascript file and name it ReadEnvVariable.js. Update the JS file with the following code. Then, click Terminal to open a new Terminal screen. Type Node ReadEnvVariable.js in the Terminal screen and press “Enter”.

// Retrieve a value from System Environment Variables
console.log(`Existing Environment Variable chromeDir : ${process.env.windir}`);  

Result: Will display the existing Environment Variables windir

//Add a new custom value into System Environment Variables
let chromeDirValue = "C:Chrome";  
process.env.chromeDir = chromeDirValue;  
console.log(
  `New Custom Environment Variable chromeDir : ${process.env.chromeDir}`
);  

Result: Will display the new created Environment Variables chromeDir

//Update a custom value from Environment Variables
chromeDirValue = "C:Chrome\\Data";  
process.env.chromeDir = chromeDirValue;  
console.log(`Update Custom Environment Variable chromeDir :${process.env.chromeDir}`);

Result: Will display the updated Environment Variables chromeDir

//Delete a custom value from Environment Variables
delete process.env.chromeDir;  
console.log(`Delete Custom chromeDir : ${process.env.chromeDir}`); // Undefined value

Result: Will display undefined value because chromeDir Environment Variables has been deleted.

Below is the final result after running all the codes above.

img


// Retrieve all values from Environment Variables
console.log(
  `\n All Environments Variables : ${JSON.stringify(process.env, null, 2)}`
);

Result: Will display all the environment variables that belong to the current machine.

By using Process module from Node, I can easily get and use Environment variables in my cleanup script. There are many ways that we can take advantage of Process module. In my case, I just need it to easily enable me to run my cleanup script in the pipelines.