Core Spark (Azuredevopswiki)

Azuredevopswiki

An Azure DevOps wiki helper

Write a wiki article called "Deploying ASP.NET Core Applications to Azure" Include a Step-by-step guide on deploying ASP.NET Core web applications to Azure. Discusses various deployment methods and best practices for maintaining uptime and performance.


Responses From Spark Variants that Implement the Core Spark (AzureDevOpsWiki)

[[_TOC_]]

# Deploying ASP.NET Core Applications to Azure

This article provides a step-by-step guide on deploying ASP.NET Core web applications to Azure. We will discuss various deployment methods and best practices for maintaining uptime and performance.

## Prerequisites

- An active Azure subscription.
- The latest version of Visual Studio or Visual Studio Code.
- An ASP.NET Core web application.

## Deployment Methods

### Option 1: Deploy via Visual Studio

1. **Open your ASP.NET Core project in Visual Studio.**
2. **Right-click on the project in Solution Explorer and select 'Publish'.**
3. **Select Azure as the publish target.**
4. **Choose 'Azure App Service (Windows)' or 'Azure App Service (Linux)' depending on your preference.**
5. **Follow the prompts to sign in to your Azure account and select your subscription.**
6. **Create a new App Service or select an existing one.**
7. **Configure the App Service settings and click 'Create'.**
8. **Click 'Publish' to deploy your application.**

### Option 2: Deploy via Azure CLI

1. **Open the terminal or command prompt.**
2. **Login to your Azure account:**
   ```shell
   az login
   ```
3. **Create a resource group:**
   ```shell
   az group create --name myResourceGroup --location "East US"
   ```
4. **Create an App Service plan:**
   ```shell
   az appservice plan create --name myAppServicePlan --resource-group myResourceGroup --sku B1
   ```
5. **Create a web app:**
   ```shell
   az webapp create --name myUniqueAppName --plan myAppServicePlan --resource-group myResourceGroup
   ```
6. **Deploy your application using Git:**
   ```shell
   az webapp deployment source config-local-git --name myUniqueAppName --resource-group myResourceGroup
   git remote add azure <deploymentLocalGitUrl>
   git push azure master
   ```

### Option 3: Deploy via GitHub Actions

1. **Create a GitHub repository for your ASP.NET Core application.**
2. **Push your code to the repository.**
3. **Navigate to the 'Actions' tab in your GitHub repository.**
4. **Click 'Set up a workflow yourself' or choose a suggested workflow.**
5. **Add the following YAML configuration to deploy to Azure:**
   ```yaml
   name: Build and Deploy ASP.NET Core App to Azure

   on:
     push:
       branches:
         - main

   jobs:
     build-and-deploy:
       runs-on: ubuntu-latest
   
       steps:
       - name: Checkout code
         uses: actions/checkout@v2
   
       - name: Setup .NET Core
         uses: actions/setup-dotnet@v1
         with:
           dotnet-version: '5.0.x'
   
       - name: Build
         run: dotnet build --configuration Release
   
       - name: Publish
         run: dotnet publish -c Release -o ./publish
   
       - name: Deploy to Azure Web App
         uses: azure/webapps-deploy@v2
         with:
           app-name: 'myUniqueAppName'
           slot-name: 'production'
           publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
           package: ./publish
   ```
6. **Commit and push the workflow file to GitHub.**

## Best Practices

- **Use Deployment Slots:** Use deployment slots to reduce downtime during deployment. You can test your application in a staging slot before swapping it with the production slot.
- **Monitor Performance:** Use Azure Application Insights to monitor the performance and health of your application.
- **Auto-Scaling:** Configure auto-scaling to handle varying loads and ensure your application remains responsive.
- **Secure Your Application:** Use Azure Key Vault to manage your application secrets securely.
- **Backup:** Regularly backup your application and database to prevent data loss.

## Conclusion

Deploying ASP.NET Core applications to Azure can be done in multiple ways, each with its own benefits. Choose the method that best suits your workflow and requirements. Following best practices will help maintain uptime and ensure optimal performance.