Skip to main content

Use Cockpit-set environment variables at deployment time

This article will guide you through using Cockpit-set environment variables at deployment time.

How to use Cockpit-set environment variables at deployment time

When you deploy your project to Front-Commerce Cloud, you can use environment variables set that you previously set in the Cockpit. Those variables are available in a .env file generated at the root of your project.

Here is an example script that will retrieve the environment variables from this file using the dotenv package and display some of them in the console:

scripts/prebuild.js
import dotenv from "dotenv";

dotenv.config();

console.log("Here are the environment variables:");
console.log("TEST_PREBUILD:", process.env.TEST_PREBUILD);
console.log("TEST_PREBUILD_SECRET:", process.env.TEST_PREBUILD_SECRET);

To use this script at deployment time, you can use for example the prebuild script in your package.json file:

{
"scripts": {
"prebuild": "node scripts/prebuild.js"
//... other scripts ...
}
}

Then, when you deploy your project, the script will be executed and the environment variables will be displayed in the deployment logs.