Create Release to Run Tests on Azure DevOps Release Pipeline via C#

Introduction

In the realm of modern software development, automation is key to maintaining efficiency and consistency. Azure DevOps offers robust tools for continuous integration and continuous deployment (CI/CD), allowing you to streamline your release management processes. In this blog, we’ll walk you through the steps to create a release on Azure DevOps using C# code. Whether you're a seasoned developer or just starting with Azure DevOps, this guide will help you harness the power of automation.

What You'll Need

  1. Azure DevOps Account: If you don’t have one, sign up at Azure DevOps Services.
  2. Personal Access Token (PAT): You'll need this for authentication. Create one from your Azure DevOps profile settings.
          Click on Settings Icon next to profile icon and then click Personal access tokens.

            Click on New Token  and then you need enter the required details in the below screen.




   3.Visual Studio or any C# IDE: For writing and running your C# code.
   4.NuGet Packages: We'll use following NuGet Packages for interacting with Azure DevOps.
  • Microsoft.TeamFoundationServer.Client
  • Microsoft.VisualStudio.Services.Release.Client

Setting Up Your Environment

  1. Create a Console Application:

    • Open Visual Studio and create a new C# Console Application project.
    • Name your project appropriately (e.g., AzureDevOpsReleaseCreator).
  2. Add NuGet Packages:

    • Right-click on your project in Solution Explorer and select Manage NuGet Packages.
    • Search for Microsoft.TeamFoundationServer.Client & Microsoft.VisualStudio.Services.Release.Client and install it. These package contains the necessary libraries to interact with Azure DevOps Services.

Writing the C# Code

1. Initialize Azure DevOps Connection

First, you need to establish a connection to Azure DevOps using your PAT and organization URL. 

Replace {OrganisationURL} with your Azure DevOps Organisation Name and {PAT} with your Personal Access Token.


2. Create Test Run

In the CreateTestRunAsync method, you need to pass TestRunName (which can be any string), TestPlanId and integer array of pointsIds (Points Ids can be obtained using TestPlanHttpClient)                



3. Create Release

Once Test Run is created then you will need to create a release passing the BuildId (this can we get from build create from pipelines section) and Release DefinitionId (This is a unique identifier assigned to each release definition within Azure DevOps under releases section)


4. Update Test Run

Once the Test Run and Release are created, we then need to update the Test Run passing the release that we created in the previous step.


5. Update Release

Once the test run is updated, next is to update the release. We can update the release by passing the release object we created in step 3 and TestRunId (can we obtained from Test Run created under step2).


6. Update Release Environment

Once Release and Test Run are updated. Last step is to update the release environment, once the release environment is updated then you must be able to see release getting created under Releases Section for that particular Release CD for which you have passed the Release Definition Id.


7. Run Your Code

  • Build and run your application.
  • You must able to see newly created release under Releases Section and newly created Test Run under Test Run section. 

Testing and Troubleshooting

  1. Check Azure DevOps: Navigate to the Releases section in your Azure DevOps project to verify that the release has been created.
  2. Debugging: If you encounter issues, make sure your PAT has the appropriate permissions and that all IDs and names are correct.

Conclusion

Automating the creation of releases with C# code can save you time and reduce the risk of manual errors. By leveraging Azure DevOps APIs and the Microsoft.TeamFoundationServer.Client & Microsoft.VisualStudio.Services.Release.Client package, you can seamlessly integrate release creation into your CI/CD pipelines.

Feel free to expand upon this basic example by integrating it with other automation scripts or extending functionality to handle more complex release scenarios.

Happy coding, and may your releases be smooth and bug-free!

Comments

Post a Comment