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
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=1024
When using key-value pairs format, the action automatically converts values to appropriate types:
true
,True
,yes
,y
,1
→ booleantrue
false
,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: true
Then 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 or YAML file with variables | No | - |
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'
- 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 files
If 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=value
with 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_path
is 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: false
during development
You can provide variables in either JSON or YAML 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
# 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 }}
Contributions 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.