Create and Test an ASP.NET Core App in Azure DevOps Pipelines

Rabiatu MohammedRabiatu Mohammed
11 min read

What is Azure DevOps?

Azure DevOps is Microsoft’s all-in-one platform for planning, developing, testing, and deploying software. It offers tools like Azure Repos for source control, Azure Pipelines for automating CI/CD, Azure Boards for project management, and more—all to help teams deliver better software faster.

In this guide, we’ll focus on Azure Pipelines to automate the build, test, and deployment of an ASP.NET Core application. Azure DevOps makes software delivery smoother and more efficient, whether you're working solo or in a team.

Step 1: Access Your Azure DevOps Organization

  1. Go to the Azure DevOps homepage.

  2. Click "My Azure DevOps Organizations" to view or manage your organization.

Step 2: Create Your Azure DevOps Organization

  1. On the Azure DevOps homepage, click Create new organization.

  2. Sign in with your Microsoft account if prompted.

  3. Enter a unique name for your organization (e.g., dev.azure.com/rabiatyusuf71).

  4. Choose your region (e.g., United Kingdom).

  5. Complete the CAPTCHA, then click Continue.

Step 3: Create a New Project

  1. Once your organization is set up, you’ll be prompted to create a new project.

  2. Enter a Project name (e.g., CI-CD-PROJECT).

  3. Select the Visibility:

    • Private: Only people you give access to can view this project.

    • Public: Available for anyone to view on the internet (may have limitations).

  4. Click Create project to complete the setup.

Step 4: Add Code to Your Repository

  1. In your Azure DevOps project, go to Repos in the left sidebar.

  2. To connect your local repository, copy the HTTPS or SSH URL.

  3. If you’re pushing an existing repository, run this command in your terminal.

  4. Push your code.

Step 5: Review VM Details in Azure Portal

To access and interact with Azure DevOps securely, we created a Linux virtual machine (VM) in Azure. This VM serves as a controlled environment from which we can manage Azure DevOps resources and workflows.

  1. In the Azure Portal, navigate to your VM's Overview tab to review essential details, including:

    • Resource Group: The group the VM is part of (e.g., LinuxVM).

    • Status: Current status (e.g., Running).

    • Location: Azure region hosting the VM (e.g., UK South (Zone 1)).

    • Operating System: Type and version (e.g., Linux Ubuntu 24.04).

    • Public IP Address: Use this IP address to connect to the VM and access Azure DevOps securely.

  2. Ensure your VM is running and accessible, as it will act as the primary interface for managing Azure DevOps.

Step 6: Connect to Your VM via SSH

To access and manage Azure DevOps securely from your VM, connect to it using SSH.

  1. Open a terminal on your local machine.

  2. Run the following command, replacing the IP address with your VM's public IP.

  3. If prompted, confirm the connection by typing yes.

  4. Enter your VM password when prompted.

You’re now connected to your VM and can use it to interact with Azure DevOps resources.

Step 7: Clone the Repository to Your VM

To work with code and resources needed for your Azure DevOps setup, clone the required repository onto your VM.

  1. In the SSH session on your VM, run the following command to clone the repository (replace with your repository URL if different).

    • -b unit-test specifies the branch to clone (in this case, unit-test).
  2. Once the cloning is complete, you’ll have the repository files available on your VM for further configuration or testing.

Step 8: Verify and Remove the Remote Origin

After cloning the repository, you may want to remove the default remote origin if you plan to link it to a different repository.

  1. List Current Remotes: Check the current remote repositories by running: git remote -v

    • This should display the current remote URL for both fetch and push.
  2. Remove the Remote Origin: Use the following command to remove the existing remote origin: git remote remove origin

  3. Verify Removal: Confirm that the remote has been removed by listing remotes again: git remote -v

You should see no output, indicating the origin has been removed successfully.

Step 9: Access Personal Access Tokens (PAT)

To authenticate with Azure DevOps from your VM or other tools, you’ll need a Personal Access Token (PAT).

  1. In Azure DevOps, go to the top-right corner and click on the User Settings icon (gear icon).

  2. From the dropdown menu, select Personal access tokens.

This will take you to the page where you can create and manage your tokens for secure access.

Step 10: Create a Personal Access Token (PAT)

  1. In the Personal Access Tokens section, click Create new token.

  2. Fill in the details:

    • Name: Enter a name for the token (e.g., rymaa).

    • Organization: Verify your organization (e.g., rabiatyusuf71).

    • Expiration: Set the expiration duration (e.g., 30 days).

    • Scopes: Select Full access to allow comprehensive access to Azure DevOps resources.

  3. Click Create to generate the token.

Once created, copy and securely store the token, as it will be required for authenticating with Azure DevOps.

Step 11: Set Up the Build Pipeline in Azure DevOps

  1. In your Azure DevOps project, go to Repos > Files to view the repository contents.

  2. Ensure the repository contains the necessary files, including the azure-pipelines.yml file, which defines your pipeline steps.

  3. Click on Set up build at the top right to initiate the pipeline setup process.

This setup will allow Azure DevOps to automatically follow the instructions in azure-pipelines.yml to build, test, and deploy your application.

Step 12: Select an Agent Pool for Your Pipeline

An agent pool is necessary to run your pipeline tasks. Azure DevOps provides a default agent pool for you to use.

  1. In your Azure DevOps project, go to Project Settings > Agent pools.

  2. Under Agent pools, confirm that the Default pool is available. This pool allows Azure-hosted agents to run your builds and deployments.

By using the default pool, your pipeline will have access to a range of agents suitable for building and testing applications.

Step 13: Set Up a Self-Hosted Agent on Linux

If you prefer to use a self-hosted agent instead of the default Azure-hosted agents, follow these steps to set up one on Linux.

  1. In Azure DevOps, go to Project Settings > Agent pools and click New agent.

  2. Select the Linux tab to get instructions for setting up the agent on a Linux machine.

  3. Download the agent by clicking the Download button.

  4. Open a terminal on your Linux machine and navigate to the download location.

  5. Run the following commands to install and configure the agent.

  6. Follow the prompts to complete the configuration.

Once configured, your self-hosted agent will be ready to execute pipeline jobs.

Step 14: Add Remote and Push Code to Azure DevOps

To link your local repository with Azure DevOps and push all branches, follow these steps:

  1. Add Remote: Run the following command to add Azure DevOps as a remote origin.

  2. Push All Branches: Use this command to push all branches to the remote origin.

After pushing, all branches, including unit-test, will be available in Azure DevOps for further integration and deployment.

Step 15: Download the Azure DevOps Agent

To set up a self-hosted agent for Azure DevOps, download the agent package on your Linux VM.

  1. Create a Downloads directory (if it doesn’t already exist) and navigate to it.

  2. Use wget to download the Azure DevOps agent package.

Once downloaded, you’ll be ready to extract and configure the agent.

Step 16: Extract the Azure DevOps Agent

After downloading the agent package, extract it to set up the agent files.

  1. Create a new directory for the agent and navigate to it.

  2. Extract the downloaded agent package.

This will unpack the necessary files into the MyAgent directory, preparing it for configuration.

Step 17: Configure the Azure DevOps Agent

After extracting the agent files, configure the agent to connect to your Azure DevOps organization.

  1. Run the configuration script.

  2. When prompted, accept the Team Explorer Everywhere license by entering y.

  3. Enter the following details as prompted:

    • Server URL: Enter your Azure DevOps organization URL (e.g., https://dev.azure.com/rabiatyusuf71).

    • Authentication Type: Press Enter to use PAT (Personal Access Token).

    • Personal Access Token: Enter your PAT and press Enter.

  4. For additional prompts:

    • Agent Pool: Press Enter to use the default pool.

    • Agent Name: Press Enter to use the default name or provide a custom name.

    • Work Folder: Press Enter to use the default _work folder.

Once configured, the agent is registered and ready to run pipeline jobs.

Step 18: Set Up the Pipeline Using a Template

  1. In Azure DevOps, navigate to Pipelines on the left sidebar.

  2. Click on Pipelines and then select New pipeline if you're creating a new one.

  3. Under the Choose a template section, find and select ASP.NET Core.

  4. Click Apply to use the ASP.NET Core template.

This template provides a preconfigured pipeline setup for building and testing an ASP.NET Core web application, making it easier to get started.

Step 19: Configure the ASP.NET Core Pipeline

  1. After applying the ASP.NET Core template, you’ll see the pipeline configuration screen.

  2. In the Agent pool dropdown, select Default to use the default Azure-hosted agent pool.

  3. Review the pipeline tasks included in the template:

    • Restore: Restores dependencies specified in the .csproj files.

    • Build: Compiles the project.

    • Test: Runs any tests specified in the test project(s).

    • Publish: Prepares the build output for deployment.

    • Publish Artifact: Stores the build output for further use in release pipelines.

  4. Check the Parameters section to ensure the correct project(s) are specified for building, testing, and publishing (e.g., **/*.csproj).

Once configured, save and run the pipeline to initiate the build and testing process for your ASP.NET Core project.

tep 20: Configure and Save the Pipeline

  1. In the pipeline configuration, locate the Use .NET Core sdk task.

  2. Specify the Version of the .NET Core SDK you want to use (e.g., 8.0.x).

  3. Click Save & queue or Save to save your pipeline.

  4. In the Save build pipeline dialog:

    • Choose a folder by clicking the ellipsis (...) and selecting the desired folder.

    • Add any comments if needed.

  5. Click Save to finalize and store the pipeline configuration.

This completes the setup of your ASP.NET Core pipeline, ready to build, test, and deploy your application.

Step 22: Verify Specific Files in the Repository

To ensure that your application files are correctly stored and accessible in Azure DevOps:

  1. Navigate to Repos > Files in your project.

  2. Browse to the AspNetCoreWebApplication > Views > Home directory.

  3. Confirm that essential files like Index.cshtml and Error.cshtml are present in this directory. These files are typically used for rendering the main view and error handling in an ASP.NET Core application.

Having these files in place verifies that your project structure is ready for building and deploying the application.

Step 23: Update and Commit Changes to Index.cshtml

  1. In Repos > Files, navigate to AspNetCoreWebApplication > Views > Home and select Index.cshtml.

  2. Edit the file to make the necessary changes.

  3. After making your changes, click Commit.

  4. In the Commit dialog:

    • Enter a commit message, such as "Updated Index.cshtml".

    • Ensure the correct branch is selected (e.g., unit-test).

  5. Click Commit to save your changes to the repository.

This process confirms that the Index.cshtml file is updated and committed, ready for the pipeline to deploy the latest changes.

Step 24: View Pipeline Run and Job Status

After committing your changes, the CI/CD pipeline automatically runs to build, test, and publish your ASP.NET Core application.

  1. Go to Pipelines > Pipelines and select the latest pipeline run.

  2. You’ll see a list of Jobs in the pipeline, showing each step with its status:

    • Initialize job: Sets up the agent environment.

    • Checkout: Checks out the code from the repository.

    • Use .NET Core sdk: Specifies the .NET Core SDK version.

    • Restore: Restores project dependencies.

    • Build: Builds the application.

    • Test: Runs any configured tests.

    • Publish: Prepares the application for deployment.

    • Publish Artifact: Stores build artifacts for release.

  3. Each job is marked with a green check mark if successful. To view detailed logs, click on each job step or click View raw log on the right.

This overview confirms the successful execution of each stage in your CI/CD pipeline.

Step 25: Start the Azure DevOps Agent

To manually start the Azure DevOps agent and allow it to listen for jobs.

  1. In your MyAgent directory, run the following command.

  2. The agent will connect to the server and begin listening for jobs.

  3. As jobs are received, the agent will execute them. You will see updates such as:

    • Running job: Indicates a job has started.

    • Job completed with result: Succeeded: Confirms successful completion of the job.

Running the agent manually provides control over job execution and allows you to monitor job status in real-time.

Conclusion

Setting up a CI/CD pipeline in Azure DevOps has automated the build, test, and deployment for our ASP.NET Core app, enabling continuous integration and reliable deployment with every commit.

In this guide, we:

  • Created a secure Azure DevOps organization.

  • Configured a pipeline to automate builds and tests.

  • Used a Linux VM as a self-hosted agent for better control.

  • Seamlessly committed and deployed changes.

With this setup, we can now focus on feature development, trusting our pipeline to maintain quality and speed up releases. Azure DevOps truly streamlines software delivery, making deployments more efficient and reliable.

0
Subscribe to my newsletter

Read articles from Rabiatu Mohammed directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rabiatu Mohammed
Rabiatu Mohammed

CLOUD ENGINEER | DEVOPS | SECURITY