Package: github-action-templates
Github Action TemplatesGithub Action Templates is a tool that allows for templating of yaml files to compose a github action.
This allows for re-use of jobs, steps and events.Quick StartCreate a new directory for your templates.mkdir-p.action_templates/{jobs,steps,events}Templated jobs should go in thejobsdirectory.# .action_templates/jobs/hello-world.yamlHelloWorld:if:success()runs-on:ubuntu-latestNote:stepscan be ommited if you are also providing a additional steps templates.There should be asingle jobper file.Templated steps go under thestepsdirectory.The contents of this file should be a yaml array with the step items to be templated.# .action_templates/steps/python-setup.yaml-uses:actions/setup-python@v2with:python-version:'3.6'-run:pip install -r requirements.txt-run:python my-script.pyNoteThestepskey is ommited, we just provide the contents.Templated events go in theeventsdirectory# .action_templates/events/on-pull-request-master.yamlpull_request:branches:-masterCreate the Template file to pull everything together# .action_templates/my-github-action.yamlname:"MyGithubAction"jobs:-template:hello-worldif:success()steps:-template:python-setupif:always()events:-template:on-pull-request-masterGenerate the templatepythonmain.py-t.action_templates/my-github-action.yamlThe generated contents will bename:My Github Actionon:pull_request:branches:-masterjobs:HelloWorld:if:success()runs-on:ubuntu-lateststeps:# template: python-setup-uses:actions/setup-python@v2with:python-version:'3.6'if:always()-run:pip install -r requirements.txtif:always()-run:python my-script.pyif:always()Alternatively, install the python package and use the provided library in your applications.pipinstall.

python

>>>fromghat.templateimporttemplate_github_action
>>>template_github_action(".action_templates/my-github-action.yaml"){'name':'My Github Action','on':{'pull_request':ordereddict([('branches',['master'])])},'jobs':{'HelloWorld':ordereddict([('if','success()'),('runs-on','ubuntu-latest'),('steps',[ordereddict([('uses','actions/setup-python@v2'),('with',ordereddict([('python-version','3.6')])),('if','always()')]),ordereddict([('run','pip install -r requirements.txt'),('if','always()')]),ordereddict([('run','python my-script.py'),('if','always()')])])])}}>>>Structure of the template fileFieldTypeDescriptionSample ValuenameStringName of the GitHub Action.My Github ActionjobsArrayArray of jobs which the Github Action will containjobs[n].templateStringLocation of the file to the template file..action_templates/jobs/hello-world.yamljobs[n].ifStringContents of theif:condition that will be used for this job.github.event.pull_request.head.repo.full_name == '<some-value>'jobs[n].stepsArrayArray of step template objectsjobs[n].steps[n].templateStringLocation of the file to the template file.python-setupjobs[n].steps[n].ifStringContents of theif:condition that will be used forall stepsin this template.always()eventsArrayArray of events, these correspond to theonsection of a workflow file.events[n].templateStringFull or relative path to the template file. Can be just the name if the default location (.action_templates/events) is used.on-pull-request-masterNote on file locations.File location can be the absolute or relative path of a file. An optional argument is accepted to thetemplate_github_action. This accepts a directory to search for template files.
Just the name of the file can be provided if it exists within this directory and has a .yml or .yaml extension.A url can also be used as long as the response contains valid yaml. E.g.https://raw.githubusercontent.com/mongodb/mongodb-kubernetes-operator/master/.action_templates/jobs/tests.yaml