Skip to content

roamingowl/template-output-with-eta

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Repository files navigation

Render template to output variable with Eta

CI CodeQL Coverage Quality Gate Status Esbuild Badge Code Smells

Description

A JavaScript action that renders a string template into an output variable using the ETA library.

Inputs

Name Description Type Required Default
template

Template string to render (supports ETA syntax) or a path to a file containing the template.

string true ""
varName

Name of the variable that holds all data to be used in the template (variables).

string false it
variables

Variables to substitute in the template. You can use YAML, JSON, or dotenv format. See examples

string false ""

Outputs

Name Type Description
text string

Rendered template string.

Enhancements

You can use functions from the date-fns library to format dates inside the template. Use any function with the utils.dateFns prefix. For example, to format a date, you can use utils.dateFns.format(it.date, "MM/dd/yyyy HH:mm:ss").

In addition to the default functions, you can use the UTCDateMini class to work with UTC date objects. You can use this class directly (without the utils prefix). For example, to format a date, you can use UTCDateMini(it.date).

[!INFO] The heavier alternative UTCDate class is not available in this action for now.

Example formatting timestamp:

steps:
  - name: Render template with formatted date
    uses: roamingowl/template-output-with-eta@v1
    with:
      template: |
        Formatted date is <%= utils.dateFns.format(new UTCDateMini(it.timestamp * 1000), "MM/dd/yyyy HH:mm:ss") %>
      variables: |
        timestamp: 1711187861

will produce output like:

Formatted date is 03/23/2024 09:57:41

Usage Examples

Variables in YAML format:

steps:
  - name: Render a simple template with variables in YAML format
    uses: roamingowl/template-output-with-eta@v1
    with:
      template: |
        <%= it.what %> this is <%= it.name %>
      variables: |
        name: 'John'
        what: 'hi'

Variables in JSON format:

steps:
  - name: Render a simple template with variables in JSON format
    uses: roamingowl/template-output-with-eta@v1
    with:
      template: |
        <%= it.what %> this is <%= it.name %>
      variables: |
        { "what": "hi", "name": "John" }

Variables in dotenv format:

steps:
  - name: Render a simple template with variables in dotenv format
    uses: roamingowl/template-output-with-eta@v1
    with:
      template: |
        <%= it.WHAT %> this is <%= it.NAME %>
      variables: |
        WHAT=hi
        NAME=John

Load template from a file:

steps:
  - name: Render a simple template from a file
    uses: roamingowl/template-output-with-eta@v1
    with:
      template: ./template.txt
      variables: |
        what: hi
        name: John

Print the date difference between two timestamps in minutes:

steps:
  - name: Render the difference between two timestamps in minutes
    uses: roamingowl/template-output-with-eta@v1
    id: render
    with:
      template: |
        The difference is <%= Math.abs(utils.dateFns.differenceInMinutes(new Date(it.t1 * 1000), new Date(it.t2 * 1000))) %> minutes
      variables: |
        t1: 1711187861
        t2: 1711188041
  - name: Print the difference
    shell: bash
    run: echo "Difference is ${{ steps.render.outputs.text }}"

License

The scripts and documentation in this project are released under the MIT License.

About

GitHub action that renders a template to output string

Resources

License

Stars

Watchers

Forks

Contributors 3

  •  
  •  
  •