Builder > Shell question

22 views
Skip to first unread message

Enrique Valenzuela

unread,
Jun 6, 2024, 11:17:26 AM6/6/24
to jenkins-job-builder
Hi team!
Kinda new to the game and I have been migrating an older version of JJB to the 6.3.0 for a team, while doing this, I found that we had a lot of repetitive bash scripting, so I want to add it in a shared script and then add that into almost every shell of my job templates but when doing that, I end up with 2 shells (because the include tag doesn't allow me to include and add inline scripting).

Thus, I was wondering if there is a way to have a shell in the builder section to include both scripts and also a shell, for example:

Right now I have the following code:
======= CODE ====
- job-template:
    id: apb_build_application_v0
    name: 'BuildApplication-{job_name}'
    folder: '{job_path}'
    description: 'BuildApplication-{job_name}'
    project-type: freestyle

    wrappers:
      - timestamps
      - ansicolor:
          colormap: xterm
      - build-user-vars
      - workspace-cleanup
    builders:
      - shell:
          !include-raw-verbatim:
            - functions.sh
      - shell: |
          #!/bin/bash -e
          # echo_notification function defined in the above functions.sh file
          echo_notification hello

==== END CODE ===
due to having 2 shell in the builders, its defined as 2 distinct steps, so I was wondering if I can do something like:
==== CODE ====

builders:
  - shell: 
      !include-raw-verbatim:
         - functions.inc.sh
      - |
         echo_notification hello
=== END CODE

Any idea on how to achieve that that is not "adding all the additional code" to other scripts and just adding them in a list for the include tags?

Thanks for your time and help!
Best
Enrique

Vsevolod Fedorov

unread,
Jun 7, 2024, 1:09:41 PM6/7/24
to Enrique Valenzuela, jenkins-job-builder

Hi, Enrique.

One approach is to create your custom shell macro, which would receive both include list and actual script (not tested, just an example):

Macro file:
----------------------------------
builder:
  name: my-shell
  builders:
  - shell: !j2: |
      #/bin/bash -e
      {{ for file in includes }}
      source "{{ file }}"
      {{ endfor }}
      {{ shell-script }}
----------------------------------

Job template file:

----------------------------------
builders:
- my-shell:
    includes:
    - functions-1.sh
    - functions-2.sh
    shell-script:

           # echo_notification function defined in the above functions.sh file
      echo_notification hello

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


You can also use defaults for includes, or have some of them hardcoded in macro, etc.

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/fa546e47-e2b6-4f1a-a05a-c6738b257052n%40googlegroups.com.

Enrique Valenzuela

unread,
Jun 7, 2024, 5:11:54 PM6/7/24
to jenkins-job-builder
Hi Vsevolod, 
Thanks for the macro idea! Will give it a whirl!
Best

Reply all
Reply to author
Forward
0 new messages