|
| 1 | +--- |
| 2 | +title: Deployment state caching |
| 3 | +description: Learn how the aspire deploy command manages deployment state through cached configuration files. |
| 4 | +ms.date: 10/17/2025 |
| 5 | +ai-usage: ai-assisted |
| 6 | +--- |
| 7 | + |
| 8 | +# Deployment state caching |
| 9 | + |
| 10 | +The `aspire deploy` command manages deployment state through cached configuration files stored locally on your machine. This caching mechanism streamlines repeated deployments by preserving provisioning settings and parameters, making subsequent deployments faster and more efficient. |
| 11 | + |
| 12 | +## Default behavior |
| 13 | + |
| 14 | +The `aspire deploy` command automatically manages deployment state based on whether cached configuration exists for your application and target environment. |
| 15 | + |
| 16 | +### First deployment |
| 17 | + |
| 18 | +When you run `aspire deploy` for the first time, or for the first time in a target `--environment`, the command: |
| 19 | + |
| 20 | +1. Prompts for provisioning information (subscription ID, resource group name, location). |
| 21 | +1. Prompts for deployment parameters (for example, API keys, connection strings). |
| 22 | +1. Initiates the deployment process. |
| 23 | +1. Saves all prompted values and deployment state to `~/.aspire/deployments/{AppHostSha}/production.json`. |
| 24 | + |
| 25 | +### Subsequent deployments |
| 26 | + |
| 27 | +On subsequent `aspire deploy` executions, the command: |
| 28 | + |
| 29 | +1. Detects the existing deployment state file at _~/.aspire/deployments/{AppHostSha}/production.json_. |
| 30 | +1. Notifies you that settings will be read from the cached file. |
| 31 | +1. Prompts for confirmation to load the cached settings. |
| 32 | +1. Loads the configuration from the cached file into the configuration provider. |
| 33 | +1. Proceeds with deployment using the cached values (no re-prompting). |
| 34 | + |
| 35 | +## Environment-specific deployments |
| 36 | + |
| 37 | +Different deployment environments (such as development, staging, and production) typically require different configurations, resource names, and connection strings. The `aspire deploy` command supports environment-specific deployments, ensuring that each environment maintains isolated deployment state. |
| 38 | + |
| 39 | +### Specify an environment |
| 40 | + |
| 41 | +Use the `--environment` flag to deploy to different environments: |
| 42 | + |
| 43 | +```Aspire |
| 44 | +aspire deploy --environment staging |
| 45 | +``` |
| 46 | + |
| 47 | +**First deployment to a specific environment:** |
| 48 | + |
| 49 | +- Prompts for all provisioning and parameter information. |
| 50 | +- Saves deployment state to _~/.aspire/deployments/{AppHostSha}/{environment}.json_ (for example, _staging.json_). |
| 51 | + |
| 52 | +**Subsequent deployments:** |
| 53 | + |
| 54 | +- Reads the environment-specific cached file. |
| 55 | +- Loads configuration from the cached state. |
| 56 | +- Uses cached values without prompting. |
| 57 | + |
| 58 | +### Environment variable support |
| 59 | + |
| 60 | +The deployment environment can also be specified using the `DOTNET_ENVIRONMENT` environment variable: |
| 61 | + |
| 62 | +```bash |
| 63 | +export DOTNET_ENVIRONMENT=staging && aspire deploy |
| 64 | +``` |
| 65 | + |
| 66 | +This behaves identically to using the `--environment` flag, loading the appropriate cached configuration file. |
| 67 | + |
| 68 | +## Cache management |
| 69 | + |
| 70 | +The `aspire deploy` command provides mechanisms to manage cached deployment state, giving you control over when to use cached values and when to start fresh. |
| 71 | + |
| 72 | +### Clear the cache |
| 73 | + |
| 74 | +Use the `--clear-cache` flag to reset deployment state: |
| 75 | + |
| 76 | +```Aspire |
| 77 | +aspire deploy --clear-cache |
| 78 | +``` |
| 79 | + |
| 80 | +**Behavior:** |
| 81 | + |
| 82 | +1. Prompts for confirmation before deleting the cache for the specified environment. |
| 83 | +1. Deletes the environment-specific deployment state file (for example, _~/.aspire/deployments/{AppHostSha}/production.json_). |
| 84 | +1. Prompts for all provisioning and parameter information as if deploying for the first time. |
| 85 | +1. Proceeds with deployment. |
| 86 | +1. **Does not save the prompted values** to cache. |
| 87 | + |
| 88 | +### Environment-specific cache clearing |
| 89 | + |
| 90 | +The `--clear-cache` flag respects the environment context: |
| 91 | + |
| 92 | +```Aspire |
| 93 | +aspire deploy --environment staging --clear-cache |
| 94 | +``` |
| 95 | + |
| 96 | +This clears only the _staging.json_ cache file while leaving other environment caches (like _production.json_) intact. |
| 97 | + |
| 98 | +## File storage location |
| 99 | + |
| 100 | +- **Path pattern:** _~/.aspire/deployments/{AppHostSha}/{environment}.json_. |
| 101 | +- **Default environment:** `production`. |
| 102 | +- **AppHostSha:** A hash value representing the application host configuration, ensuring deployment states are specific to each application configuration. |
| 103 | + |
| 104 | +## Use deployment state in CI/CD pipelines |
| 105 | + |
| 106 | +When using the `aspire deploy` command in continuous integration and deployment (CI/CD) pipelines, you might want to persist deployment state across pipeline runs. This approach can be useful for maintaining consistent deployment configurations without manual intervention. |
| 107 | + |
| 108 | +### GitHub Actions example |
| 109 | + |
| 110 | +The following example demonstrates how to cache deployment state in a GitHub Actions workflow using the `actions/cache` action: |
| 111 | + |
| 112 | +```yaml |
| 113 | +name: Deploy to Azure |
| 114 | + |
| 115 | +on: |
| 116 | + push: |
| 117 | + branches: [ main ] |
| 118 | + |
| 119 | +jobs: |
| 120 | + deploy: |
| 121 | + runs-on: ubuntu-latest |
| 122 | + steps: |
| 123 | + - uses: actions/checkout@v4 |
| 124 | + |
| 125 | + - name: Cache Aspire deployment state |
| 126 | + uses: actions/cache@v4 |
| 127 | + with: |
| 128 | + path: ~/.aspire/deployments |
| 129 | + key: aspire-deploy-${{ hashFiles('**/AppHost.csproj') }}-${{ github.ref }} |
| 130 | + restore-keys: | |
| 131 | + aspire-deploy-${{ hashFiles('**/AppHost.csproj') }}- |
| 132 | + aspire-deploy- |
| 133 | + |
| 134 | + - name: Deploy with Aspire CLI |
| 135 | + run: aspire deploy --environment production |
| 136 | + env: |
| 137 | + AZURE_CREDENTIALS: ${{ secrets.AZURE_CREDENTIALS }} |
| 138 | +``` |
| 139 | +
|
| 140 | +This workflow caches the _~/.aspire/deployments_ directory, using the AppHost project file hash and branch reference as cache keys. The `actions/cache` action automatically restores the cache before the deployment step and saves any updates to the cache after the job completes. Subsequent workflow runs restore the cached deployment state, allowing automated deployments without re-prompting for configuration values. |
| 141 | + |
| 142 | +> [!CAUTION] |
| 143 | +> When caching deployment state in CI/CD pipelines, ensure that your pipeline has appropriate access controls and secret management practices in place, as the cached state might contain sensitive configuration values. |
| 144 | + |
| 145 | +## Security considerations |
| 146 | + |
| 147 | +The deployment state files are stored locally on your machine in the _~/.aspire/deployments_ directory. These files contain provisioning settings and parameter values, including secrets that might be associated with parameter resources. The `aspire deploy` command follows the same security pattern as .NET's user secrets manager: |
| 148 | + |
| 149 | +- Files are stored outside of source code to mitigate against accidental secret leaks in version control. |
| 150 | +- Secrets are stored in plain text in the local file system. |
| 151 | +- Any process running under your user account can access these files. |
| 152 | + |
| 153 | +Consider these security best practices: |
| 154 | + |
| 155 | +- Ensure your local machine has appropriate security measures in place. |
| 156 | +- Be cautious when sharing or backing up files from the _~/.aspire/deployments_ directory. |
| 157 | +- Use the `--clear-cache` flag when you need to change sensitive parameter values. |
| 158 | + |
| 159 | +## Key points |
| 160 | + |
| 161 | +- Each environment maintains its own isolated deployment state. |
| 162 | +- Cached values persist across deployments unless explicitly cleared. |
| 163 | +- The `--clear-cache` flag performs a one-time deployment without persisting new values. |
| 164 | +- Environment selection can be specified via flag or environment variable. |
| 165 | +- You're prompted for confirmation when loading cached settings. |
| 166 | +- Cache files are stored per application (via AppHostSha) and per environment. |
| 167 | + |
| 168 | +## See also |
| 169 | + |
| 170 | +- [aspire deploy command reference](../cli-reference/aspire-deploy.md) |
| 171 | +- [Deploy to Azure Container Apps using Aspire CLI](aspire-deploy/aca-deployment-aspire-cli.md) |
0 commit comments