|
| 1 | +--- |
| 2 | +title: Deploy a static web app with Azure Static Web Apps CLI |
| 3 | +description: Deploy a static web app with Azure Static Web Apps CLI |
| 4 | +services: static-web-apps |
| 5 | +author: craigshoemaker |
| 6 | +ms.service: static-web-apps |
| 7 | +ms.topic: conceptual |
| 8 | +ms.date: 09/30/2022 |
| 9 | +ms.author: cshoe |
| 10 | +--- |
| 11 | + |
| 12 | +# Deploy a static web app with Azure Static Web Apps CLI |
| 13 | + |
| 14 | +The `deploy` command deploys the current project to Azure Static Web Apps. |
| 15 | + |
| 16 | +Some common use cases include: |
| 17 | + |
| 18 | +- Deploy a front-end app without an API |
| 19 | +- Deploy a front-end app with an API |
| 20 | +- Deploy a Blazor app |
| 21 | + |
| 22 | +## Deployment token |
| 23 | + |
| 24 | +The SWA CLI supports deploying using a deployment token. This is usually useful when deploying from a CI/CD environment. You can get a deployment token either from: |
| 25 | + |
| 26 | +- The [Azure portal](https://portal.azure.com/): **Home → Static Web App → Your Instance → Overview → Manage deployment token** |
| 27 | + |
| 28 | +- If you are using the [Azure CLI](https://aka.ms/azcli), you can get the deployment token of your project using the following command: |
| 29 | + |
| 30 | +```azstatic-cli |
| 31 | +az staticwebapp secrets list --name <APPLICATION_NAME> --query "properties.apiKey" |
| 32 | +``` |
| 33 | + |
| 34 | +- If you are using the Azure Static Web Apps CLI, you can use the following command: |
| 35 | + |
| 36 | +```azstatic-cli |
| 37 | +swa deploy --print-token |
| 38 | +``` |
| 39 | + |
| 40 | +You can then use that value with the `--deployment-token <token>` or you can create an environment variable called `SWA_CLI_DEPLOYMENT_TOKEN` and set it to the deployment token. |
| 41 | + |
| 42 | +> [!IMPORTANT] |
| 43 | +> Don't store deployment tokens in a public repository. |
| 44 | +
|
| 45 | +## Deploy a front-end app without an API |
| 46 | + |
| 47 | +You can deploy a front-end application without an API to Azure Static Web Apps by running the following steps: |
| 48 | + |
| 49 | +If your front-end application requires a build step, run `swa build` or refer to your application build instructions. |
| 50 | + |
| 51 | +* **Option 1:** From build folder you would like to deploy, run the deploy command: |
| 52 | + |
| 53 | + ```azstatic-cli |
| 54 | + cd build/ |
| 55 | + swa deploy |
| 56 | + ``` |
| 57 | +
|
| 58 | + > [!NOTE] |
| 59 | + > The *build* folder must contain the static content of your app to be deployed. |
| 60 | +
|
| 61 | +* **Option 2:** You can also deploy a specific folder: |
| 62 | +
|
| 63 | + 1. If your front-end application requires a build step, run `swa build` or refer to your application build instructions. |
| 64 | +
|
| 65 | + 2. Deploy your app: |
| 66 | +
|
| 67 | + ```azstatic-cli |
| 68 | + swa deploy ./my-dist |
| 69 | + ``` |
| 70 | +
|
| 71 | +## Deploy a front-end app with an API |
| 72 | +
|
| 73 | +To deploy both the front-end app and an API to Azure Static Web Apps, use the following steps. |
| 74 | +
|
| 75 | +1. If your front-end application requires a build step, run `swa build` or refer to your application build instructions. |
| 76 | +
|
| 77 | +2. Make sure the API language runtime version in the *staticwebapp.config.json* file is set correctly, for example: |
| 78 | +
|
| 79 | +```json |
| 80 | +{ |
| 81 | + "platform": { |
| 82 | + "apiRuntime": "node:16" |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +> [!NOTE] |
| 88 | +> If your project doesn't have the *staticwebapp.config.json* file, add one under your `outputLocation` folder. |
| 89 | +
|
| 90 | +3. Deploy your app: |
| 91 | + |
| 92 | +```azstatic-cli |
| 93 | +swa deploy ./my-dist --api-location ./api |
| 94 | +``` |
| 95 | + |
| 96 | +### Deploy a Blazor app |
| 97 | + |
| 98 | +To deploy a Blazor app with an API to Azure Static Web Apps, use the following steps: |
| 99 | + |
| 100 | +1. Build your Blazor app in **Release** mode: |
| 101 | + |
| 102 | +```azstatic-cli |
| 103 | +dotnet publish -c Release -o bin/publish |
| 104 | +``` |
| 105 | + |
| 106 | +2. From the root of your project, run the deploy command: |
| 107 | + |
| 108 | +```azstatic-cli |
| 109 | +swa deploy ./bin/publish/wwwroot --api-location ./Api |
| 110 | +``` |
| 111 | + |
| 112 | +## Deploy using the `swa-cli.config.json` |
| 113 | + |
| 114 | +> [!NOTE] |
| 115 | +> The path for `outputLocation` must be relative to the `appLocation`. |
| 116 | +
|
| 117 | +If you are using a [`swa-cli.config.json`](./static-web-apps-cli-configuration.md) configuration file in your project and have a single configuration entry, for example: |
| 118 | + |
| 119 | +```json |
| 120 | +{ |
| 121 | + "configurations": { |
| 122 | + "my-app": { |
| 123 | + "appLocation": "./", |
| 124 | + "apiLocation": "api", |
| 125 | + "outputLocation": "frontend", |
| 126 | + "start": { |
| 127 | + "outputLocation": "frontend" |
| 128 | + }, |
| 129 | + "deploy": { |
| 130 | + "outputLocation": "frontend" |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +Then you can deploy your application by running the following steps: |
| 138 | + |
| 139 | +1. If your front-end application requires a build step, run `swa build` or refer to your application build instructions. |
| 140 | + |
| 141 | +2. Deploy your app: |
| 142 | + |
| 143 | +```azstatic-cli |
| 144 | +swa deploy |
| 145 | +``` |
| 146 | + |
| 147 | +If you have multiple configuration entries, you can provide the entry ID to specify which one to use: |
| 148 | + |
| 149 | +```azstatic-cli |
| 150 | +swa deploy my-otherapp |
| 151 | +``` |
| 152 | + |
| 153 | +## Options |
| 154 | + |
| 155 | +Here are the options you can use with `swa deploy`: |
| 156 | + |
| 157 | +- `-a, --app-location <path>`: the folder containing the source code of the front-end application (default: "`.`") |
| 158 | +- `-i, --api-location <path>`: the folder containing the source code of the API application |
| 159 | +- `-O, --output-location <path>`: the folder containing the built source of the front-end application. The path is relative to `--app-location` (default: "`.`") |
| 160 | +- `-w, --swa-config-location <swaConfigLocation>`: the directory where the staticwebapp.config.json file is located |
| 161 | +- `-d, --deployment-token <secret>`: the secret token used to authenticate with the Static Web Apps |
| 162 | +- `-dr, --dry-run`: simulate a deploy process without actually running it (default: `false`) |
| 163 | +- `-pt, --print-token`: print the deployment token (default: `false`) |
| 164 | +- `--env [environment]`: the type of deployment environment where to deploy the project (default: "`preview`") |
| 165 | +- `-S, --subscription-id <subscriptionId>`: Azure subscription ID used by this project (default: `process.env.AZURE_SUBSCRIPTION_ID`) |
| 166 | +- `-R, --resource-group <resourceGroupName>`: Azure resource group used by this project |
| 167 | +- `-T, --tenant-id <tenantId>`: Azure tenant ID (default: `process.env.AZURE_TENANT_ID`) |
| 168 | +- `-C, --client-id <clientId>`: Azure client ID |
| 169 | +- `-CS, --client-secret <clientSecret>`: Azure client secret |
| 170 | +- `-n, --app-name <appName>`: Azure Static Web App application name |
| 171 | +- `-cc, --clear-credentials`: clear persisted credentials before login (default: `false`) |
| 172 | +- `-u, --use-keychain`: enable using the operating system native keychain for persistent credentials (default: `true`) |
| 173 | +- `-nu, --no-use-keychain`: disable using the operating system native keychain |
| 174 | +- `-h, --help`: display help for command |
| 175 | + |
| 176 | +## Usage |
| 177 | + |
| 178 | +Deploy using a deployment token. |
| 179 | + |
| 180 | +```azstatic-cli |
| 181 | +swa deploy ./dist/ --api-location ./api/ --deployment-token <TOKEN> |
| 182 | +``` |
| 183 | + |
| 184 | +Deploy using a deployment token from the environment variables. |
| 185 | + |
| 186 | +```azstatic-cli |
| 187 | +SWA_CLI_DEPLOYMENT_TOKEN=123 swa deploy ./dist/ --api-location ./api/ |
| 188 | +``` |
| 189 | + |
| 190 | +Deploy using `swa-cli.config.json` file |
| 191 | + |
| 192 | +```azstatic-cli |
| 193 | +swa deploy |
| 194 | +swa deploy myconfig |
| 195 | +``` |
| 196 | + |
| 197 | +Print the deployment token. |
| 198 | + |
| 199 | +```azstatic-cli |
| 200 | +swa deploy --print-token |
| 201 | +``` |
| 202 | + |
| 203 | +Deploy to a specific environment. |
| 204 | + |
| 205 | +```azstatic-cli |
| 206 | +swa deploy --env production |
| 207 | +``` |
0 commit comments