Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 403
Remco Oomen Avatar asked May 23 '20 19:05

Remco Oomen


People also ask

How do you set the environment on an azure pipeline?

There are 3 ways to get an environment variable value on your build server: Set the value on the build machine. Set the value in the YAML build script. Set the value in Azure DevOps for the build pipeline definition.

What are the available options for deployment environments provided by Azure?

Environments can include Kubernetes clusters, Azure web apps, virtual machines, databases. Typical examples of environment names are Dev, Test, QA, Staging, and Production.

How does Azure pipelines automatically create the environment for a YAML pipeline?

When you author a YAML pipeline and refer to an environment that does not exist in the YAML file, Azure Pipelines automatically creates the environment in some cases: You use the YAML pipeline creation wizard in the Azure Pipelines web experience and refer to an environment that hasn't been created yet.

What is a deployment job in YAML pipelines?

Each job in a stage must have a unique name. In YAML pipelines, we recommend that you put your deployment steps in a special type of job called a deployment job. A deployment job is a collection of steps that are run sequentially against the environment. A deployment job and a traditional job can exist in the same stage.

How to use a deployment job in Azure DevOps?

A deployment job can be used to target an entire environment (group of resources) or a particular resource within the environment. Step 1: Create a new Environment in Azure Pipeline. Login to your Azure DevOps portal and navigate to Azure Pipeline for the Project that we have created during the setup.

How do I deploy an Azure App service from a YAML?

Set the cursor on a new line at the end of the YAML definition. This will be the location where new tasks are added. Select the Azure App Service Deploy task. Select the Azure subscription where you created the app service earlier. Click Authorize and follow the path to complete authorization.


1 Answers

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\'
like image 186
Remco Oomen Avatar answered Oct 16 '22 16:10

Remco Oomen