Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template front end #85

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

abutcher-gh
Copy link

@abutcher-gh abutcher-gh commented Oct 27, 2018

Potentially relevant to #2, though it's more of a idea to throw out there; I'm not sure it's exactly what was intended.

It's working well for us and supports the user's choice of templating engine. Indeed, they could choose to use (or combine from different sources in one configrepo) different templating engines.

The only requirement is that you have a script or program to process files with a user-defined naming pattern.

Example

Example config: (mustache.gocd-yaml-generator.yaml)

generators:
  mustache-inline:
    pattern: '**/*.gocd.mi.yaml'
    script: |
      mustache-inline "{{file}}"

Example input: (test.gocd.mi.yaml)

---
branches:
    - name: master
    - name: demo
---
pipelines:
  {{#branches}}
  mcu-firmware.{{name}}:
    group: stuff
    label_template: "mcu-firmware-{{name}}.${COUNT}"
    materials:
      mcu-firmware:
        git: http://my-server/git/mcu-firmware
        branch: {{name}}
    stages:
      - build:
          clean_workspace: true
          artifacts:
           - build:
               source: out/mcu-firmware-*
               destination: mcu-firmware
          tasks:
           - script: |
              PATH="/usr/local/crosstool/gcc-arm-none-eabi-7-2018-q2-update/bin:$PATH"
              sh ./build.sh
  {{/branches}}

Note the file does not have to be combined (data and template) as the example above shows. That was just convenient for demo.

Usage of "inclusion" within the chosen templating engine would allow the instantiating file to specify the data and a reference to the template to use. With mustache something similar to

---
branches:
    - name: master
    - name: demo
---
pipelines:
  {{#branches}}
  {{> first-pipeline}}
  {{> second-pipeline}}
  {{/branches}}

Where first-pipeline.mustache and second-pipeline.mustache are local templates for particular pipelines. They inherit the scope of the branches "loop" (i.e. can use name in this case).

I think this strategy gives a lot of flexibility. I just wanted to put it out there to see if there was any interest in getting it (or something similar) merged into the main plugin.

Example config: (mustache.gocd-yaml-generator.yaml)

   generators:
     mustache-inline:
       pattern: '**/*.gocd.mi.yaml'
       script: |
         mustache-inline "{{file}}"

Example input: (test.gocd.mi.yaml)

   ---
   branches:
       - name: master
       - name: demo
   ---
   pipelines:
     {{#branches}}
     mcu-firmware.{{name}}:
       group: stuff
       label_template: "mcu-firmware-{{name}}.${COUNT}"
       materials:
         mcu-firmware:
           git: http://my-server/git/mcu-firmware
           branch: {{name}}
       stages:
         - build:
             clean_workspace: true
             artifacts:
              - build:
                  source: out/mcu-firmware-*
                  destination: mcu-firmware
             tasks:
              - script: |
                 PATH="/usr/local/crosstool/gcc-arm-none-eabi-7-2018-q2-update/bin:$PATH"
                 sh ./build.sh
     {{/branches}}
@tomzo
Copy link
Owner

tomzo commented Oct 28, 2018

Hi,

Thanks a lot. I really like the concept. This isn't exactly templates as in #2 because here, input parameters and templates have to be in the same repository. But I see how it is a powerful tool.

I think this strategy gives a lot of flexibility. I just wanted to put it out there to see if there was any interest in getting it (or something similar) merged into the main plugin.

I was thinking of something like it. generators seems like a good name.
If I understand correctly you use it when yaml references are not enough, e.g. to generate same pipeline for each branch like in the demo.

I am not sure if relying on having a pre-installed tool on server is correct approach. Usually GoCD plugins don't do that. I would rather support explicitly some template engines, using library such as jmustashe or jinjava

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants