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

时间:2020-05-23 19:32:57

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

目标

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

YAML

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

  1. 构建
  2. 测试
  3. 发布工件
  4. 将工件部署到RO-TST环境中的资源(VM内置)

# 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

我知道您可以通过“经典”方法来使用“部署”组,通过“ YAML”和“ CD”通过Azure DevOps中的发行版来分隔CI。但是我真的很想在一个YAML文件中包含完整的CI-CD管道。那么,我是否缺少设置部署作业的方式,还是无法通过环境针对YAML中的多个资源?

1 个答案:

答案 0 :(得分: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,其中添加了属性名称,resourceType和标记。我已经在我的环境资源上添加了标签,因此效果很好。在运行管道之后,使用标记控制台将工件部署到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\'