Author : MD TAREQ HASSAN

Options

CI pipeline can be created in two ways:

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:

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

CI YAML pipeline

Azure DevOps - CI YAML Pipeline Step 1

Azure DevOps - CI YAML Pipeline Step 2

Azure DevOps - CI YAML Pipeline Step 3

Azure DevOps - CI YAML Pipeline Step 4

Azure DevOps - CI YAML Pipeline Step 5

Azure DevOps - CI YAML Pipeline Step 6

Azure DevOps - CI YAML Pipeline Step 7

Azure DevOps - CI YAML Pipeline Step 8

Azure DevOps - CI YAML Pipeline Step 9

Azure DevOps - CI YAML Pipeline Step 10

Azure DevOps - CI YAML Pipeline Step 11

Azure DevOps - CI YAML Pipeline Step 12

Azure DevOps - CI YAML Pipeline Step 13

Azure DevOps - CI YAML Pipeline Step 14

Build and push docker 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

Azure DevOps - CI pipeline using classic editor Step 1

Azure DevOps - CI pipeline using classic editor Step 2

Azure DevOps - CI pipeline using classic editor Step 3

Azure DevOps - CI pipeline using classic editor Step 4

Azure DevOps - CI pipeline using classic editor Step 5

Azure DevOps - CI pipeline using classic editor Step 6

Azure DevOps - CI pipeline using classic editor Step 7

Settings

ASP.NET Core web application

Azure DevOps settings - ASP.NET Core web application.

ASP.NET Core web application that targets the full .NET Framework.

Azure DevOps settings - ASP.NET Core web application that targets the full .NET Framework.

Trigger

Azure DevOps - creating Continous Integration trigger