Search code examples
azure-devopsyamlazure-pipelinesdevopsazure-pipelines-yaml

Read Data From Xml and Assign the values in Variable section in Yaml File in Azure DevOps


I was trying to read the data from the xml file and take backup of the each file from the location in azure DevOps and Create a artifact

I have a xml with this format and have to read the files data from the file

<?xml version="1.0" encoding="utf-16"?>
<configdatastorage>
  <Files>
    <configfile filename="File1.zip"/>
    <configfile filename="File2.zip"/>
<!--<configfile filename="File3.zip"/>
    <configfile filename="File4.zip"/>
    <configfile filename="File5.zip"/>-->
  </Files>
 </configdatastorage>

I have created a PowerShell script for extracting the value

$XMLfile = "$(Build.SourcesDirectory)\Test.xml"
[XML]$empDetails = Get-Content $XMLfile
$FileNames = ''
foreach($empDetail in $empDetails.configdatastorage.Files.configfile)
{
    $Solution=$empDetail.filename.split(".")
    $FileNames =$FileNames + $Solution[0] + ","
}

Write-Host $fulllength
$FilesRequired = $FileNames.Substring(0,$FileNames.Length-1)
Write-Host "##vso[task.setvariable variable=FileNames;]$FilesRequired"

Now i want to use these values into iterating for each value to take backup of each file from the location

I have created a variable in the pipeline with

# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
    
trigger:
- none

pool:
  vmImage: ubuntu-latest

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $XMLfile = "$(Build.SourcesDirectory)\Test.xml"
      [XML]$empDetails = Get-Content $XMLfile
      $FileNames = ''
      foreach($empDetail in $empDetails.configdatastorage.Files.configfile)
      {
          $Files=$empDetail.filename.split(".")
          $FileNames =$FileNames + $Files[0] + ","
      }
      $FilesRequired = $FileNames.Substring(0,$FileNames.Length-1)
      Write-Host "##vso[task.setvariable variable=FileNames;]$FilesRequired"

- ${{ each file in split(variables.FileNames, ',')}}:
  - script: echo backup ${{ file }}

This will not provide any output. I came to know the compile type and runtime variable section. where the values are set at different section. How to use the iteration in this case. Do we have any other approach to access the value from the Xml and iterate through the DevOps pipeline

Couldnt find a workaround for this


Solution

  • We can use the condition variable with condition if you need to filter. We cannot use the variable during runtime