fresh-angle-76971
11/06/2025, 2:48 PMproviders.exec.outputs.initScript.log and use the last line as context for the kubernetes provider? Something like this:
context: ${split("${providers.exec.outputs.initScript.log}", "\n")[-1]}microscopic-river-74022
11/06/2025, 2:48 PMmicroscopic-river-74022
11/06/2025, 2:48 PMinitScript log and access its elements, including the last line. The split function works as follows:
${split("a,b,c", ",")} -> ["a","b","c"]
[source]()
The output of the initScript is available as ${providers.exec.outputs.initScript.log} for the exec provider [source]().
However, the documentation does not explicitly confirm support for negative indices (e.g., [-1] for the last element) in template string array access. The examples provided only show positive indices and do not mention negative indexing.
So, while you can split the log into lines, it is unclear from the documentation whether you can directly use [-1] to get the last line. If negative indices are not supported, you would need to use a workaround, such as ensuring your script outputs only the desired context line as the last line, and then always referencing a known positive index.
In summary:
- Splitting the log is supported.
- Accessing the last element with [-1] is not explicitly documented, so it may not work as expected.
Example usage (assuming positive indexing):
yaml
context: ${split("${providers.exec.outputs.initScript.log}", "\n")[0]}
If you want to use the last line and negative indices are not supported, you may need to adjust your script or use a different approach.
If you want to try negative indexing, you can test it, but be aware it is not guaranteed by the documentation.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------microscopic-river-74022
11/06/2025, 2:49 PMfresh-angle-76971
11/06/2025, 2:54 PM16:50:03 ❯ garden deploy
[verbose] garden version: 0.14.9
Deploy 🚀
ℹ garden → Initializing...
/Users/spike/gits/demo/project.garden.yml:31
...
30 | # https://github.com/garden-io/garden/issues/1477
31 | context: ${split("${providers.exec.outputs.initScript.log}", "\n")[0]}
-----------------^
Unable to parse as valid template string.
If I move the index reference outside the {} like so:
context: ${split("${providers.exec.outputs.initScript.log}", "\n")}[0]
the parser passes but I get the following error:
Could not read cluster from kubeconfig for context kubernetes-admin@dev-cluster-c21d8db[0]
If I remove the index reference completely, I get the following error:
Error validating provider configuration:
context must be a string
So splitting seems to be working but index references do not...microscopic-river-74022
11/06/2025, 2:54 PMmicroscopic-river-74022
11/06/2025, 2:54 PM${split(...)[0]} or ${split(...)[-1]}) is not supported. This is confirmed by a [GitHub issue]() where users encountered the same error when trying to use array indexing directly after split. The workaround suggested is to assign the result of split to a variable and then access the desired index:
yaml
variables:
arr: ${split("something.else", ".")}
Then use:
yaml
context: ${var.arr[1]}
But directly using an index on the result of split inside a template string is not supported and will result in a parsing error, as you have experienced.
Additionally, negative indices (e.g., [-1] for the last element) are not supported in Garden’s templating engine, and there is no documented way to get the last element of an array in a single template expression.
Summary:
- You can split a string and assign the result to a variable.
- You can access array elements by index from a variable (e.g., ${var.arr[1]}).
- You cannot use array indexing directly on the result of split in a template string (e.g., ${split(...)[0]} will fail).
- Negative indices are not supported.
Workaround:
If you need the last line, you must ensure your script outputs only the desired line, or you must know the exact index in advance. There is no built-in way to get the last element of an array in a single template expression in Garden as of now.microscopic-river-74022
11/06/2025, 2:54 PM