Is there a JJB variable with the name of the builder?

13 views
Skip to first unread message

Aleksey Tsalolikhin

unread,
Jul 19, 2022, 2:14:40 PM7/19/22
to jenkins-job-builder
Hello,

I'd like to be able to reference the name of the builder within the builder shell code.

For example:

- builder:
    name: test-builder
    builders:
        - shell:
            echo Builder {builder_name} is starting
            echo hello world
This builder should output:
Builder test-builder is starting
hello world
Is this possible in JJB?
Thanks,
Aleksey





--
Aleksey Tsalolikhin
Sr. Systems Engineer
 LinkedIn  •   Instagram  •   Youtube
 

Eric Ball

unread,
Jul 19, 2022, 2:29:25 PM7/19/22
to jenkins-job-builder
Hi Aleksey. Just based on how YAML works, you can reference that value with {name}

Aleksey Tsalolikhin

unread,
Jul 19, 2022, 2:33:18 PM7/19/22
to Eric Ball, jenkins-job-builder
That makes sense. Thanks, Eric!!

Aleksey

--
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/4505a4c8-a2b8-4993-91aa-62a45661b7efn%40googlegroups.com.

Aleksey Tsalolikhin

unread,
Jul 19, 2022, 3:22:45 PM7/19/22
to Eric Ball, jenkins-job-builder
Given the following JJB YAML:

- builder:
    name: builder-test
    builders:
      - shell:
          echo {name}

- job:
    name: test
    builders:
      - builder-test

I am expecting to see "echo builder-test" in the generated job; but JJB-4.1.0 generates:

  <builders>
    <hudson.tasks.Shell>
      <command>echo {name}</command>
    </hudson.tasks.Shell>

Did I do something wrong?

Thanks,
Aleksey

Aleksey Tsalolikhin

unread,
Jul 25, 2022, 2:42:44 PM7/25/22
to jenkins-job-builder
I also tried this, but no luck:

---

- builder:
    name: builder-test
    builders:
      - shell: echo '{name}'

- job:
    name: test
    builders:
      - builder-test


Still seeing:

  <builders>
    <hudson.tasks.Shell>
      <command>echo '{name}'</command>
    </hudson.tasks.Shell>
  </builders>

in the output.

Thanks,
Aleksey

Lesley Kimmel

unread,
Aug 1, 2022, 5:01:44 PM8/1/22
to jenkins-job-builder
Aleksey;

There's a note in the documentation that states if you use a parameterized macro but pass it no parameters the replacement will NOT be performed which explains why you are seeing what you are. I assume you might be wanting to do what you are asking because you might be wanting to use the name of the builder in several locations in the shell macro. You might be just as well off to statically assign the builder name to a shell variable at the top of your script:

- builder:
    name: builder-test
    builders: 
      - shell: |
          BUILDER_NAME=builder-test
          echo "Builder ${{BUILDER_NAME}} is starting"

Doesn't seem like the best solution but I don't see anything indicating you can do what you're trying to do.

Darragh Bailey

unread,
Aug 2, 2022, 3:55:53 AM8/2/22
to Lesley Kimmel, jenkins-job-builder


On Mon 1 Aug 2022, 22:01 Lesley Kimmel, <lesley....@gmail.com> wrote:
Aleksey;

There's a note in the documentation that states if you use a parameterized macro but pass it no parameters the replacement will NOT be performed which explains why you are seeing what you are. I assume you might be wanting to do what you are asking because you might be wanting to use the name of the builder in several locations in the shell macro. You might be just as well off to statically assign the builder name to a shell variable at the top of your script:

It's been a while, however I recall that if you add a colon after the macro when calling it, it will be treated as though it is given parameters, even if you don't pass anything else in.

---
- builder:
    name: builder-test
    builders:
      - shell: echo '{name}'

- job:
    name: test
    builders:
      - builder-test:

----------------------^ (extra colon)

This (or something similar) did work at one stage, whether it still works or not, I'm not sure as I'm not using JJB anymore.

I also recall a suggestion to always treat macros as having parameters being passed in, might be worth revisiting with an option to enable this for projects without impacting existing usage.

--
Darragh Bailey
"Nothing is foolproof to a sufficiently talented fool" - unknown

Aleksey Tsalolikhin

unread,
Aug 3, 2022, 5:06:03 PM8/3/22
to Darragh Bailey, Lesley Kimmel, jenkins-job-builder
Thanks! I tried the colon, no go.  I also tried passing a parameter, no go:

---
- builder:
    name: builder-test
    builders:
      - shell: echo {name} {foo}


- job:
    name: test
    builders:
      - builder-test:
          foo: bar

returns

      <command>echo  bar</command>

What I'm trying to do is to have each shell builder announce its name to facilitate reading the build logs and facilitate troubleshooting.  Ideally without hardcoding the builder name, but I'm not above hardcoding it if I can't do it natively with JJB.  

Thanks,
Aleksey

--
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.
Reply all
Reply to author
Forward
0 new messages