Author : MD TAREQ HASSAN
Options
CI pipeline can be created in two ways:
- YAML file
- Classic editor
Project setup
Clone my sample project from Github: https://github.com/hovermind/AzureDevOpsYamlPipelineDemos
(contains everything you need to create CI yaml pipeline).
Or alternatively, scaffold Visual Studio projects:
- Create ASP.Net Web API named “XyzAPI”
- Create xUnit test project named “XyzAPI.Test”
xunit
andxunit.runner.visualstudio
Nuget packages will be installed during project creation- Install xUnit AutoFixture and AutoMoq
- Controller tests
WeatherForecastControllerTestFixture
classWeatherForecastControllerTest
class (add test methods)
- Service tests
- Create
IArithmeticService
andArithmeticService
(register in DI:builder.Services.AddScoped<IArithmeticService, ArithmeticService>()
) - Add simple
Add(int x, int y)
method ArithmeticServiceTest
(add test cases)
- Create
- Create
azure-pipelines-ci.yml
and add jobs- Build job
- Test job
- DotNetCoreCLI task: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/build/dotnet-core-cli?view=azure-devops
azure-pipelines-ci.yml
(add if you are creating your own projects, sample project in Github already contains it)
trigger:
- master
pool:
vmImage: ubuntu-latest
jobs:
- job: buildJob
displayName: Build Job
steps:
- task: UseDotNet@2
displayName: Use .Net 6.x
inputs:
packageType: 'sdk'
version: '6.x'
- task: DotNetCoreCLI@2
displayName: 'DotNet Restore Before Build'
inputs:
command: 'restore'
projects: '**/XyzAPI/*.csproj'
- task: DotNetCoreCLI@2
displayName: 'DotNet Build'
inputs:
command: 'build'
arguments: '--no-restore'
projects: '**/XyzAPI/*.csproj'
- job: testJob
displayName: Test Job
dependsOn: buildJob
condition: succeeded('build_job')
steps:
- task: UseDotNet@2
displayName: Use .Net 6.x
inputs:
packageType: 'sdk'
version: '6.x'
- task: DotNetCoreCLI@2
displayName: 'DotNet Restore Before Test'
inputs:
command: 'restore'
projects: '**/XyzAPI.Test/*.csproj'
- task: DotNetCoreCLI@2
displayName: 'DotNet Test'
inputs:
command: 'test'
arguments: '--no-restore'
projects: '**/XyzAPI.Test/*.csproj'
publishTestResults: true
Repository setup
- Create Azure DevOps organigation if does not exist
- Create Azure DevOps project if does not exist
- Create Azure DevOps repository named ‘XyzAPI’
- Push code to repository
CI YAML pipeline
- Select Pipelines > New pipeline
- Select “Azure Repos Git” > Select target Repo > “Existing Azure Pipelines YAML file”
- Select ‘
azure-pipelines-ci.yml
’ > Continue - Click dropdown on right of ‘Run’ > Save
- Edit > tripple verticle dots on top right
- Triggers > YAML > Name: “XyzAPI-Pipeline-CI”
- Save & queue > Save and run
Build and push docker image to ACR
- Create ACR service connection (will be used by ‘Docker@2’ task)
- Add ‘Docker@2’ task to bulild and push image to ACR
# ... ... ...
- job: buildAndPushDockerImageJob
displayName: Build & Push Docker Image Job
steps:
- task: UseDotNet@2
displayName: Use .Net 6.x
inputs:
packageType: 'sdk'
version: '6.x'
- task: Docker@2
displayName: Build & Push XyzAPI Image to ACR
inputs:
containerRegistry: AcrServiceConnectionXyzAPI
repository: 'xyz-api'
command: buildAndPush
Dockerfile: '**/XyzAPI/Dockerfile'
buildContext: $(Build.Repository.LocalPath)
tags: $(Build.BuildId)
Complete azure-pipelines-ci.yml
trigger:
- master
pool:
vmImage: ubuntu-latest
jobs:
- job: buildJob
displayName: Build Job
steps:
- task: UseDotNet@2
displayName: Use .Net 6.x
inputs:
packageType: 'sdk'
version: '6.x'
- task: DotNetCoreCLI@2
displayName: 'DotNet Restore Before Build'
inputs:
command: 'restore'
projects: '**/XyzAPI/*.csproj'
- task: DotNetCoreCLI@2
displayName: 'DotNet Build'
inputs:
command: 'build'
arguments: '--no-restore'
projects: '**/XyzAPI/*.csproj'
- job: testJob
displayName: Test Job
dependsOn: buildJob
condition: succeeded('build_job')
steps:
- task: UseDotNet@2
displayName: Use .Net 6.x
inputs:
packageType: 'sdk'
version: '6.x'
- task: DotNetCoreCLI@2
displayName: 'DotNet Restore Before Test'
inputs:
command: 'restore'
projects: '**/XyzAPI.Test/*.csproj'
- task: DotNetCoreCLI@2
displayName: 'DotNet Test'
inputs:
command: 'test'
arguments: '--no-restore'
projects: '**/XyzAPI.Test/*.csproj'
publishTestResults: true
- job: buildAndPushDockerImageJob
displayName: Build & Push Docker Image Job
steps:
- task: UseDotNet@2
displayName: Use .Net 6.x
inputs:
packageType: 'sdk'
version: '6.x'
- task: Docker@2
displayName: Build & Push XyzAPI Image to ACR
inputs:
containerRegistry: AcrServiceConnectionXyzAPI
repository: 'xyz-api'
command: buildAndPush
Dockerfile: '**/XyzAPI/Dockerfile'
buildContext: $(Build.Repository.LocalPath)
tags: $(Build.BuildId)
Run pipeline and go to ACR to check that image is pushed successfully
CI pipeline using classic editor
Settings
- Pipelines > Select the target pipeline
- Edit > set Task, Variables, Options etc.
ASP.NET Core web application
ASP.NET Core web application that targets the full .NET Framework.
Trigger
- Will run the pipeline when a commit is pushed to the remote
- Pipelines > Select target pipeline > Edit
- Triggers > Select under “continuous integration” > Tick (check box) “Enable continuous integration”
- Optionally you can check “Batch changes while a build is in progress” (multiple builds will be combined in a single run if needed)
- Set “Branch filters” and “Path filters”
- Save > enter comment > Save
- Now push a test commit > pipeline should run by CI trigger