Skip to main content

Template GitHub Actions

Project description

Github Action Templates

Github 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 Start

Create a new directory for your templates.

mkdir -p .action_templates/{jobs,steps,events}

Templated jobs should go in the jobs directory.

# .action_templates/jobs/hello-world.yaml
HelloWorld:
  if: success()
  runs-on: ubuntu-latest

Note:

  • steps can be ommited if you are also providing a additional steps templates.
  • There should be a single job per file.

Templated steps go under the steps directory.

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@v2
  with:
    python-version: '3.6'
- run: pip install -r requirements.txt
- run: python my-script.py

Note

  • The steps key is ommited, we just provide the contents.

Templated events go in the events directory

# .action_templates/events/on-pull-request-master.yaml
pull_request:
  branches:
    - master

Create the Template file to pull everything together

# .action_templates/my-github-action.yaml
name: "My Github Action"
jobs:
  - template: hello-world
    if: success()
    steps:
    - template: python-setup
      if: always()
events:
  - template: on-pull-request-master

Generate the template

python main.py -t .action_templates/my-github-action.yaml

The generated contents will be

name: My Github Action
on:
  pull_request:
    branches:
    - master
jobs:
  HelloWorld:
    if: success()
    runs-on: ubuntu-latest
    steps:
    # template: python-setup
    - uses: actions/setup-python@v2
      with:
        python-version: '3.6'
      if: always()
    - run: pip install -r requirements.txt
      if: always()
    - run: python my-script.py
      if: always()

Alternatively, install the python package and use the provided library in your applications.

pip install .

python

>>> from ghat.template import template_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 file

Field Type Description Sample Value
name String Name of the GitHub Action. My Github Action
jobs Array Array of jobs which the Github Action will contain
jobs[n].template String Full or relative path to the template file. Can be just the name if the default location (.action_templates/jobs) is used. .action_templates/jobs/hello-world.yaml
jobs[n].if String Contents of the if: condition that will be used for this job. github.event.pull_request.head.repo.full_name == '<some-value>'
jobs[n].steps Array Array of step template objects
jobs[n].steps[n].template String Full or relative path to the template file. Can be just the name if the default location (.action_templates/steps) is used. python-setup
jobs[n].steps[n].if String Contents of the if: condition that will be used for all steps in this template. always()
events Array Array of events, these correspond to the on section of a workflow file.
events[n].template String Full or relative path to the template file. Can be just the name if the default location (.action_templates/events) is used. on-pull-request-master

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

github-action-templates-0.0.2.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

github_action_templates-0.0.2-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file github-action-templates-0.0.2.tar.gz.

File metadata

  • Download URL: github-action-templates-0.0.2.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for github-action-templates-0.0.2.tar.gz
Algorithm Hash digest
SHA256 5741380a79d1c80c172f9c35a16b896814e43876a79bb3bf496ce03c0befc694
MD5 49aa8c7501c429b356e1ee09a57986b5
BLAKE2b-256 120b590fb92b06d22c43a02e24efcb66040ccc002077ed599c5a13fd6e2b1b0a

See more details on using hashes here.

File details

Details for the file github_action_templates-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: github_action_templates-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for github_action_templates-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 bdd52a096f80e63c524a31e4dbc97cfe0f21eae52713a38891a36a864d0c4205
MD5 fd29c5d0ebe33a6880923a8152b75036
BLAKE2b-256 eea4e6a59f8bbb6e89997fe3aafd1401753fe4048fbfc42a13653d21072762f4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page