Remco Oomen
Remco Oomen

Reputation: 231

How can you target environments in a Azure YAML Pipeline via deployment job?

Goal

Deploy pipeline artifact to VM resources in a Environment via Azure YAML using Deployment job.

YAML

This is the complete YAML pipeline that i'm using. With this YAML file i'm hoping to achieve the following.

  1. Build
  2. Test
  3. Publish artifact
  4. Deploy artifact to resources in RO-TST Environment (VM's on premise)

# 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\'

Result

Steps 1 through 3 are working just fine. In step 4 (deployment job) the copy files task is not running on the resource agents that are registered on the RO-TST environment. But instead the copy files task is running on the hosted agent.

Job initialization:

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

When I target a specific resource (RO-TST.APP1234) in the environment the copy file task does run on the resource agent. This is done by changing the environment value in the deployment job to 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\'

Job initialization:

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

I've tried other Deployment strategies like rolling and canary but they don't work with Environment scoped targets. Below the documentation from Microsoft regarding Deployment Jobs.

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

I know that you can use Deployment groups via the "classic" approach seperating CI via YAML and CD via Releases in Azure DevOps. But I would really love to have the complete CI-CD pipeline in one YAML file. So am I missing something in the way the deployment job is setup or is it just not possible to target multiple resources in YAML via Environments?

Upvotes: 11

Views: 13307

Answers (1)

Remco Oomen
Remco Oomen

Reputation: 231

So I finally found out why my YAML didn't work for the deployment job. Thanks to the example given in the VM resource documentation.

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

This is the modified YAML in which I added the properties name, resourceType and tags. I already had tags on my Environment resources so that worked out fine. After running the pipeline the artifacts were deployed to all resources in RO-TST with the tag Console.

- 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\'

Upvotes: 12

Related Questions