title: How can you target environments in a Azure YAML Pipeline via deployment job?
body: <p><strong>Goal</strong></p>
  
  <p>Deploy pipeline artifact to VM resources in a Environment via Azure YAML using Deployment job.</p>
  
  <p><strong>YAML</strong></p>
  
  <p>This is the complete YAML pipeline that i'm using. With this YAML file i'm hoping to achieve the following. </p>
  
  <ol>
  <li>Build</li>
  <li>Test</li>
  <li>Publish artifact</li>
  <li>Deploy artifact to resources in RO-TST Environment (VM's on premise)</li>
  </ol>
  
  <hr>
  
  <pre><code># 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\'
  </code></pre>
  
  <p><strong>Result</strong></p>
  
  <p>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.</p>
  
  <p>Job initialization:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>- 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\'
  </code></pre>
  
  <p>Job initialization:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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.</p>
  
  <p><a href="https://docs.microsoft.com/en-us/azure/devops/pipelines/process/deployment-jobs?view=azure-devops" rel="noreferrer">https://docs.microsoft.com/en-us/azure/devops/pipelines/process/deployment-jobs?view=azure-devops</a></p>
  
  <p>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?</p>