【问题标题】:How can you target environments in a Azure YAML Pipeline via deployment job?如何通过部署作业定位 Azure YAML 管道中的环境?
【发布时间】:2020-09-10 15:01:47
【问题描述】:

目标

使用部署作业通过 Azure YAML 将管道工件部署到环境中的 VM 资源。

YAML

这是我正在使用的完整 YAML 管道。有了这个 YAML 文件,我希望实现以下目标。

  1. 构建
  2. 测试
  3. 发布工件
  4. 将工件部署到 RO-TST 环境中的资源(本地虚拟机)

# CI/CD Pipeline
# https://docs.microsoft.com/azure/devops/pipelines/apps/windows/dot-net

trigger:
- master

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

stages:

- stage: BuildTestPublishArtifact
  displayName: Build - Test - Publish Artifact
  jobs:
  - job: Build
    steps:
    - task: NuGetToolInstaller@1
    - task: NuGetCommand@2
      inputs:
        restoreSolution: '$(solution)'
    - task: VSBuild@1
      inputs:
        solution: '$(solution)'
        platform: '$(buildPlatform)'
        configuration: '$(buildConfiguration)'
    - task: VSTest@2
      inputs:
        platform: '$(buildPlatform)'
        configuration: '$(buildConfiguration)'
    - task: PublishPipelineArtifact@1
      inputs:
        targetPath: '$(System.DefaultWorkingDirectory)\HelloWorld\HelloWorld\bin\$(buildConfiguration)'
        artifact: 'HelloWorld'
        publishLocation: 'pipeline'

- stage: DeployTst
  displayName: Deploy to TST
  jobs:
  - deployment: Deployment
    environment: RO-TST
    strategy:
      runOnce:
        deploy:
          steps:
          - task: CopyFiles@2
            inputs:
              SourceFolder: '$(Pipeline.Workspace)'
              Contents: '**'
              TargetFolder: 'D:\Application\'

结果

步骤 1 到 3 运行良好。在步骤 4(部署作业)中,复制文件任务未在 RO-TST 环境中注册的资源代理上运行。但复制文件任务正在托管代理上运行。

作业初始化:

Starting: Initialize job
Agent name: 'Hosted Agent'
Agent machine name: 'fv-az686'
Current agent version: '2.168.2'
Operating System
Virtual Environment
Current image version: '20200517.1'
Agent running as: 'VssAdministrator'
Prepare build directory.
Set build variables.
Download all required tasks.
Downloading task: DownloadPipelineArtifact (1.2.4)
Downloading task: CopyFiles (2.164.0)
Downloading task: CmdLine (2.164.0)
Checking job knob settings.
   Knob: AgentToolsDirectory = C:/hostedtoolcache/windows Source: ${AGENT_TOOLSDIRECTORY} 
   Knob: AgentPerflog = c:\vsts\perflog Source: ${VSTS_AGENT_PERFLOG} 
Finished checking job knob settings.
Start tracking orphan processes.
Finishing: Initialize job

当我以环境中的特定资源 (RO-TST.APP1234) 为目标时,复制文件任务确实在资源代理上运行。这是通过将部署作业中的环境值更改为 RO-TST.APP1234 来完成的。

- stage: DeployTst
  displayName: Deploy to TST
  jobs:
  - deployment: Deployment
    environment: RO-TST.APP1234
    strategy:
      runOnce:
        deploy:
          steps:
          - task: CopyFiles@2
            inputs:
              SourceFolder: '$(Pipeline.Workspace)'
              Contents: '**'
              TargetFolder: 'D:\Application\'

作业初始化:

Starting: Initialize job
Agent name: 'APP1234'
Agent machine name: 'APP1234'
Current agent version: '2.168.2'
Agent running as: 'APP1234$'
Prepare build directory.
Set build variables.
Download all required tasks.
Checking job knob settings.
Finished checking job knob settings.
Start tracking orphan processes.
Finishing: Initialize job

我尝试过其他部署策略,例如滚动和金丝雀,但它们不适用于环境范围的目标。在 Microsoft 有关部署作业的文档下方。

https://docs.microsoft.com/en-us/azure/devops/pipelines/process/deployment-jobs?view=azure-devops

我知道您可以通过“经典”方法使用部署组,在 Azure DevOps 中通过 YAML 和 CD 分离 CI。但我真的很想在一个 YAML 文件中拥有完整的 CI-CD 管道。那么我是在部署作业的设置方式上遗漏了什么,还是无法通过环境在 YAML 中定位多个资源?

【问题讨论】:

    标签: azure-devops continuous-integration yaml azure-pipelines continuous-deployment


    【解决方案1】:

    所以我终于找到了为什么我的 YAML 不适用于部署工作。感谢 VM 资源文档中给出的示例。

    https://docs.microsoft.com/en-us/azure/devops/pipelines/process/environments-virtual-machines?view=azure-devops#reference-vm-resources-in-pipelines

    这是修改后的 YAML,我在其中添加了属性名称、资源类型和标签。我的环境资源上已经有标签,所以效果很好。运行管道后,工件被部署到带有标签控制台的 RO-TST 中的所有资源。

    - stage: DeployTst
      displayName: Deploy to TST
      jobs:
      - deployment: Deployment
        environment: 
          name: RO-TST
          resourceType: VirtualMachine
          tags: Console
        strategy:
          runOnce:
            deploy:
              steps:
              - task: CopyFiles@2
                inputs:
                  SourceFolder: '$(Pipeline.Workspace)'
                  Contents: '**'
                  TargetFolder: 'D:\Application\'
    

    【讨论】:

    • 感谢您在这里分享您的解决方案,请您接受您的解决方案as the answer?因此,对于遇到相同问题的其他成员轻松找到解决方案将很有帮助。祝你有美好的一天:)
    • @HughLin-MSFT,yaml 管道的文档质量有待提高。
    猜你喜欢
    • 2021-02-27
    • 2020-01-30
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    • 2022-01-03
    • 2020-08-14
    相关资源
    最近更新 更多