How to implement low-cost CI/CD for .Net projects

Javier Eduardo Mendoza Blandón
11 min readMay 30, 2023

--

using Azure DevOps with hosting on Smarter ASP.

Artículo en español: https://bit.ly/3WI5qBV

Welcome, this guide aims to teach you everything you need to know to implement CI/CD in Azure DevOps with hosting on Smarter ASP for .Net projects, particularly with APIs, in just 29 steps.

Requirements:

  • Basic knowledge of Visual Studio .NET 2022.
  • Visual Studio 2022 Version 17.5.4.
  • Dotnet version 6.0.16

Why CI/CD?

Continuous integration and continuous delivery/deployment (CI/CD) with Azure DevOps are important because they offer a fast and reliable way to develop, test, and deploy software applications.

CI/CD allows software development teams to deliver software updates frequently, ensuring higher quality and reducing costs.

Context

Are you aware of the option to create CI/CD for smaller development teams using Azure DevOps with SmarterASP.NET hosting? It is a cost-effective way to improve collaboration, streamline development processes, and provide the scalability and security needed to host .NET applications for startups, small and medium-sized businesses, as well as personal projects.

Not everyone can afford the high cost of implementing CI/CD in the Azure or AWS cloud. That’s why I offer this good alternative based on my past experiences.

Start of the guide

I will show you the concise guide on how to carry out this process in the following PDF. In some steps, I will provide you with links to videos that can help you meet certain prerequisites before starting the actual guide.

Step 0: Create an account, organization, project in Azure DevOps.

https://www.youtube.com/watch?v=A91difri0BQ
https://www.youtube.com/watch?v=705enJzR2cY
https://www.youtube.com/watch?v=Ii70yZ7pmJs

Step 1: Create an account in SmarterASP.NET hosting

Step 2: How to create a MSSQL database in SmarterASP.NET (optional)

Step 3: For this implementation, we will need a .Net API project. You can use an existing one that you have created before or learn how to create one and obtain additional information in the following article I have created: [ In it, you will find detailed instructions on how to create a .Net API project and other interesting related concepts.

https://bit.ly/42XOucN For the rest of the article, I will use the repository in the end of the link article.

Step 3.1: Quick verification

Step 3.2: Please verify if you have the following two code line in your .Net api project

Step 3.3: Please verify if your swagger documentation is available for all environment.

Ready, all was verified.

Step 4: We need to create a new website in our SmarterASP hosting.

Step 5: We need to turn on the website create before in the step 4.

Step 5.1: Write this data because you will need them in the CD pipeline.

ServiceUrl your web deploy Service URL
SiteName your web deploy site name
UserName your web deploy user name
Password your web deploy password

Step 5.2: I recommend for security reasons to change the password to one that you can always remember because normally is the same as the login.

Step 6: We need to create an Azure repository to upload our .Net api project.

We choose the project
click on create button.
result.

Step 7: Now we need to add our code to the new Azure repository created.

copy the url

Step 7.1: Now you can use your favorite tool to do the following steps, in my case is git bash.

Step 7.2: Clone the repository to your local machine using Git command-line tools.

git clone https://your-project-url the url copied in the step 7

Remember your login credential to azure portal here.

Step 7.3: Change your working directory to the local repository.

cd YourRepoName

Step 7.4: Add your code files to the repository using the “git add” command.

git add .

Step 7.5: Commit your changes to the repository using the “git commit” command.

git commit -m “Initial api version 2”

Step 7.6: Push your changes to the Azure DevOps repository using the “git push” command.

git push origin main
result.

Step 8: We need to establishing pull requests as the way to merge changes into the main branch in Azure Repositories (or any Git repository) is important for several reasons for example: Code review, Collaboration, Control over changes, version control.

click in more options.

That is the basic configuration to do it

Building Continuous Integration — CI

Step 9: We need to create our first pipeline, navigate to Pipelines at the left nav menu bar, then click Create Pipeline button.

Step 10: Select the classic editor to create your first pipeline.

Step 11: We need to select a source of our repository, in our case, we use Azure Repos Git.

Step 12: We need to choose a template that builds your kind of app,in our case its a ASP.NET Core API.

Step 13: Do a similar configuration in the following fields and parameters:

Step 13.1: Name* : DON’T leave any white space in the name, to prevent any unexpected problems.

use a name related to your project.

Step 13.2: Agent Specification* : please choose “windows” since we are going to build and publish the project to a Windows platform.

Step 13.3: Project(s) to restore and build, please be specific.

find where is yout .csproj
result.

Step 14: We need to open the “Publish” pane, untick Zip published projects.

Step 15: If you need to add,update or delete variables you can use Variables tab page, you can update the configurations as you need, the default settings are enough to publish a standard asp.net core app.

Step 16: We need to go to the triggers page and tick the Enable continuous integration, this will build your project pipeline whenever a change is committed to your main branch repository.

Step 17: We need to Save & queue the pipeline

Step 18: After of save and run, the page will navigate to the Summary pane automatically. You can check the job status and our published results.

Step 19: We need to verify the artifact about the published project produced files, write down the path “drop\src”, we will need to use it later in the CD pipeline.

SourcePath this will be your pipeline published path, default is “drop\src”.

Building Continuous Delivery — CD

Step 20: We need to create our second pipeline, navigate to the releases tab page, and create a new release pipeline.

Step 21: We need to select a template of “Empty job”.

We need to write the name of our first environment, in my case will be “Development”.

Step 22: We need to add our artifact, created before.

Step 23: We need to choose the source(build pipeline) NOT update the Source alias * field.

Step 24: We need to enable the continuous deployment trigger, this will execute the subsequent stage jobs when a pipeline is built.

Step 25: We need to add a task for the deployment.

Step 26: We need to do the following configurations.

Step 26.1: Open the Agent job pane, in the Agent pool, choose Hosted Windows 2019 with VS2019.

Step 26.2: Make sure the Artifact download is configured with the matched build pipeline.

Step 26.3: Add a PowerShell task.

Step 26.4: We need open powerShell script task:

- Change Type to Inline
- Copy and paste the following script to Script* field.
You don't need to update anything in the script.
$sourcePath = Join-Path -Path "$($Env:AGENT_RELEASEDIRECTORY)\$($Env:RELEASE_PRIMAR
YARTIFACTSOURCEALIAS)" -ChildPath $Env:SourcePath
# Deploy project using Web Deploy
& "C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe" -verb:sync -source:co
ntentPath=$sourcePath -dest:contentPath=$Env:SiteName,computerName=$Env:ServiceUrl,
userName=$Env:UserName,password=$Env:Password,authtype="Basic",includeAcls="False"
-allowUntrusted -disableLink:AppPoolExtension -disableLink:ContentExtension -disabl
eLink:CertificateExtension -enableRule:AppOffline -verbose

Or you can use the following with retries, this is my favorite to be honest.

$sourcePath = Join-Path -Path "$($Env:AGENT_RELEASEDIRECTORY)\$($Env:RE
LEASE_PRIMARYARTIFACTSOURCEALIAS)" -ChildPath $Env:SourcePath
$maxRetries = 5
$retryCount = 0
while ($retryCount -lt $maxRetries) {
& "C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe" -verb:sync -sourc
e:contentPath=$sourcePath -dest:contentPath=$Env:SiteName,computerName=$Env
:ServiceUrl,userName=$Env:UserName,password=$Env:Password,authtype="Basic",
includeAcls="False" -allowUntrusted -disableLink:AppPoolExtension -disableLink:Con
tentExtension -disableLink:CertificateExtension -enableRule:AppOffline -verbose
# Check if the deployment succeeded
if ($LASTEXITCODE -eq 0) {
Write-Output "Deployment succeeded after $($retryCount + 1) retries"
break
}
# Wait for a few seconds before retrying
Start-Sleep -Seconds 10
pg. 23 Ing. Javier Eduardo Mendoza Blandón
cvmendozacr.com
$retryCount++
}
if ($retryCount -eq $maxRetries) {
Write-Output "Deployment failed after $maxRetries retries"
}

Step 26.5: Expand the “Environment Variables” tab and add the following information:

SourcePath
ServiceUrl
SiteName
UserName
Password

Remember you can get this data from Step 5, Step 19.

Step 27: We need to do click in the following marked images to complete the pipeline.

click on save button.
click on OK button , after create release button.
click on release 1 created.
click on deploy.
result.

Cheers we have completed all.

Step 28: We need to verify if your .Net Api project was deployed successfully at SmarterASP side.

https://www.smarterasp.net/
https://member5-3.smarterasp.net/account/loginform

End of implementation.

After to complete the implementation, this will be your frequently step.

Step 29: Let do another test folks, only to be sure if all this effort to create this CI/CD flow is working as expected.

A- Let create another git branch with the name that you prefer.

git checkout -b myTestBrach

B- Open your .net api project with VS and do any change in your swagger ui

C- Let save,add, push the change to the azure repository.

git add .

git commit -m “Initial api version 3”
git push origin myTestBrach: myTestBrach

D- We’ll need to create the pull request in the azure repository

E- Automatically after completing the pull request the CI pipeline will be triggered.

F- Automatically after complete the CI pipeline , CD pipeline will be triggered.

That is everything folks, I will wait for your feedback, ideas, or advice, don’t hesitate to do it please, thank you.

--

--