Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing VSTS Release Multi-Configuration Phase two variables, but only one multiplier

I have a VSTS Release Definition that is passed two variables, the contents of which I do not control. They contain a comma separated string of Names and a comma separated string of IDs which correspond to the Names by index.

I want to use an agent phase configured with the parallelism option set to multi-configuration and the multiplier field set to the string of IDs, so the phase is run once for each ID. I also want to use the name that corresponds to the ID in that phase, but I'm not sure how to do this.

If I set the multiplier to both variables (ID and Name) it runs the phase for the Cartesian product (cross join) of the two arrays which is not desired.

Example:

                    IDs: "A1, A2, A3"
                  Names: "Anna, Adam, Abby"
 Runs the phase 9 times: "A1" & "Anna", "A2" & "Anna", "A3" & "Anna"
                         "A1" & "Adam", "A2" & "Adam", "A3" & "Adam"
                         "A1" & "Abby", "A2" & "Abby", "A3" & "Abby"

If I set the multiplier to the ID variable only it runs the phase the correct number of times, but I can't figure out how to pass the corresponding name into the phase.

Is this even possible?

Thanks in advance!

like image 647
msdedwards Avatar asked Jan 03 '23 05:01

msdedwards


2 Answers

To make the multi-configuration phase run once for each item in the IDs variable, and make the both the current ID and name available for use in the phase:

  1. Set the Multipliers field to the IDs variable as Marina Liu suggested. This runs the phase once for each ID with the current ID stored in the IDs variable.
  2. Add an empty variable named CurrentName this will store the name associated with the IDs variable.

  3. Add an inline Powershell script task into the Agent Phase containing the following commands:

$names = "$(Names)".Split(", ",[System.StringSplitOptions]::RemoveEmptyEntries) Write-Host "##vso[task.setvariable variable=CurrentName]$($names[[int]"$(SYSTEM.JOBPOSITIONINPHASE)" - 1])"

The script uses the built in System.JobPositionInPhase variable, which contains the index of the phase currently being run, to get the name for the current phase for the from the Names variable and stores it in the CurrentName variable.

You can use $(CurrentName) anywhere in the phase to get the value of the name associated with $(IDs).

like image 164
msdedwards Avatar answered Jan 04 '23 19:01

msdedwards


If you want to run the phase for three times (A1-Anna, A2-Adam and A3-Abby), you just need to specify one variable (no matter for variable IDs or Names) as Multipliers and filter the corresponding item from the other variable.

Such as use variable IDs as Multipliers and filter corresponding name from variable Names each time, you can use below steps:

1. Specify IDs as Multipliers

enter image description here

2. Get the ID for each time run on the agent phase

After specifying variable IDs as Multipliers, the agent will run three times: A1, A2 and A3.

And you can get relate ID by variable $(IDs) when run on the agent phase each time:

  • When run the phase A1, the variable $(IDs) value is A1;
  • When run the phase A2, the variable $(IDs) value is A2;
  • When run the phase A3, the variable $(IDs) value is A3.

3. Filter corresponding name from Names variable when each time run on the agent phase

Since you can get the ID for each time to run, you can get corresponding name with the ID by some defined rules. Such as use if condition to get the correct name, or parse ID's index and get the corresponding name etc.

like image 38
Marina Liu Avatar answered Jan 04 '23 17:01

Marina Liu