AZ-400: Designing and Implementing Microsoft DevOps Solutions

Work with Azure Repos and GitHub

Creating repository in Azure Repos

In this lesson, you'll learn how to create a GitHub repository, link it with an Azure DevOps project, and configure an Azure Pipeline to build your ASP.NET application. Follow the detailed steps and refer to the diagrams below for a seamless experience.


1. Setting Up the Azure DevOps Project

First, create a new project in Azure DevOps from scratch. In this example, the project is named "CoolWebsite." Once the project is created, you'll have access to boards and Azure Repos. Note that using the provided Azure Repo is optional.

To link your project with an external Git repository, use the following command to add the remote URL:

git remote add origin https://[email protected]/jeremy0665/CoolWebsite/_git/CoolWebsite

This command establishes your repository connection in Azure DevOps.


2. Creating the GitHub Repository

Switch to your GitHub account and create a new repository also named "CoolWebsite." Configure the repository as private with the default branch set to main.

The image shows a GitHub interface for creating a new repository named "CoolWebsite," with options for privacy settings, adding a README file, and choosing a license.

After creating the repository, copy the repository URL and clone it to your local machine using:

PS C:\Users\jeremy\Projects> git clone https://github.com/jeremykodelkoud/CoolWebsite.git
Cloning into 'CoolWebsite'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
Receiving objects: 100% (3/3), done.
PS C:\Users\jeremy\Projects>

Navigate into your local repository and add your application files:

PS C:\Users\jeremy\Projects> cd .\CoolWebsite\
PS C:\Users\jeremy\Projects\CoolWebsite> git add .
PS C:\Users\jeremy\Projects\CoolWebsite> git commit -m "Initial Commit"

After pushing the changes, refresh your GitHub repository page to view the updated file structure.

The image shows a GitHub repository page for a project named "CoolWebsite," displaying its file structure and details such as commits, branches, and languages used.


3. Connecting GitHub to Azure DevOps

Next, connect your GitHub repository to Azure DevOps. In your Azure DevOps project, navigate to Project Settings and select GitHub Connections. Click on "Connect to your GitHub account." If you're signed in already, choose the desired account (for example, "KodeKloud"). When prompted, grant the necessary repository access.

Tip

It is best practice to limit permissions only to the repositories you need (e.g., "CoolWebsite" and "Simple Web API") to maintain a secure environment.

The image shows a GitHub settings page for managing repository access, specifically for installing an Azure Boards app on selected repositories. There are options to approve, install, suspend, or uninstall the app.

Once connected, a settings page in Azure DevOps confirms the successful GitHub connection.

The image shows a GitHub connections settings page on Azure DevOps, displaying a connection to a GitHub repository. The left sidebar lists various project settings options.


4. Configuring Azure Pipelines

To configure your build process, navigate to the Pipelines section in Azure DevOps and click Create Pipeline. With your GitHub account already connected, you'll see available GitHub repositories—select the "CoolWebsite" repository.

The image shows an Azure DevOps interface prompting the user to create their first pipeline, with a sidebar menu on the left and a "Create Pipeline" button in the center.

Authorize the connection for Azure Pipelines as needed, and if prompted, choose to limit access to specific repositories.

The image shows a GitHub settings page for managing repository access, specifically for installing the Azure Pipelines app with options to select specific repositories. There are also options to suspend or uninstall the app in the "Danger zone" section.

Select your project type (e.g., ASP.NET) when configuring the pipeline.

The image shows the "Configure your pipeline" page in Azure DevOps, where various project types like ASP.NET, .NET Desktop, and Xamarin are listed for selection.


5. Defining the Pipeline YAML

Below is the final YAML configuration for your ASP.NET pipeline. This configuration instructs the pipeline to trigger on pushes to the main branch, restore NuGet packages, build the solution using MSBuild, and run tests using VSTest. The pipeline is set to run on a self-hosted agent pool named "KodeKloudCustomer."

# ASP.NET
# Build and test ASP.NET projects.
# For further customization, please refer to:
trigger:
  - main

pool:
  vmImage: 'windows-latest'
  name: 'KodeKloudCustomer'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
  - task: NuGetToolInstaller@1
    inputs:
      versionSpec: '5.x'

  - task: NuGetCommand@2
    inputs:
      restoreSolution: '$(solution)'

  - task: VSBuild@1
    inputs:
      solution: '$(solution)'
      msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
      platform: '$(buildPlatform)'
      configuration: '$(buildConfiguration)'

  - task: VSTest@2
    inputs:
      platform: '$(buildPlatform)'
      configuration: '$(buildConfiguration)'

Editing Your Pipeline

If your pipeline fails to build correctly, review the YAML configuration to adjust variables or select the appropriate agent pool. You can also refer to the Azure Pipelines Documentation for additional guidance.

For those using their own self-hosted agent pool, navigate to Agent Pools in your project settings, click Add Pool, and name it "KodeKloud Customer." Update your pipeline configuration accordingly. An updated YAML snippet might resemble the following:

# ASP.NET
# Build and test ASP.NET projects.
# For further customization, please refer to:
trigger:
  - main

pool:
  vmImage: 'windows-latest'
  name: 'KodeKloud'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
  - task: NuGetToolInstaller@1
    inputs:
      versionSpec: '5.*'

  - task: NuGetCommand@2
    inputs:
      restoreSolution: '$(solution)'

  - task: VSBuild@1
    inputs:
      solution: '$(solution)'
      msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
      platform: '$(buildPlatform)'
      configuration: '$(buildConfiguration)'

  - task: VSTest@2
    inputs:
      platform: '$(buildPlatform)'
      configuration: '$(buildConfiguration)'

After validating and saving your changes, check your GitHub repository. An updated azure-pipelines.yml file should now confirm the successful configuration.

The image shows a GitHub repository page for a project named "CoolWebsite," displaying its file structure and recent commit history. The repository is private and includes files like `App.razor`, `Program.cs`, and `azure-pipelines.yml`.


6. Conclusion

By following this lesson, you have successfully linked a GitHub repository to Azure DevOps and configured an automated build pipeline for your ASP.NET project. This integration supports continuous integration and continuous delivery (CI/CD), ensuring that your application is always built, tested, and ready for deployment.

For further reading, consider exploring these resources:

Thank you for following this guide. Enjoy building robust and scalable applications with Azure DevOps and GitHub!

Watch Video

Watch video content

Previous
Getting Started with GitHub