gitness icon indicating copy to clipboard operation
gitness copied to clipboard

Jsonnet nested template data

Open mckunda opened this issue 3 years ago • 1 comments

Overview

This PR provides ability to put nested YAML arrays and mappings as serialized JSON string values into jsonnet's vm.ExtVar function and use it later in the jsonnet pipeline template with std.parseJson.

Example pipeline:

kind: template
load: plugin.jsonnet
data:
  stepName: my_step
  image: my_image
  commands: my_command
  additionalSteps:
    - name: my_second_step
      image: my_second_image
      commands:
        - my_second_command1
        - my_second_command2
    - name: my_third_step
      image: my_third_image
      commands:
        - my_third_command

Example jsonnet template:

local stepName = std.extVar("input.stepName");
local image = std.extVar("input.image");
local commands = std.extVar("input.commands");
local additionalSteps = std.parseJson(std.extVar("input.additionalSteps"));

{
  "kind": "pipeline",
  "type": "docker",
  "name": "default",
  "steps": [
    {
      "name": stepName,
      "image": image,
      "commands": [
        commands
      ]
    }
  ] + [step for step in additionalSteps]
}

Processed output:

---
{
   "kind": "pipeline",
   "name": "default",
   "steps": [
      {
         "commands": [
            "my_command"
         ],
         "image": "my_image",
         "name": "my_step"
      },
      {
         "commands": [
            "my_second_command1",
            "my_second_command2"
         ],
         "image": "my_second_image",
         "name": "my_second_step"
      },
      {
         "commands": [
            "my_third_command"
         ],
         "image": "my_third_image",
         "name": "my_third_step"
      }
   ],
   "type": "docker"
}

Of course, the above examples seem a bit of a stretch and make the new functionalty look redundant, but those are for testing purposes only. With the power of jsonnet we can implement much more complex templating solutions for a broader set of use cases. I can try and work on the real-world example if you want.

mckunda avatar Feb 23 '22 16:02 mckunda

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Feb 23 '22 16:02 CLAassistant

This PR has been automatically closed due to inactivity.

bot2-harness avatar Dec 19 '23 19:12 bot2-harness