Hello,
I’m using template.
I’m currently writing inline JSON that I parse with “std.parseJson” in Jsonnet. This seems unnecessary.
kind: template
load: test.jsonnet
data:
builds: >
[
{
"name": "build1"
},
{
"name": "build2"
}
]
In my opinion it would be better to just specify as below.
kind: template
load: test.jsonnet
data:
builds:
- name: build1
- name: build2
And then parse from Jsonnet.
local builds = std.parseJson(std.extVar("input.builds"));
...
Right now objects and lists doesn’t seem to get serialized. I guess my question is, could Drone serialize them as JSON before parsing args to Jsonnet?
Right now objects and lists doesn’t seem to get serialized.
this is by design because, unlike jsonnet, starlark is able to handle nested data. If you want to work with nested template data you might consider starlark instead.
Do you have any example on how to parse this nested data using starlark?
it doesn’t need to be parsed, it is loaded into starlark as structured data. for example ctx.input.builds[0]
Well that seems like a better solution. Will try it out.
Thanks for the help.
I can’t get it to work in Starlark either. If i specify ctx.input.builds[0] or just ctx.input.builds i get the error:
"input" struct has no .builds attribute
If I don’t use nested list or object, it works.
.drone.yml I’m using.
kind: template
load: test.starlark
data:
builds:
- name: build1
- name: build2
Hey @mortenfryd
I’ve identified the issue and will be pushing out a fix soon.
Cheers,
Eoin
Hey @mortenfryd
Would it be possible for you to provide me with the desired end state, the drone.yml & template you wish to get to? Not the entire end state but a working example so I can make sure the change has fully met the requirement.
Cheers,
Eoin
Sure.
I have this simple template that should do the job.
.drone.yml
kind: template
load: plugin.starlark
data:
name: "stepname"
env:
foo: foo
bar: bar
cmds:
- echo foo bar
- ls
plugin.starlark
def main(ctx):
return {
"kind": "pipeline",
"name": "build",
"steps": [
{
"name": ctx.input.name,
"image": "alpine",
"environment": ctx.input.env,
"commands": ctx.input.cmds
}
]
}
Also it would be nice if it worked with nested objects in lists, like below. But i assume that will work too… maybe
?
.drone.yml
kind: template
load: plugin.starlark
data:
name: "stepname"
builds:
- name: test123
- name: test1234
@mortenfryd
If you pull the latest, I’ve added this enhancement to templating.
Thanks. Will check it out asap.
Hi @eoinmcafee00,
I can confirm that the fix works as expected.
/Morten