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
- Azure DevOps Account: If you don’t have one, sign up at Azure DevOps Services.
- Personal Access Token (PAT): You'll need this for authentication. Create one from your Azure DevOps profile settings.
- Microsoft.TeamFoundationServer.Client
- Microsoft.VisualStudio.Services.Release.Client
Setting Up Your Environment
Create a Console Application:
- Open Visual Studio and create a new C# Console Application project.
- Name your project appropriately (e.g.,
AzureDevOpsReleaseCreator
).
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
4. Update Test Run
5. Update Release
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
- Check Azure DevOps: Navigate to the Releases section in your Azure DevOps project to verify that the release has been created.
- 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!
Very helpful azure solution need more of that ✨
ReplyDelete