j2 / j2-yaml for job-template parameters

117 views
Skip to first unread message

Paride Legovini

unread,
Jun 19, 2023, 11:46:02 AM6/19/23
to jenkins-job-builder
Hi, I am trying to use jinja2 to construct a list in a project stanza to be used to realize a job-template, however apparently the list endss up "literal" in the job definition. This example will clarify:

-------------------------------------------------------------------
$ cat test.yaml
- project:
    name: foo
    param: !j2: "{{ [1, 2] }}"
    jobs:
      - bar-{param}

- project:
    name: foo2
    param: !j2-yaml: |
      {% for x in [3, 4] %}
      - {{ x }}
      {% endfor %}
    jobs:
      - bar-{param}

- job-template:
    name: bar-{param}
-------------------------------------------------------------------

$ jenkins-jobs test test.yaml 2>&1 | grep "Job name" 2>/dev/null
INFO:jenkins_jobs.builder:Job name:  bar-[1, 2]
INFO:jenkins_jobs.builder:Job name:  bar-[3, 4]

-------------------------------------------------------------------

What I'd expect are of course the following jobs: bar-1, bar-2, bar-3, bar-4. Am I missing the correct usage of j2 or is this somehow not supported by JJB? This is with JJB 5.0.2:

$ jenkins-jobs --version
Jenkins Job Builder version: 5.0.2

Thanks!

Vsevolod Fedorov

unread,
Jun 19, 2023, 12:26:36 PM6/19/23
to Paride Legovini, jenkins-job-builder
Hi, Paride.

!j2 and !j2-yaml are not suitable nor are needed here. This should work:

- project:
    name: foo
    param: [1, 2]
    jobs:
      - bar-{param}

- project:
    name: foo2
    param:
      - 3
      - 4
    jobs:
      - bar-{param}

- job-template:
    name: bar-{param}


Vsevolod.

Paride Legovini

unread,
Jun 19, 2023, 12:46:46 PM6/19/23
to jenkins-job-builder
Hello and thanks for your reply. I know j2 is not really needed in my example, I just wanted to make it as minimal as possible.

Vsevolod Fedorov

unread,
Jun 19, 2023, 12:52:00 PM6/19/23
to Paride Legovini, jenkins-job-builder

No problem.

Just make longer example to show what your actual problem is.


Vsevolod.

--
You received this message because you are subscribed to the Google Groups "jenkins-job-builder" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkins-job-bui...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkins-job-builder/ddce7948-b092-4d51-87e2-e7c24c1391f3n%40googlegroups.com.

Paride Legovini

unread,
Jun 19, 2023, 3:15:19 PM6/19/23
to jenkins-j...@googlegroups.com
I was experimenting with generating jobs using an external file
containing newline-separated strings (the output of another tool as you
can imagine):

- project:
name: foo
rawlist: !include-raw: list.inc
param: !j2-yaml: |
{% for element in rawlist.split() %}
- {{ element }}
{% endfor %}
jobs:
- bar-{param}

- job-template:
name: bar-{param}

I know this is ugly, and I'm probably ditching this idea in favor of
having list.inc in yaml format, skipping jinja2. However I still find
that !j2-yaml does not behave as I would expect from the documentation
("it loads the Jinja-rendered string as YAML and embeds it in the
calling YAML construct").

Paride
>> https://groups.google.com/d/msgid/jenkins-job-builder/ddce7948-b092-4d51-87e2-e7c24c1391f3n%40googlegroups.com <https://groups.google.com/d/msgid/jenkins-job-builder/ddce7948-b092-4d51-87e2-e7c24c1391f3n%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "jenkins-job-builder" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to jenkins-job-bui...@googlegroups.com
> <mailto:jenkins-job-bui...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jenkins-job-builder/27cc3951-3848-92a9-2a22-83ca092bfa1c%40networkoptix.com <https://groups.google.com/d/msgid/jenkins-job-builder/27cc3951-3848-92a9-2a22-83ca092bfa1c%40networkoptix.com?utm_medium=email&utm_source=footer>.

Reply all
Reply to author
Forward
0 new messages