Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It is possible to convert a json to variables/parameters YAML and iterate? #599

Closed
giseliramos opened this issue Aug 24, 2023 · 3 comments
Closed
Assignees
Labels
Area: Yaml question Further information is requested

Comments

@giseliramos
Copy link

Hello,

I have a powershell script in a stage that creates a jsonObject and I save it to a variable to be reused later:

$imagesJson = @"
[
{
   "myAcr": "someCustomAcr1",
   "listImages": "image1, image2"
},
{
   "myAcr": "someCustomAcr2",
   "listImages": "image4, image5, image6"
}
]
"@

# < some operations I do on imagesObj, already converted >

$newJson = ConvertTo-Json $imagesObj -Compress
"##vso[task.setvariable variable=ImagesList;isOutput=true]$newJson"

This part works okay. The matter is that I want to populate some YAML variables with it or iterate, for example, I want to iterate for each "dict" here and extract the info. Is that possible to do in the YAML, to populate on variables or on parameters template?

Thanks!

@KonstantinTyukalov
Copy link

Hi @giseliramos, because these are runtime pipeline variables, you can't use YAML template syntax (which is compile-time) with them.
More info about runtime(+macro) and compile variables and differences between them shared in this doc

Could you clarify your use case with more details? Probably we'll be able to find a suitable solution for you

@KonstantinTyukalov KonstantinTyukalov added question Further information is requested and removed triage labels Sep 3, 2023
@KonstantinTyukalov KonstantinTyukalov self-assigned this Sep 3, 2023
@giseliramos
Copy link
Author

giseliramos commented Sep 4, 2023

Hello @KonstantinTyukalov, the scenario I have:

  • One stage where some runtime variables are defined in a powershell script and written, for example:
##vso[task.setvariable variable=$SourceAcr1;isOutput=true]$source1
  • The number is variable (not known before the run), so I may have $SourceAcr1, $SourceAcr2 in one run and in another run have 1 or 3, for example. That is why I tried the json to use to iterate between the keys.
  • In subsequent stages, I recover these variables using the stage dependencies with:
AcrOrigin1: $[ stageDependencies.Stage1.Job1.outputs['TaskName.SourceAcr1'] ]

. The problem is that I want to avoid hard-coding it, like as sourceAcr1, sourceAcr2. So I need to estimate a max number, like, max 5 variables and check for each (hard-coded) if they are not empty in the "condition" of job/task.

  • I can get a number of the defined variables, like as numberPopulated and try to loop. It is possible to use this information and loop, like as in:
copy the numberPopulated to variables and expand as in a array, from 1 to numberPopulated
- ${{ each index in numberPopulatedArray }}:
  - ${{ if eq($[stageDependencies....SourceAcr$index])}}:
    - template: myCustomJob.yml

Is that possible? Or I'll need to hard-copy and check for each variable? For now, I'm doing the following for each variable, but it is cumbersome:

      - task: PowerShell@2
        condition: ne(variables.AcrOrigin1, 'placeholder')
        displayName: 'Debug'
        inputs:
          targetType: 'inline'
          script: |
            Write-Host "Hey it worked!"

@max-zaytsev
Copy link
Contributor

Such questions should be posted on the Developer Community, since this repo is mainly about the templates located in this repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Yaml question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants