- An Azure subscription. If you don't have one, you can create a free Azure account.
- A React application built for production (using
npm run buildoryarn build). - Node.js and npm (or yarn) installed. You can download Node.js from nodejs.org.
-
Create a Static Web App in Azure:
- Navigate to the Azure portal (https://portal.azure.com).
- Search for "Static Web Apps" in the search bar and select it.
- Click "Create" to create a new Static Web App. You'll need to provide details like the subscription, resource group, and name for your app. Important: You do not need to connect a repository at this stage if you are deploying manually.
-
Create
swa-cli.config.json:- Create a file named
swa-cli.config.jsonin the root directory of your React project (alongsidepackage.json). - Paste the following configuration into the file, replacing
<static-app name>with the exact name of your Static Web App in Azure:
{ "$schema": "https://aka.ms/azure/static-web-apps-cli/schema", "configurations": { "<static-app name>": { "appDir": "build", "outputLocation": "build" } } }- Explanation:
$schema: Points to the schema for validation.configurations: Contains the configuration for your static web app.<static-app name>: The name of your static web app in Azure. This must match exactly.appDir: Specifies the directory containing your built application files (usuallybuildafter runningnpm run build).outputLocation: Should match theappDirin this case. This is where the built files will be located.
- Create a file named
-
Create
staticwebapp.config.json:- Create a file named
staticwebapp.config.jsonin the root directory of your React project (alongsidepackage.jsonandswa-cli.config.json). - Paste the following configuration into the file:
{ "navigationFallback": { "rewrite": "/index.html" } }- Explanation:
navigationFallback: Tells Azure Static Web Apps to serveindex.htmlfor requests that don't match static files (CSS, JavaScript, images). This is how client-side routing works.
- Create a file named
-
Install the Azure Static Web Apps CLI:
- Open your terminal in the root directory of your React project.
- Run the following command to install the CLI globally:
npm install -g @azure/static-web-apps-cli
-
Verify Installation:
- Run the following command to check the CLI version:
swa --version
-
Get the Deployment Token:
- In the Azure portal, navigate to your Static Web App.
- In the left-hand menu, under "Settings," find "Deployment credentials."
- Copy the "Manage deployment token" value. Keep this token secure! Treat it like a password.
-
Deploy the Application:
- In your terminal, navigate to the root directory of your React project.
- Run the following command, replacing
<token>with the deployment token you copied and<static-app name>with the name of your static web app in Azure:
swa deploy --deployment-token "<token>" --config-name "<static-app name>" --env production
- If you do not set
--envit will deploy in preview mode.
-
Access Your Application:
- Once the deployment is complete, the CLI will output the URL of your deployed application. You can also find this URL in the Azure portal on the overview page of your Static Web App.
swacommand not found: Ensure Node.js and npm (or yarn) are installed correctly, and that you installed the@azure/static-web-apps-cliglobally.- Deployment errors: Double-check the
<static-app name>in yourswa-cli.config.jsonfile and theswa deploycommand. Verify the deployment token is correct. Examine the output in the terminal for specific error messages.
You can also use the following deployment script to automate the process:
#!/bin/bash
# Function to check if a command exists
dependencies=("node" "npm" "swa")
check_dependency() {
if ! command -v "$1" &>/dev/null; then
echo "Error: $1 is not installed. Please install it before running this script."
exit 1
fi
}
# Check dependencies
for dep in "${dependencies[@]}"; do
check_dependency "$dep"
done
# Ensure Azure Static Web Apps CLI is installed
if ! npm list -g @azure/static-web-apps-cli &>/dev/null; then
echo "Installing Azure Static Web Apps CLI..."
npm install -g @azure/static-web-apps-cli
fi
# Get user input for Static Web App details
echo "Enter your Azure Static Web App name:"
read STATIC_APP_NAME
if [ -z "$STATIC_APP_NAME" ]; then
echo "Error: Static Web App name cannot be empty."
exit 1
fi
# Get the deployment token
echo "Enter your Azure Static Web App deployment token (paste and press Enter):"
read DEPLOYMENT_TOKEN
if [ -z "$DEPLOYMENT_TOKEN" ]; then
echo "Error: Deployment token cannot be empty."
exit 1
fi
# Build the React app
echo "Building the React application..."
npm install && npm run build
if [ $? -ne 0 ]; then
echo "Error: React app build failed. Check the logs above."
exit 1
fi
# Check and create swa-cli.config.json if it doesn't exist
if [ ! -f swa-cli.config.json ]; then
cat > swa-cli.config.json <<EOL
{
"$schema": "https://aka.ms/azure/static-web-apps-cli/schema",
"configurations": {
"$STATIC_APP_NAME": {
"appDir": "build",
"outputLocation": "build"
}
}
}
EOL
echo "swa-cli.config.json created successfully."
else
echo "swa-cli.config.json already exists. Skipping creation."
fi
# Check and create staticwebapp.config.json if it doesn't exist
if [ ! -f staticwebapp.config.json ]; then
cat > staticwebapp.config.json <<EOL
{
"navigationFallback": {
"rewrite": "/index.html"
}
}
EOL
echo "staticwebapp.config.json created successfully."
else
echo "staticwebapp.config.json already exists. Skipping creation."
fi
# Deploy the application
echo "Deploying to Azure Static Web Apps..."
swa deploy --deployment-token "$DEPLOYMENT_TOKEN" --config-name "$STATIC_APP_NAME" --env production
if [ $? -eq 0 ]; then
echo "Deployment successful!"
else
echo "Deployment failed. Check the error messages above."
exit 1
fiThis script will:
- Verify dependencies are installed.
- Build your React app.
- Check for existing configuration files or create them.
- Deploy the app to Azure Static Web Apps using your deployment token.
To run the script using npm run build-and-deploy, you can add the following "build-and-deploy" script to your package.json:
{
"scripts": {
....
....
"build-and-deploy": "bash deploy.sh",
....
....
}
}This way, when you run:
npm run build-and-deployIt will first execute the build script (which builds your React app) and then run the deploy.sh script to deploy your app to Azure Static Web Apps. Make sure the deploy.sh script is in the root directory of your project.