提问者:小点点

如何通过部署作业在Azure YAML管道中定位环境?


目标

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

YAML

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

  1. 构建
  2. 测试
  3. 发布神器
  4. 将工件部署到RO-TST环境中的资源(VM的内部部署)
# CI/CD Pipeline
# https://learn.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

我尝试过其他部署策略,如滚动和金丝雀,但它们不适用于环境范围的目标。下面是微软关于部署作业的留档。

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

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


共1个答案

匿名用户

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

https://learn.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\'