#Build with different environment variables

4 messages · Page 1 of 1 (latest)

sturdy swift
#

How do I run astro build for different environments?

fervent patrolBOT
#

Hi @sturdy swift. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Astro allows you to use environment variables during the build process. If an .env file containing environment variables is present when the build process is run, these values will be hard-coded in the output.

To run astro build for different environments, you can provide Astro with every environment variable to expect at run-time. This can be done by specifying the environment variables before the astro build command. For example: VARIABLE_1=placeholder astro build. This signals to Astro that the actual value will be available when the built application is run. The placeholder value will be ignored by the build process, and Astro will use the value provided at run-time.

In the case of multiple run-time variables, store them in a separate file (e.g. .env.runtime) from .env. Start the build with the following command:

export $(cat .env.runtime) && astro build

This command will load the environment variables from the .env.runtime file and then run the astro build command.

Astro also includes a few default environment variables such as import.meta.env.MODE, import.meta.env.PROD, import.meta.env.DEV, import.meta.env.BASE_URL, import.meta.env.SITE, and import.meta.env.ASSETS_PREFIX. These can be used like any other environment variable.

Remember, Astro evaluates configuration files before it loads your other files. As such, you can’t use import.meta.env to access environment variables that were set in .env files in a configuration file. You can use process.env in a configuration file to access other environment variables.

#

Sources:


Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: