Special variables; Index, First, Last

Hi I’m asking for help with these Variables

Octopus.Template.Each.Index
Octopus.Template.Each.First
Octopus.Template.Each.Last

I tried each of these in the code below but it didn’t work. Let me know what I’ve got wrong.
#{if Octopus.Template.Each.First == “True”} print for first package only.
#{/if}
#{if Octopus.Template.Each.First} print for first package only.
#{/if}
#{if package.First} print for first package only.
#{/if}
#{if package.First == “True”} print for first package only.
#{/if}

Specifically how to find the first or last in the list of packages. This doesn’t work but hopefully conveys intent. I couldn’t find similar post feel free to point me to one that already exists.
#{each package in Octopus.Release.Package}
#{if on first loop} print for first package only.
#{/if}
- #{package.PackageId } :#{package.Version}
#{each workItem in package.WorkItems}
1. #{workItem.Id} - #{workItem.Description}
#{/each}
#{each commit in package.Commits}
1. #{commit.CommitId} - #{commit.Comment}
#{/each}
#{/each}

1 Like

Hi Kenneth
Thanks for reaching out to us. Sorry to see that you’re having issues with our Special Variables.

That is definitely an interesting question, since as you say our docs are unfortunately a little light on specific examples for these Special Variables.

In the variables docs there is a different example to iterate over a comma separated list:

That might be a good use case to test the Special Variables indexing and see how the syntax works. I will try and reproduce this in our labs and see if it will work for you.

I see also that since it is a variable index, the standard index notation might also work, as in:

Octopus.Template.Each.First[0]

I will get back to you as soon as I have some tangible results.

Kind regards,
Paraic

1 Like

Hi Kenneth,
Going through a simple test case using the comma delimited array I was able to get the Special Variables to print out some values.

So to show what I did, I set a variable as comma delimited list of 3 items called TEST2 with values SV1, SV2, SV3.

I then created a process step for Run a Script just to print out the values using Special Variables.

#{each endpoint in TEST2}

#{if Octopus.Template.Each.First}
  write-host 'This is the first item in the Index : ' #{endpoint} 
#{/if}

#{if Octopus.Template.Each.Last}
  write-host 'This is the last item in the Index : ' #{endpoint} 
#{/if}

That gave me the result:

This is the first item in the Index :  SV1 

This is the last item in the Index :  SV3 

So its a True/False return and by rights your example should have worked. Can you try this simple example to make sure its evaluating correctly for you? Then we can look at the Package index to see where it might have gone wrong.

Kind Regards,
Paraic

1 Like

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.