Automating ASP.NET Core App Build and Testing with Azure Pipelines

Automating ASP.NET Core App Build and Testing with Azure Pipelines

ยท

6 min read

In this hands-on lab, you're tasked with pushing a .NET Core application (with tests) to Azure Repos and then building and testing it using Azure DevOps. You'll be provided with the necessary code and guided through the steps to accomplish these tasks.

Once you've successfully completed the build job, you have finished this lab.

Prerequisites

Before diving in, make sure you have the following:

  1. An Azure Account (with subscription).

  2. An Azure DevOps account (you can sign up for free).

  3. Basic understanding of Git and Azure DevOps concepts.

Step 1: Setup Azure DevOps

  1. First I had all the resources above launched on my Azure Portal.

  2. Next, I went on the search bar, search and selected Azure DevOps Organization.

  3. Next select "My Azure DevOps Organizations"

  4. Then fill in the form with your details to login, when done click "Continue".

  5. Next, click "Create new organization".

  6. Continue to fill the form, then create a new Project.

  7. The next thing to do is to disable these following toggles that comes at default.

    To do this go to Organizational settings at the bottom left, Navigate to Settings under Pipeline, then toggle these to ensure they're disabled.

Step 2: Set Up Azure Repos

  1. Navigate to the Repos tab and create a new repository for your ASP.NET Core application.

  2. Clone the repository from git using command line.

    To get started on this task there's need to SSH login into my Linux VM titled "linVM"

  3. On the terminal, enter the command "SSH cloud_user@40.112.151.137"

  4. Next is to run the git clone command,

    git clone -b unit-testhttps://github.com/ACloudGuru-Resources/content-az400-lab-resources.git

    specifying the exact branch which is "unit test". Click to view the repo on Github.

  5. Next check the contents in linVM with the ls command.

  6. Next ran git remote -v to get the remote origin because we are planning to remove it.

    Then, ran command git remote remove origin this command is used to remove the remote so we can add the one from the Azure Repos repository.

  7. On Azure DevOps, go to Repos and copy the commands on "HTTPS".

    Then run the commands.

  8. To get the password, we need to create a Personal Access Token.

    First, click the profile settings at the top right, then select Personal Access Tokens.

    Next add a Token

    Fill the form, select Full access, and click on create.

    Now, copy the token and save somewhere, because you'll need it later.

  9. Now paste the token as your password, then click enter.

    Now we have our files on Repos.

Step 3: Download Self-Hosted Agent

  1. At the top of the page, click the Azure DevOps icon, then select the project.

    On the bottom left, click Project settings.

  2. Scroll down the left-hand pane and under Pipelines, select Agent pools.

    Select the Default pool.

  3. Click New agent.

  4. Select the Linux tab.

    Click the Copy icon, next to Download to copy the agent URL to your clipboard.

    Paste it to a text file to use in the following steps. Keep this tab and popup open.

  5. Go back to the Linux VM, and change directory to the root:

    cd ~

    Make and navigate to a new directory called Downloads:

    mkdir Downloads && cd Downloads

    Run wget against the agent URL you just copied:

    wgethttps://vstsagentpackage.azureedge.net/agent/3.236.1/vsts-agent-linux-x64-3.236.1.tar.gz

  6. Navigate back to the root - cd ~

    Create and navigate to another directory called myagent:

    mkdir myagent && cd myagent

    Unzip the package you just downloaded. You can copy this same command from the popup in Azure DevOps to get the full package and version number:

    tar zxvf -/Downloads/vsts-agent-linux-x64-3.236.1.tar.gz

  7. Review the contents of the directory (ls). Observe all of the files and folders for the agent.

  8. Configure the Agent. Run the configuration script: ./config.sh

    Type y to accept the license agreement.

  9. When prompted for your server URL, go back to Azure DevOps and grab the URL from your browser. It will look something like https://dev.azure.com/clouduser..., followed by some numbers (i.e., copy everything up to and not including /MyFirstProject). Paste this in the terminal.

    Press Enter to accept the default authentication type of PAT.

    For personal access token, copy and paste the personal access token you created and saved earlier in this lab.

    Press Enter to accept the default agent pool.

    Press Enter to accept the default name.

    Press Enter to accept the default work folder.

  10. Run the agent interactively so that it is always listening for any jobs:

    ./run.sh

Step 4: Create the new Pipeline

  1. Return to Azure DevOps, close the agent configuration box, and click the breadcrumb for MyFirstProject near the top of the screen.

  2. In the left navigation, click Pipelines.

  3. Click Create Pipeline.

  4. Click Use the classic editor.

  5. Under Select a source, select Azure Repos Git and make the following selections:

    Team project: MyFirstProject Repository: MyFirstProject Default branch: unit-test Click Continue.

  6. Scroll down and select the ASP.NET Core template.

    Click Apply.

  7. On the right panel, for Agent pool, select Default.

    For Project(s) to test, enter: /[Tt]est/.csproj.

  8. Next to Agent job 1, click the + icon to add a new task.

    Search for and click Add for the Use .NET Core task.

  9. In the task list for Agent job 1, drag and move the Use .NET Core task to the top.

  10. With the Use .NET Core task still selected, change the Version on the right to 3.1.x.

  11. At the top, click Save & queue > Save > Save.

  12. Select the Triggers tab, and check the box for Enable continuous integration on the right panel.

  13. At the top, click Save & queue > Save > Save.

  14. Update the Branch On the left menu, select Repos.

    • Select the AspNetCoreWebApplication folder on the right.

    • Navigate through the folders to Views > Home.

    • Open the Index.cshtml file.

      To the right of the code pane, click Edit.

      Make a minor change in the heading.

  • At the top, click Commit > Commit.
  1. Run the Pipeline and Verify Success From the left navigation menu, select Pipelines.

  2. Select the pipeline shown to open it. Then, select the currently running job.

  3. Select Agent job 1 to view the progress. It will take a few minutes for the job to complete.

  4. Once complete, click the back arrow above the job progress.

  5. Select the Tests tab to verify success.

Conclusion

Congratulations! You've successfully completed this hands-on lab, creating and testing an ASP.NET Core application using Azure Pipelines. This experience has equipped you with valuable skills in automating build and test processes within Azure DevOps, essential for modern software development workflows.

ย