Aller au contenu principal

Exemple de pipeline Azure DevOps pour build et déploiement d'images Docker à partir de projets .NET

azure-pipelines-docker.yml

# Azure DevOps Pipeline pour build et déploiement Docker
# Template pour applications .NET
#
# Instructions:
# 1. Remplacer VOTRE_SERVICE_CONNECTION_ID par l'ID de votre service connection ACR
# 2. Remplacer VOTRE_APP_NAME par le nom de votre application
# 3. Remplacer VOTRE_REGISTRY par l'URL de votre Azure Container Registry
# 4. Adapter les projets à publier selon votre structure

trigger:
batch: true
branches:
include:
- docker/*

name: $(Rev:r)

resources:
- repo: self

variables:
# Version extraite du nom de branche (ex: docker/1.0.0 -> 1.0.0)
appversion: ${{ replace(variables['Build.SourceBranchName'], 'docker/', '') }}
# Version complète avec build number (ex: 1.0.0.42)
version: $(appversion).$(Build.BuildNumber)

# À CONFIGURER: Service connection vers Azure Container Registry
dockerRegistryServiceConnection: 'VOTRE_SERVICE_CONNECTION_ID'

# À CONFIGURER: Nom de votre application
imageRepository: 'VOTRE_APP_NAME'

# À CONFIGURER: URL de votre container registry
containerRegistry: 'VOTRE_REGISTRY.azurecr.io'

stages:
- stage: Build
condition: ${{ contains(variables['Build.SourceBranch'], 'docker/') }}
displayName: Build and push stage
jobs:
- job: Build
displayName: Build
pool:
# Pool d'agents self-hosted
name: 'jit-azure-devops-agents'

steps:
# Définir le Short Commit ID (7 premiers caractères)
- task: Bash@3
name: SetShortCommitId
displayName: 'Set Short Commit ID'
inputs:
targetType: 'inline'
script: |
echo "##vso[task.setvariable variable=ShortCommitId]${BUILD_SOURCEVERSION:0:7}"

# Installer le SDK .NET
- task: UseDotNet@2
name: SetSdkVersion
displayName: 'Install .NET SDK'
inputs:
packageType: 'sdk'
version: '9.x'
installationPath: $(Agent.ToolsDirectory)/dotnet

# Restaurer les workloads .NET (si nécessaire, ex: Blazor WebAssembly)
- script: |
dotnet workload restore
displayName: 'Restore workloads'

# Restore NuGet packages
- task: DotNetCoreCLI@2
displayName: 'Restore NuGet packages'
inputs:
command: 'restore'
projects: '**/*.sln'

# Build la solution
- task: DotNetCoreCLI@2
displayName: 'Build solution'
inputs:
command: 'build'
projects: '**/*.sln'
arguments: '--configuration Release'

# =====================================================
# PROJET 1 - À ADAPTER SELON VOS BESOINS
# =====================================================
- task: DotNetCoreCLI@2
displayName: 'Publish Project1'
inputs:
command: 'publish'
publishWebProjects: false
zipAfterPublish: false
modifyOutputPath: false
projects: '**/Project1.csproj'
arguments: '--self-contained true --configuration Release --output $(Build.ArtifactStagingDirectory)/Project1 /p:Version=$(appversion) /p:AssemblyVersion=$(version) /p:IncludeSourceRevisionInInformationalVersion=false'

- task: Docker@2
displayName: 'Build Docker image - Project1'
inputs:
containerRegistry: '$(dockerRegistryServiceConnection)'
repository: '$(imageRepository)/project1'
command: 'buildAndPush'
Dockerfile: 'Project1/Dockerfile'
buildContext: '$(Build.ArtifactStagingDirectory)/Project1'
tags: |
$(version)
latest

# =====================================================
# PROJET 2 - Dupliquer cette section si vous avez plusieurs projets
# =====================================================
# - task: DotNetCoreCLI@2
# displayName: 'Publish Project2'
# inputs:
# command: 'publish'
# publishWebProjects: false
# zipAfterPublish: false
# modifyOutputPath: false
# projects: '**/Project2.csproj'
# arguments: '--self-contained true --configuration Release --output $(Build.ArtifactStagingDirectory)/Project2 /p:Version=$(appversion) /p:AssemblyVersion=$(version)'

# - task: Docker@2
# displayName: 'Build Docker image - Project2'
# inputs:
# containerRegistry: '$(dockerRegistryServiceConnection)'
# repository: '$(imageRepository)/project2'
# command: 'buildAndPush'
# Dockerfile: 'Project2/Dockerfile'
# buildContext: '$(Build.ArtifactStagingDirectory)/Project2'
# tags: |
# $(version)
# latest

# =====================================================
# EXEMPLE: Stage de déploiement (optionnel)
# =====================================================
# - stage: Deploy
# displayName: Deploy to staging
# dependsOn: Build
# condition: succeeded()
# jobs:
# - deployment: DeployStaging
# displayName: Deploy to staging environment
# pool:
# name: 'jit-azure-devops-agents'
# environment: 'staging'
# strategy:
# runOnce:
# deploy:
# steps:
# - task: Docker@2
# displayName: 'Pull latest image'
# inputs:
# containerRegistry: '$(dockerRegistryServiceConnection)'
# command: 'pull'
# arguments: '$(containerRegistry)/$(imageRepository)/project1:latest'
#
# - script: |
# docker stop project1 || true
# docker rm project1 || true
# docker run -d --name project1 -p 8080:80 $(containerRegistry)/$(imageRepository)/project1:latest
# displayName: 'Deploy container'