Introduction
In this article, we will explore the process of deploying an ASP.NET MVC application to Azure Cloud using Azure DevOps and GitHub. This approach ensures a seamless continuous integration and continuous deployment (CI/CD) pipeline, facilitating an efficient and robust development workflow. By the end of this chapter, you will have a comprehensive understanding of setting up an end-to-end deployment pipeline from GitHub to Azure using Azure DevOps Classic Editor.
Step 1. Prerequisites
Before we dive into the steps, let's ensure you have the necessary prerequisites.
- Azure Subscription: An active Azure subscription is required to deploy your application.
- Azure DevOps Account: You need an Azure DevOps account to create and manage the CI/CD pipeline.
- GitHub Repository: Your ASP.NET MVC application should be hosted on GitHub.
- Basic Knowledge of ASP.NET MVC: Familiarity with ASP.NET MVC will help you follow along with the steps.
- Azure App Service: Set up an Azure App Service where your application will be deployed.
Step 2. Creating an Azure App Service
Azure App Service is a fully managed platform for building, deploying, and scaling web apps. Here's how to create one:
- Log in to Azure Portal: Navigate to Azure Portal.
- Create a New Resource: Click on "Create a resource" in the left-hand menu and select "Web App" under the "Compute" section.
- Configure Web App
- Subscription: Select your Azure subscription.
- Resource Group: Create a new resource group or use an existing one.
- Name: Provide a unique name for your web app, which will be part of its URL (e.g., yourappname.azurewebsites.net).
- Publish: Choose "Code" since you are deploying an ASP.NET MVC application.
- Runtime stack: Select ".NET Core" and the appropriate version for your application.
- Operating System: Choose "Windows".
- Region: Select a region close to your user base to reduce latency.
- App Service Plan: Create a new plan or select an existing one. The app service plan determines pricing and available features.
- Review and Create: Click "Review + Create" to review your settings and then "Create" to create the web app.
Create the Azure Web app and fill in all the above details.
Fill in the details and then click next.
Step 3. Setting Up an Azure DevOps Project
Azure DevOps is a comprehensive suite of development tools that facilitate CI/CD. Follow these steps to set up your project:
- Log in to Azure DevOps: Navigate to Azure DevOps.
- Create a new project
- Click on the "New Project" button.
- Project Name: Enter a name for your project.
- Description: Optionally, add a description to describe the project's purpose.
- Visibility: Choose either public or private based on your preference.
- Create Project: Click "Create" to finalize the project setup.
Step 4. Connecting Azure DevOps to GitHub
To link your Azure DevOps project with your GitHub repository.
- Go to Repos: In your Azure DevOps project, navigate to "Repos" from the left-hand menu.
- Import Repository
- Click on "Import a repository" at the top right.
- Clone URL: Provide the URL of your GitHub repository.
- Authentication: Enter your GitHub credentials if necessary to access the repository.
- Import: Click "Import" to create a copy of your GitHub repository in Azure DevOps.
Step 5. Creating a Build Pipeline
A build pipeline automates the process of building and testing your application. Here’s how to create one.
- Go to Pipelines: In Azure DevOps, navigate to "Pipelines" and then "Builds".
- New Pipeline: Click on "New pipeline" and select "Use the classic editor".
- Select Repository: Choose your GitHub repository.
- Source: Select "GitHub".
- Repository: Choose your repository from the list or provide the URL.
- Select a Template: Choose the ASP.NET Core template, which includes tasks for restoring, building, and publishing your application.
- Configure Build Pipeline
- Agent Pool: Choose "Azure Pipelines" for Microsoft's hosted agents.
- Restore: Use the "NuGet restore" task to restore NuGet packages.
- Task Name: NuGet restore
- Path to the solution: Provide the path to your solution file (e.g., **/*.sln).
- Build: Add a build task to compile your solution.
- Task Name: .NET Core
- Command: Build
- Projects: Provide the path to your project file (e.g., **/*.csproj).
- Test: Optionally, add a task to run tests.
- Task Name: .NET Core
- Command: Test
- Projects: Provide the path to your test project file (e.g., **/*Tests/*.csproj).
- Publish: Use the "Publish Build Artifacts" task to publish the build outputs.
- Task Name: Publish and build artifacts
- Path to Publish: Specify the path to the directory containing your build outputs (e.g., $(Build.ArtifactStagingDirectory)).
- Artifact Name: Name the artifact (e.g., drop).
- Save and Queue: Save the pipeline and queue a new build to test the setup.
Step 6. Creating a Release Pipeline
A release pipeline automates the process of deploying your application to various environments. Here’s how to set it up.
- Go to Releases: In Azure DevOps, navigate to "Pipelines" and then "Releases".
- New Pipeline: Click on "New pipeline" and select "Empty job".
- Add Artifacts: Link the artifacts from the build pipeline.
- Source Type: Build
- Project: Select your project.
- Source: Choose the build pipeline created earlier.
- Default Version: Select "Latest".
- Add a Stage: Add a new stage and name it "Deployment".
- Stage Name: Provide a name for the stage (e.g., Dev).
- Add Deployment Task
- Click on "1 job, 0 task" under your stage.
- Click the "+" icon to add a task and search for "Azure App Service deploy".
- Azure Subscription: Authorize Azure DevOps to connect to your Azure subscription.
- App Service Name: Select the web app you created earlier.
- Package or Folder: Point to the build artifacts (e.g., $(System.DefaultWorkingDirectory)/_your_build_artifact_name/drop).
- Configure Continuous Deployment: Enable continuous deployment triggers to automatically deploy on successful builds.
- Click on "Continuous deployment trigger" and enable it.
- Save and Create Release: Save the pipeline and create a release to test the deployment.
- Click "Create release" and select the appropriate stages.
- Click "Create" to initiate the release process.
Step 7. Monitoring and Verifying Deployment
Once the release pipeline is set up, it’s important to monitor and verify the deployment:
- Monitor Release: Go to "Releases" to track the deployment process.
- Check logs and deployment status to ensure the process is completed successfully.
- Verify Deployment: After deployment, visit the Azure Portal and navigate to your web app.
- Access the URL of your web app (e.g., https://yourappname.azurewebsites.net) to ensure it is running correctly.
- Monitor the release pipeline
Conclusion
In this article, you have learned how to set up an end-to-end CI/CD pipeline for an ASP.NET MVC application using Azure DevOps and GitHub. By following these steps, you can automate the process of building, testing, and deploying your application to Azure App Service, ensuring a streamlined and efficient development workflow. This setup not only enhances productivity but also ensures that your application is always up-to-date with the latest changes, providing a seamless experience for both developers and users.