Skip to content

Use Jinja2 template engine on multipe files as a GitHub action

License

Notifications You must be signed in to change notification settings

fletort/jinja2-template-action

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

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jinja2-template-action

Use Jinja2 template engine on multipe files as a GitHub action.

Continuous Testing Coverage Status Testspace tests count

This is a very simple version of the action, that is ok for my first need. Futur enhancement will come.

The actual "simple" version transform all local j2 files (recursively) ('*.j2') with the jinja2 library.

The new file name is the same filename without the j2 extension. For exemple, README.md.j2 becomes README.md.

It this version, it can only be used with environment variable, see my test template file.

Environement variable as used as with the jinja2 cli, with a kind of environ method :

{{ environ('TEST') }}

Usage

Using Environment Variable

- uses: fletort/jinja2-template-action@v1
  env:
    TEST: my_value

Environment variables are used as with jinja2 cli:

{{ environ('TEST') }}

Or as other contextual github information:

{{ env.TEST }}

Using Input Variables

- uses: fletort/jinja2-template-action@v1
  with:
    variables: |
      TEST1=mytest
      TEST2=isfunny

Using Data Files

- uses: fletort/jinja2-template-action@v1
  with:
    data_file: my_data.json
    data_format: json # can be detected automatically (see below)

Possible data type are: env, yaml, json, ini

1. env file

EXEMPLE_SMART=yoyo
EXEMPLE_MY_VAR=True

2. ini

[EXEMPLE]
SMART=yoyo
MY_VAR=True

3. yaml

---
EXEMPLE:
  SMART=yoyo
  MY_VAR=True

4. json

{
  "EXEMPLE": {
    "SMART": "yoyo",
    "MY_VAR": True
  }
}

Related Jinja Template

For previous INI, YAML, JSON examples, jinja template will be:

{{ EXEMPLE.SMART }}
{{ EXEMPLE.MY_VAR }}

For previous ENV example, jinja template will be:

{{ EXEMPLE_SMART }}
{{ EXEMPLE_MY_VAR }}

Using Workflow Github contextual Information

Some of the contextual information about workflow runs are available inside your jinja Template:

  • github
  • job
  • runner
  • strategy
  • matrix

As part of an expression, you can access context information using one of two syntaxes. Index syntax: github['sha'] Property dereference syntax: github.sha

{{ github.repository }}
{{ job.status }}
{{ runner.os }}
{{ strategy.job_index }}
{{ matrix.your_matrix_variable_name }}

Note: All strategy information key contains dashes that must me marked as underscore in jinja expression: ${{ strategy.job-index }} becomes {{ strategy.job_index }}.

Actions inputs

Name Description Default
variables Variable to substitute in the jinja templates. Must be Key, value pairs in .env file format (key=value). ""
keep_template Put to true to keep original template file. false
data_file Source file contening inputs variable for the jinja template. ""
data_format Format of the data_file. Can be env, ini, yaml, json or automatic (for automatic detection). The automatic detction is based on the extension then on the content. automatic

Code Quality

All unit test executed on each branch/PR are listed/described on testspace.

Coverage information and history is also avalailable on coveralls.

License

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

About

Use Jinja2 template engine on multipe files as a GitHub action

Resources

License

Stars

Watchers

Forks

Packages

No packages published