Deploy Code to Azure App Service
This document provides a comprehensive guide on how to deploy your application code to Azure App Service, a fully managed platform for building, deploying, and scaling web apps and APIs.
Introduction to Deployment Options
Azure App Service supports a variety of deployment methods, catering to different development workflows and preferences. You can deploy code from local sources, cloud repositories, or automated CI/CD pipelines.
1. Deploying from a Local Git Repository
If you are using Git for version control locally, you can configure App Service to be a remote repository. This allows you to push your code directly to App Service.
- In the Azure portal, navigate to your App Service.
- Under "Deployment", select "Deployment Center".
- Choose "Local Git" as your source.
- Follow the instructions to set up your Git credentials and configure the local Git remote.
- Use the following commands in your local Git Bash or terminal:
git remote add azure <Your-App-Service-Git-URL> git push azure main
2. Deploying from GitHub or Azure Repos
Integrate directly with your GitHub or Azure Repos repositories for continuous deployment. App Service can monitor your repository and automatically deploy new commits.
Configure a GitHub Actions workflow to build and deploy your application. Here's a simplified example for Node.js:
name: Node.js Deploy to Azure
on:
push:
branches: [ main ]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Node.js version
uses: actions/setup-node@v1
with:
node-version: '14.x'
- name: npm install, build, and test
run: |
npm install
npm run build --if-present
npm test
- name: Azure WebApp Deploy
uses: azure/webapps-deploy@v2
with:
app-name: 'YourAppName'
package: '.'
slot-name: 'production'
env:
AZURE_CREDENTIALS: ${{ secrets.AZURE_CREDENTIALS }}
To set up this integration in the Azure portal:
- Navigate to your App Service and select "Deployment Center".
- Choose "GitHub" or "Azure Repos" as your source.
- Authorize the connection to your repository and select the branch to deploy from.
- Configure build settings if necessary.
3. Deploying using Zip Deploy
For scenarios where you have a pre-built deployment package (e.g., a ZIP archive), you can use the Zip Deploy feature.
- Azure CLI:
az webapp deploy --resource-group <YourResourceGroup> --name <YourAppName> --src-path <PathToYourZipFile> --type zip
- REST API: You can also use the App Service Deployment REST API.
4. Continuous Deployment from Cloud Sources
App Service offers built-in integrations with popular cloud storage services like Dropbox and OneDrive. This allows you to synchronize your deployment package directly from these services.
- In the Deployment Center, select "Cloud Storage".
- Choose your preferred cloud provider and follow the authentication steps.
- Configure the folder or file to deploy from.
Deployment Slots
Deployment slots are a powerful feature for managing your deployments. They allow you to deploy your application to a staging environment, test it thoroughly, and then swap it into production with zero downtime.
- Navigate to "Deployment Slots" in your App Service blade.
- Click "Add Slot" to create a new staging environment.
- Deploy your code to the staging slot.
- Once satisfied, click "Swap" to exchange the staging slot with the production slot.
Troubleshooting Deployments
If you encounter issues during deployment, consider the following:
- Check the deployment logs in the "Deployment Center".
- Verify your application's dependencies and build process.
- Ensure you have the correct file structure in your deployment package.
- Review the App Service logs for runtime errors.