首页 > 解决方案 > Azure YAML 脚本 - 生成和发布

问题描述

我们可以在 Azure DevOps 的同一个 YAML 脚本中构建和发布吗?如果是,有人可以帮我编写示例脚本吗?对于发布部分,我们正在部署到多个环境。

标签: azureazure-devopsazure-pipelines-yaml

解决方案


我们有多种方式通过 Azure DevOps 启动构建和发布我们的配置。

首先,我们将着眼于以两种不同的 yml 格式构建 CI 和 CD,并创建两个管道来自动化我们的任务。为此,我们将在 YAML 管道中使用资源。

现在我们将通过创建新的 DevOps 项目和新的存储库来创建构建管道,可以在下面的 yaml 文件中添加 build-pipeline.yml

trigger:
  branches:
    include:
    - master
  paths:
    exclude:
    - build-pipeline.yml
    - release-pipeline.yml

variables:
  vmImageName: 'ubuntu-latest'

jobs:  
- job: Build
  pool:
    vmImage: $(vmImageName)
  steps:
  - script: | 
      echo 'do some unit test'
    displayName: 'unit test'
  - script: | 
      echo 'compile application'
    displayName: 'compile'
  - task: ArchiveFiles@2
    displayName: 'Archive files'
    inputs:
      rootFolderOrFile: '$(System.DefaultWorkingDirectory)'
      includeRootFolder: false
      archiveType: zip
      archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
      replaceExistingArchive: true
  - upload: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
    artifact: drop

同样,我们创建一个发布管道,下面是 yml 格式:

# Explicitly set none for repositry trigger
trigger:
- none

resources:
  pipelines:
  - pipeline: myappbuild  # Name of the pipeline resource
    source: myapp-build-pipeline # Name of the triggering pipeline
    trigger: 
      branches:
      - master

variables:
  vmImageName: 'ubuntu-latest'

jobs:
- deployment: Deploy
  displayName: Deploy
  environment: dev
  pool: 
    vmImage: $(vmImageName)
  strategy:
    runOnce:
      deploy:
        steps:          
        - download: myappbuild
          artifact: drop  
        - task: ExtractFiles@1
          inputs:
            archiveFilePatterns: '$(PIPELINE.WORKSPACE)/myappbuild/drop/$(resources.pipeline.myappbuild.runID).zip'
            destinationFolder: '$(agent.builddirectory)'
            cleanDestinationFolder: false
        - script: |  
            cat $(agent.builddirectory)/greatcode.txt

在这里我们可以理解为先运行构建管道,然后运行发布管道,这些管道可以与多个部署链接。

此外,我们还有一些有用的博客的更多信息,例如c-sharpcorneradamtheautomater。感谢博主。


推荐阅读