CI/CD build pipeline for .NET solutions using Cake2 – Azure Devops

In a previous article Build .NET solutions/projects using cake 2 I described how you can build a .NET solution using cake 2. In this tutorial I will explain how you can create your continuous integration pipeline to build your projects automatically with cake 2 and a build agent.

You should first store your project on a git repository. For the purposes of this demo I will choose Azure Devops. I first clone the repository and add the appropriate code. Then I push back to the remote.

Git commands:

git add
git commit -m "added code"
git push

Then under main branch the src folder will be located. In this folder the code is placed.

The second step is to create the pipeline and store it in the repository. Press new pipeline and then select your Azure Repos Git.

Then choose the starter pipeline on which we will add some tasks.

The code for the pipeline is just a simple powershell script that will execute the cake build command as we examined on the previous article.

trigger:
- main

pool:
  vmImage: ubuntu-latest
steps:

- task: PowerShell@2
  displayName: Build step using cake
  inputs:
    targetType: 'inline'
    script: 'dotnet cake'
    workingDirectory: '$(Build.SourcesDirectory	)/src/'

When I tried to build my application although the procedure started, I got an error about the targeting SDK.

You can resolve this by specifying your requested DotNet version.

- task: UseDotNet@2
  displayName: Use .NET 6
  inputs:
    version: '6.0.x'
    includePreviewVersions: true

Finally the build will be successful.

The final pipeline will be the below:

trigger:
- none

pr: none

pool:
  vmImage: ubuntu-latest
steps:

- task: PowerShell@2
  displayName: install cake tool
  inputs:
    targetType: 'inline'
    script: 'dotnet tool install Cake.Tool --version 2.0.0 --global'

- task: UseDotNet@2
  displayName: Use .NET 6
  inputs:
    version: '6.0.x'
    includePreviewVersions: true

- task: PowerShell@2
  displayName: Build step using cake
  inputs:
    targetType: 'inline'
    script: 'dotnet cake'
    workingDirectory: '$(Build.SourcesDirectory)/src/'

Build Service Fabric .NET applications with CMD and Azure DevOps

In this guide I will explain how to build a service fabric solution using cmd and also Azure DevOps to automate your deployments.

Given that you already have in place your Service Fabric solution, you should edit and add the below Target directive on your .sfproj file inside your visual studio solution.

This is needed in order to create the package that will be deployed on the service fabric cluster.

<Target Name="ForcePackageTarget" AfterTargets="Build" Condition="'$(ForcePackageTarget)' =='true'">
    <CallTarget Targets="Package"/>
  </Target>

That’s all. With this option enabled you can now perform a build using the msbuild tool. You should edit servicefabric.sln to reflect your project name.

msbuild servicefabric.sln /t:Build /p:ForcePackageTarget=true /p:Configuration=Debug /p:Platform=x64

The package output will be located on solution/pkg folder depending on your build configuration specified on the command line (Debug, Release).

In order to automate this procedure, you will have to create your pipeline and place it on your repository.

Three steps are needed in order to build your service fabric solution.

  • Firstly you should download the latest .NET version if your project targets .NET 6. If not, then you should select another version.
  • Secondly you should restore your Nuget packages on your solution in order to reference any services that come with the application.
  • The third step is the actual build using the msbuild task.

The example pipeline is shown below:

trigger:
- none

pr: none
pool:
  vmImage: windows-latest

steps:
- task: UseDotNet@2
  inputs:
    packageType: sdk
    version: '6.0.x'


- task: NuGetCommand@2
  inputs:
    command: 'restore'
    restoreSolution: '**/*.sln'
    feedsToUse: 'select'

- task: MSBuild@1
  inputs:
    solution: '**\*.sln'
    msbuildArchitecture: 'x64'
    configuration: 'release'
    msbuildArguments: '/p:ForcePackageTarget=true'
    clean: true

In order to get this pipeline working with .NET 6, you should edit Stateless1.csproj and add also LangVersion.

The output of the build will be located on pkg folder.

Finally you could create a release pipeline and upload the artifacts pkg directory on your service fabric cluster.

Microsoft Documentation for service fabric deployments:

https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-package-apps

Youtube video:

Build and run .NET applications on Azure DevOps

In this article I will explain how one can automate their .NET applications development using Azure DevOps. For the sake of this example I created a simple Console Application targeting .NET Framework 6.

The code only includes the below line.

Console.WriteLine("Hello from Azure Devops!");

First things first, a Git repository will be needed for the CI procedure. I chose to use Azure DevOps repositories as it is integrated with Visual studio and can be used very quickly. By pressing Add to Source Control a dialog will appear to choose the organization and project on which the repository will be created. This will create a new repository and you should then commit and push your code to the repository using the UI of Visual Studio.

After the push, the repository will be created on Azure DevOps

I disabled the automatic triggers on the repository with pr and trigger to none and I used the latest ubuntu machine as the build agent.

If a specific version of .NET is required it should be included in the task UseDotNet@2.

The building of the project is done from the DotNetCoreCLI@2 task. It will search everything with the .csproj extension and build it using the Azure CLI.

The last task that I included will run the application from the debug output folder.

trigger:
- none

pr: none 

pool:
  vmImage: ubuntu-latest

steps:

- task: UseDotNet@2
  inputs:
    version: '6.0.x'
    includePreviewVersions: true
    

- task: DotNetCoreCLI@2
  displayName: Building .NET project
  inputs:
    command: 'build'
    projects: '**/*.csproj'
    arguments: '--configuration debug'

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      cd "bin/debug/net6.0"
      ./ConsoleApp1

As my application only Included a print message, this will be shown on the output.

More details on how to build .NET projects with Azure DevOps

https://docs.microsoft.com/en-us/azure/devops/pipelines/ecosystems/dotnet-core?view=azure-devops&tabs=dotnetfive

The specified module ‘MSAL.PS’ was not loaded because no valid module file was found in any module directory.

Recently when I used Dynamics 365 Finance and Operation tools plugin for Azure Devops I faced an issue with a missing Powershell module.

Error message:

##[error]The specified module 'MSAL.PS' was not loaded because no valid module file was found in any module directory.

In order to bypass this issue, add a PowerShell step with the below commands:

Set-PSRepository PSGallery -InstallationPolicy Trusted
Install-Module MSAL.PS
Import-Module MSAL.PS

Your final pipeline should look like the one below.

An additional issue you may face, could relate with the service connection authentication endpoint. The error indicates that the common endpoint cannot be used and the specific tenant-endpoint should be used instead.

##[error]AADSTS9001023: The grant type is not supported over the /common or /consumers endpoints. Please use the /organizations or tenant-specific endpoint.

Go and edit your service connection details

Endpoint URL:

https://login.microsoftonline.com/organizations

Be sure that your user has sufficient privileges and that prerequisites are met as documented from Microsoft.

LCS doesn’t support service-to-service authentication. Therefore, only regular user credentials (that is, a user name and password) can be used. Because the pipelines don’t run interactively, multifactor authentication must not be set up for the account that you use. We recommend that you set up a separate user account that has limited access and strong credentials that can regularly be rotated for security purposes.

Create an LCS connection in Azure Pipelines

https://docs.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/dev-tools/pipeline-lcs-connection