Azure Pipelines #

什么是 Azure Pipelines? #

Azure Pipelines 是 Azure DevOps 提供的 CI/CD 服务,支持自动化构建、测试和部署。

text
┌─────────────────────────────────────────────────────────────┐
│                    Pipelines 概览                            │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  功能                                                        │
│  ├── 持续集成 (CI): 自动构建和测试                          │
│  ├── 持续部署 (CD): 自动部署到环境                          │
│  ├── 多平台支持: Windows, Linux, macOS                      │
│  ├── 多语言支持: .NET, Java, Node.js, Python 等             │
│  └── 部署目标: Azure, AWS, GCP, 本地                        │
│                                                             │
│  代理类型                                                    │
│  ├── Microsoft 托管代理: 免费维护                           │
│  └── 自托管代理: 自定义环境                                 │
│                                                             │
│  流水线类型                                                  │
│  ├── YAML 流水线: 代码定义,推荐                            │
│  └── 经典流水线: 图形界面配置                               │
│                                                             │
└─────────────────────────────────────────────────────────────┘

YAML 流水线 #

基本结构 #

yaml
# azure-pipelines.yml
trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

variables:
  buildConfiguration: 'Release'

steps:
- script: echo Hello, world!
  displayName: 'Run a one-line script'

- script: |
    echo Add other tasks to build, test, and deploy your project.
    echo See https://aka.ms/yaml
  displayName: 'Run a multi-line script'

触发器配置 #

yaml
# CI 触发器
trigger:
- main
- develop

# 路径过滤
trigger:
  branches:
    include:
    - main
  paths:
    include:
    - src/*
    exclude:
    - docs/*

# PR 触发器
pr:
- main

# 定时触发
schedules:
- cron: "0 0 * * *"
  displayName: Daily midnight build
  branches:
    include:
    - main

构建配置 #

.NET 项目 #

yaml
trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

variables:
  buildConfiguration: 'Release'

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

- task: DotNetCoreCLI@2
  displayName: 'Restore'
  inputs:
    command: 'restore'

- task: DotNetCoreCLI@2
  displayName: 'Build'
  inputs:
    command: 'build'
    arguments: '--configuration $(buildConfiguration)'

- task: DotNetCoreCLI@2
  displayName: 'Test'
  inputs:
    command: 'test'
    arguments: '--configuration $(buildConfiguration) --no-build'

- task: DotNetCoreCLI@2
  displayName: 'Publish'
  inputs:
    command: 'publish'
    arguments: '--configuration $(buildConfiguration) --output $(Build.ArtifactStagingDirectory)'

- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact'
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'

Node.js 项目 #

yaml
trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: NodeTool@0
  inputs:
    versionSpec: '18.x'
  displayName: 'Install Node.js'

- script: |
    npm install
    npm run build
  displayName: 'npm install and build'

- script: |
    npm test
  displayName: 'Run tests'

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: 'dist'
    ArtifactName: 'drop'

Python 项目 #

yaml
trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: UsePythonVersion@0
  inputs:
    versionSpec: '3.11'
    addToPath: true

- script: |
    python -m pip install --upgrade pip
    pip install -r requirements.txt
  displayName: 'Install dependencies'

- script: |
    pip install pytest pytest-azurepipelines
    pytest
  displayName: 'Run tests'

多阶段流水线 #

阶段定义 #

yaml
trigger:
- main

stages:
- stage: Build
  jobs:
  - job: BuildJob
    steps:
    - script: echo Building...
    
- stage: Test
  dependsOn: Build
  jobs:
  - job: TestJob
    steps:
    - script: echo Testing...

- stage: Deploy
  dependsOn: Test
  condition: succeeded()
  jobs:
  - job: DeployJob
    steps:
    - script: echo Deploying...

环境部署 #

yaml
stages:
- stage: DeployDev
  displayName: 'Deploy to Dev'
  jobs:
  - deployment: Deploy
    environment: 'dev'
    strategy:
      runOnce:
        deploy:
          steps:
          - script: echo Deploying to Dev

- stage: DeployProd
  displayName: 'Deploy to Production'
  dependsOn: DeployDev
  condition: succeeded()
  jobs:
  - deployment: Deploy
    environment: 'production'
    strategy:
      runOnce:
        deploy:
          steps:
          - script: echo Deploying to Production

部署任务 #

Azure Web App 部署 #

yaml
- task: AzureWebApp@1
  inputs:
    azureSubscription: 'myServiceConnection'
    appName: 'myWebApp'
    package: '$(Build.ArtifactStagingDirectory)/**/*.zip'

Azure Function 部署 #

yaml
- task: AzureFunctionApp@1
  inputs:
    azureSubscription: 'myServiceConnection'
    appType: functionApp
    appName: 'myFunctionApp'
    package: '$(Build.ArtifactStagingDirectory)/**/*.zip'

AKS 部署 #

yaml
- task: KubernetesManifest@0
  inputs:
    action: 'deploy'
    kubernetesServiceConnection: 'myAKSConnection'
    namespace: 'default'
    manifests: |
      $(Build.SourcesDirectory)/k8s/deployment.yaml
      $(Build.SourcesDirectory)/k8s/service.yaml

变量和参数 #

定义变量 #

yaml
variables:
  buildConfiguration: 'Release'
  buildPlatform: 'x64'

# 变量组
variables:
- group: myVariableGroup

# 运行时参数
parameters:
- name: environment
  displayName: 'Target Environment'
  type: string
  default: 'dev'
  values:
  - dev
  - staging
  - production

使用变量 #

yaml
steps:
- script: echo $(buildConfiguration)
- script: echo $(parameters.environment)

服务连接 #

创建服务连接 #

text
┌─────────────────────────────────────────────────────────────┐
│                    服务连接类型                              │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  Azure Resource Manager                                      │
│  ├── 服务主体认证                                           │
│  ├── 工作负载身份                                           │
│  └── 管理 Azure 资源                                        │
│                                                             │
│  GitHub                                                      │
│  ├── OAuth 认证                                             │
│  └── 访问 GitHub 仓库                                       │
│                                                             │
│  Docker Registry                                             │
│  ├── Azure Container Registry                               │
│  ├── Docker Hub                                             │
│  └── 其他注册表                                             │
│                                                             │
│  Kubernetes                                                  │
│  ├── Azure Kubernetes Service                               │
│  └── 其他 Kubernetes 集群                                   │
│                                                             │
└─────────────────────────────────────────────────────────────┘

最佳实践 #

流水线设计 #

text
┌─────────────────────────────────────────────────────────────┐
│                    流水线最佳实践                            │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  1. 使用 YAML 定义                                           │
│     └── 版本控制,可复用                                    │
│                                                             │
│  2. 模块化设计                                               │
│     └── 使用模板复用代码                                    │
│                                                             │
│  3. 安全管理                                                 │
│     └── 使用变量组和 Key Vault                              │
│                                                             │
│  4. 并行执行                                                 │
│     └── 加快构建速度                                        │
│                                                             │
│  5. 缓存依赖                                                 │
│     └── 加速构建                                            │
│                                                             │
│  6. 质量门禁                                                 │
│     └── 测试覆盖率和代码分析                                │
│                                                             │
└─────────────────────────────────────────────────────────────┘

下一步 #

现在你已经掌握了 Azure Pipelines 的使用,接下来学习 实战案例 了解完整的应用部署!

最后更新:2026-03-29