Skip to content
file-text

GitHub Action

Template Render Action

v1.0.2 Latest version

Template Render Action

file-text

Template Render Action

A github action to render ejs/mustache template files using github context

Installation

Copy and paste the following snippet into your .yml file.

              

- name: Template Render Action

uses: recih/[email protected]

Learn more about this action in recih/template-render-action

Choose a version

Template Render Action

build-test

A github action to render ejs/mustache template files using github context.

This action is inspired by kikyous/template-action, with some additional features. Thanks to kikyous!

Input:

  • template-file: Input template file path.
  • template: Input template string.
    • You must specify template-file or template
  • vars: [optional] Input variables. A dictionary of variables in JSON format to be used in the template. Or specify a .yml/.yaml/.json file path.
  • engine: [optional] Choose template engine. Default is ejs.
    • Currently, you can choose ejs or mustache
  • options: [optional] A JSON format string of options to be passed to the template engine.
    • For example, {pretty: true}
  • output-file: [optional] Output file path.
    • If not specified, you can get rendered result from the action output with ${{ steps.<step-id>.outputs.content }}.
  • glob: [optional] Glob mode. Default is false. If true, glob mode is enabled:
    • template-file will be considered as a glob pattern (see @actions/glob).
    • template/output-file will be ignored.
    • content field in action output will not be set.
    • The output file name will be the same as the input file name, without the file extension. e.g., 'data.json.template' will be rendered to 'data.json'

Output:

  • content: template render result. (Only when glob mode is disabled)

Usage

name: Example for basic usage
on:
  push

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: recih/template-render-action@v1
        id: template
        with:
          template: |
            <% context.payload.commits.forEach(function(c){ %>
            [✅ <%= c.message %>](<%= c.url %>)\n
            <% }); %>
            commiter: <%= context.payload.head_commit.author.name %>
          vars: |
            { "name": "${{ steps.stepId.outputs.name }}" }

      - name: Get the render output
        run: echo "${{ steps.template.outputs.content }}"
name: Example for glob mode
on:
  push

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: recih/template-render-action@v1
        with:
          template-file: "**/*.template"
          glob: true

Template syntax

Render context

Following objects are exposed, and can be used in template file:

  • context: The Context object in @actions/github
  • env: The process.env object. You can access the environment variables with env.<key>

you can explore context use below action

name: Test
on:
  push

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: recih/template-render-action@v1
        id: template
        with:
          template: "<%- JSON.stringify(context, undefined, 2) %>"

      - name: Get the render output
        run: echo "${{ steps.template.outputs.content }}"

TODO

  • Support more template engines