Lewati ke konten
Docs Try Aspire
Docs Try

Environments

Konten ini belum tersedia dalam bahasa Anda.

When you run or deploy an Aspire application, the environment determines how the AppHost configures resources, what parameter values are used, and how your deployment is organized. Environments let you use the same AppHost to target different deployment contexts — development, staging, production, or any custom name — without duplicating your application model.

The Aspire environment is a single named value — like Development, Staging, or Production — that the AppHost receives as input. Environment names are case-insensitive strings; you can use any name that makes sense for your workflow. Your AppHost code can branch on this value to change resource topology, resolve different parameter values, or set environment variables on child services.

The environment name flows through the system in three stages:

  1. Input: You pass the environment name to the AppHost. Deployment commands such as aspire deploy, aspire publish, and aspire do expose --environment directly, and aspire start also accepts --environment <name>.
  2. AppHost evaluation: Your AppHost code reads the environment to branch logic, resolve parameters, or configure resources.
  3. Downstream configuration: Your AppHost explicitly sets framework-specific environment variables (like DOTNET_ENVIRONMENT or NODE_ENV) on child resources as needed.

aspire run is a development-oriented command, so the AppHost runs in development mode by default. If you want to evaluate a different AppHost environment locally, start the AppHost explicitly and specify the environment:

Start locally with a staging environment
aspire start --environment Staging

Deployment-oriented commands such as aspire publish, aspire deploy, and aspire do default to Production. Override them with the --environment flag:

Deploy to staging
aspire deploy --environment staging
Publish for production
aspire publish --environment production

Each environment maintains its own deployment state cache, so staging and production deployments track their provisioning settings independently.

The environment name and the execution context are independent concepts:

ConceptWhat it answersDefault for aspire runDefault for aspire publish
EnvironmentWhere is the app targeting?DevelopmentProduction
Execution contextHow was the AppHost invoked?Run modePublish mode

You can start an AppHost locally with aspire start --environment Staging for validation, or publish to a Development cloud environment if your workflow needs that. The two axes are independent — environment names are arbitrary strings, not tied to run vs publish.

Your AppHost code can check the current environment to change behavior. The environment is available through the builder’s host environment API:

AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
// Check for specific environments
if (builder.Environment.IsDevelopment())
{
// Development-specific configuration
}
// Check for a custom environment name
if (builder.Environment.IsEnvironment("Testing"))
{
// Testing-specific configuration
}

The following convenience methods are available:

MethodReturns true when environment is
IsDevelopment() / isDevelopment()Development
IsStaging() / isStaging()Staging
IsProduction() / isProduction()Production
IsEnvironment(name) / isEnvironment(name)Any custom name

Set environment variables on child resources

Section titled “Set environment variables on child resources”

Your services often need to know which environment they’re running in. Different frameworks use different environment variables — use WithEnvironment to set the appropriate one for each service:

AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var isDevelopment = builder.Environment.IsDevelopment();
var dotnetEnvironment = isDevelopment ? "Development" : "Production";
var appEnvironment = isDevelopment ? "development" : "production";
// .NET services use DOTNET_ENVIRONMENT
builder.AddProject<Projects.Api>("api")
.WithEnvironment("DOTNET_ENVIRONMENT", dotnetEnvironment);
// Node.js services use NODE_ENV
builder.AddNodeApp("frontend", "../frontend", "server.js")
.WithEnvironment("NODE_ENV", appEnvironment);
// Any container can receive custom env vars
builder.AddContainer("worker", "myorg/worker")
.WithEnvironment("APP_ENV", appEnvironment);
builder.Build().Run();

Common environment variable conventions by framework and ecosystem:

FrameworkEnvironment variableTypical values
ASP.NET CoreASPNETCORE_ENVIRONMENTDevelopment, Staging, Production
.NET (non-web)DOTNET_ENVIRONMENTDevelopment, Staging, Production
Node.jsNODE_ENVdevelopment, production
Python (Flask)FLASK_ENV (deprecated)development, production
Python (Django)DJANGO_SETTINGS_MODULEModule path
Ruby on RailsRAILS_ENVdevelopment, staging, production
GoAPP_ENVdevelopment, staging, production
RustAPP_ENV / RUST_ENVdevelopment, staging, production
Java (Spring)SPRING_PROFILES_ACTIVEdev, test, prod

Use parameters to externalize values that change between environments, such as SKU tiers, replica counts, or API endpoints:

AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var apiKey = builder.AddParameter("apiKey", secret: true);
builder.AddProject<Projects.Api>("api")
.WithEnvironment("API_KEY", apiKey);
builder.Build().Run();

When you call AddParameter("apiKey"), Aspire looks for a value under the configuration key Parameters:apiKey. The value can come from multiple sources:

All AppHost languages:

SourceExampleWhen to use
Environment variablesParameters__apiKey=valueCI/CD pipelines, containers
Command-line arguments--Parameters:apiKey=valueQuick overrides
Interactive promptPrompted during aspire deployFirst-time setup

C# AppHosts only (via .NET configuration):

SourceExampleWhen to use
appsettings.{env}.json{ "Parameters": { "apiKey": "value" } }Per-environment defaults in source control
appsettings.jsonSame structureBaseline defaults
User secretsdotnet user-secrets set "Parameters:apiKey" "value"Local development secrets

The double-underscore (__) in environment variable names replaces the colon (:) used in configuration keys. So the parameter apiKey maps to the environment variable Parameters__apiKey.

Per-environment defaults with config files (C# AppHosts)

Section titled “Per-environment defaults with config files (C# AppHosts)”

C# AppHosts can use appsettings.{environment}.json files to set different parameter values per environment:

appsettings.json
{
"Parameters": {
"apiKey": "dev-key-for-local-testing"
}
}
appsettings.Staging.json
{
"Parameters": {
"apiKey": "staging-key-value"
}
}

When the AppHost runs with --environment Staging, the staging file overrides the base values automatically. This is standard .NET configuration layering — no Aspire-specific mechanism is needed.

In CI/CD pipelines, set parameters as environment variables. This works for any AppHost language:

.github/workflows/deploy.yml
- name: Deploy to production
run: aspire deploy --environment production
env:
Parameters__apiKey: ${{ secrets.API_KEY }}
Parameters__replicas: '3'
Parameters__sku: 'Premium'

Environment variables take the highest priority, so they override any values from config files or defaults.

Use execution context for run vs publish decisions

Section titled “Use execution context for run vs publish decisions”

Use the execution context for choices that differ between local orchestration and publish/deploy workflows. For example, use a local Redis container in run mode and an Azure Managed Redis resource when publishing:

AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.ExecutionContext.IsRunMode
? builder.AddRedis("cache")
: builder.AddAzureManagedRedis("cache");
builder.AddProject<Projects.Api>("api")
.WithReference(cache);
builder.Build().Run();

In a CI/CD pipeline, you typically pass the environment name as part of the deployment command. Many CI systems have their own environment concepts that map naturally to Aspire environments.

GitHub Environments provide scoped secrets and protection rules. Map them to Aspire environments by passing the environment name to the CLI:

.github/workflows/deploy.yml
jobs:
deploy-staging:
runs-on: ubuntu-latest
environment: staging
steps:
- uses: actions/checkout@v4
- name: Deploy to staging
run: aspire deploy --environment staging
env:
Parameters__apiKey: ${{ secrets.API_KEY }}
deploy-production:
runs-on: ubuntu-latest
environment: production
needs: deploy-staging
steps:
- uses: actions/checkout@v4
- name: Deploy to production
run: aspire deploy --environment production
env:
Parameters__apiKey: ${{ secrets.API_KEY }}

With this approach:

  • Each GitHub Environment scopes its own secrets (the API_KEY secret has different values in staging and production).
  • Protection rules on the production environment can require approvals before deployment.
  • The --environment flag ensures Aspire uses the correct deployment state cache and passes the environment name to your AppHost.

For workflow-first guidance, see CI/CD overview. For a worked GitHub Actions example, see Example app lifecycle workflow. For GitHub workflow guidance that complements this page, see Deployment state caching.