This GitHub Action allows you to render Jinja2 templates with custom variables. AMD64 and ARM64 architectures are supported.
- âś… Render single templates or process entire directories
- âś… Load variables from JSON or YAML files
- âś… Pass variables directly as JSON string or key-value pairs
- âś… Automatic type conversion for key-value pairs
- âś… Recursive directory processing
- âś… Custom file extension support
- âś… Multi-architecture support (amd64 and arm64)
- âś… Docker-based for consistent execution
- âś… Detailed error reporting with strict mode support
name: Render Templates
on:
push:
branches: [ main ]
jobs:
render:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Render Jinja2 Templates
uses: lexty/jinja2-renderer@v1
with:
template_path: './templates'
output_path: './output'
variables_file: './variables.json'- name: Render Jinja2 Templates with .env
uses: lexty/jinja2-renderer@v1
with:
template_path: './templates'
output_path: './output'
variables_file: '.env'Your .env file:
APP_NAME=MyApplication
VERSION=1.2.3
DEBUG=true
PORT=8080- name: Render Jinja2 Templates
uses: lexty/jinja2-renderer@v1
with:
template_path: './templates/config.conf.j2'
output_path: './output/config.conf'
variables: '{"app_name": "MyApp", "version": "1.2.3", "debug": true}'The action supports three formats for key-value variables:
- name: Render Config
uses: lexty/jinja2-renderer@v1
with:
template_path: 'templates/nginx.conf.j2'
output_path: 'output/nginx.conf'
variables: |
server_name=example.com
port=80
max_connections=1024- name: Render Config
uses: lexty/jinja2-renderer@v1
with:
template_path: 'templates/nginx.conf.j2'
output_path: 'output/nginx.conf'
variables: 'server_name=example.com port=80 max_connections=1024'- name: Render Config
uses: lexty/jinja2-renderer@v1
with:
template_path: 'templates/nginx.conf.j2'
output_path: 'output/nginx.conf'
variables: >-
server_name=example.com
port=80
max_connections=1024When using key-value pairs format, the action automatically converts values to appropriate types:
true,True,yes,y,1→ booleantruefalse,False,no,n,0→ booleanfalse- Numbers (e.g.,
123) → integers - Decimal numbers (e.g.,
12.34) → floats - All other values → strings
- name: Render with Environment Variables
uses: lexty/jinja2-renderer@v1
with:
template_path: 'templates/app.conf.j2'
output_path: 'output/app.conf'
env:
APP_NAME: MyAwesomeApp
APP_VERSION: 1.0.0
DEBUG_MODE: trueThen in your template file:
# Configuration for {{ env.APP_NAME }}
version = {{ env.get('APP_VERSION') }}
debug = {{ env.get('DEBUG_MODE', 'false') }}
| Input | Description | Required | Default |
|---|---|---|---|
template_path |
Path to the Jinja2 template file or directory | Yes | - |
output_path |
Path where rendered files will be saved | Yes | - |
variables_file |
Path to a JSON, YAML, or .env file with variables | No | - |
variables_file_format |
Explicit format specification for variables_file (json, yaml, env). If not specified, format is auto-detected by file extension |
No | auto-detect |
variables |
JSON string or key=value pairs for variables | No | - |
environment_variables |
Use environment variables in templates | No | true |
recursive |
Process templates in subdirectories recursively | No | false |
file_pattern |
Glob pattern for finding template files | No | *.j2 |
strict |
Enable strict undefined variable checking | No | false |
trim_blocks |
Configure Jinja2 to trim blocks | No | true |
lstrip_blocks |
Configure Jinja2 to strip blocks | No | true |
| Output | Description |
|---|---|
rendered_files |
JSON list of rendered template files |
For more detailed examples with template files and configurations, check out the examples documentation.
- name: Render Config Template
uses: lexty/jinja2-renderer@v1
with:
template_path: './templates/nginx.conf.j2'
output_path: './output/nginx.conf'
variables_file: './variables.yaml'- name: Process Template Directory
uses: lexty/jinja2-renderer@v1
with:
template_path: './templates'
output_path: './output'
variables_file: './variables.json'
recursive: 'true'- name: Render in Strict Mode
uses: lexty/jinja2-renderer@v1
with:
template_path: './templates/config.j2'
output_path: './output/config'
variables_file: './variables.json'
strict: 'true'- name: Process Template Files
uses: lexty/jinja2-renderer@v1
with:
template_path: './templates'
output_path: './output'
variables_file: './variables.json'
file_pattern: '*.template'If your variables file has a non-standard extension, you can explicitly specify the format:
- name: Render with Explicit Format
uses: lexty/jinja2-renderer@v1
with:
template_path: './templates'
output_path: './output'
variables_file: './my-vars.txt'
variables_file_format: 'env' # Force treating as .env file- name: Render Templates
id: render
uses: lexty/jinja2-renderer@v1
with:
template_path: './templates'
output_path: './output'
variables_file: './variables.json'
- name: Use Rendered Files
run: |
echo "Rendered files: ${{ steps.render.outputs.rendered_files }}"
# Do something with the rendered filesIf you're seeing parsing errors with your variables:
- For JSON format, ensure your JSON is valid with properly quoted keys and values
- For key-value pairs, use the format
key=valuewith spaces between pairs - Try using a variables file instead of inline variables for complex data
If no templates are being rendered:
- Check that your template files match the
file_pattern(default is*.j2) - Verify the
template_pathis correct - If using
recursive: true, ensure templates are in the expected subdirectories
When using strict: true, all variables used in templates must be defined. If you get errors:
- Make sure all variables referenced in your templates are provided
- Use
{{ variable | default('fallback') }}in templates for optional variables - Consider setting
strict: falseduring development
You can provide variables in JSON, YAML, or .env format:
{
"app_name": "MyApp",
"version": "1.2.3",
"features": ["auth", "api", "admin"],
"database": {
"host": "localhost",
"port": 5432
}
}app_name: MyApp
version: 1.2.3
features:
- auth
- api
- admin
database:
host: localhost
port: 5432# Application configuration
APP_NAME=MyApp
VERSION=1.2.3
DEBUG=true
PORT=8080
# Database settings
DB_HOST=localhost
DB_PORT=5432
DB_NAME="my database"
# Multiline values (quoted)
DESCRIPTION="This is a
multiline
description"Note: .env files support:
- Comments (lines starting with
#) - Quoted values with spaces (
KEY="value with spaces") - Single and double quotes
- Multiline values (in quoted strings)
- Escape sequences (
\n,\t, etc.) - Automatic type conversion (booleans, integers, floats)
# Configuration for {{ app_name }} v{{ version }}
app:
name: {{ app_name }}
version: {{ version }}
features:
{% for feature in features %}
- {{ feature }}
{% endfor %}
database:
host: {{ database.host }}
port: {{ database.port }}If you want to test the action locally or contribute to development, you'll need to install the required Python dependencies:
pip3 install jinja2 pyyaml python-dotenvThen you can run the entrypoint script directly:
# Set required environment variables
export INPUT_TEMPLATE_PATH="./templates/test.j2"
export INPUT_OUTPUT_PATH="./output/test.conf"
export INPUT_VARIABLES_FILE="./variables.env"
# Run the script
python3 entrypoint.pyFor testing with inline variables:
export INPUT_TEMPLATE_PATH="./templates/test.j2"
export INPUT_OUTPUT_PATH="./output/test.conf"
export INPUT_VARIABLES='{"key": "value"}'
python3 entrypoint.pyContributions are welcome! Please feel free to submit a Pull Request.
All notable changes to this project will be documented in the CHANGELOG.md file.
This project is licensed under the MIT License - see the LICENSE file for details.