Skip to content

Commit

Permalink
Check jinja templates for syntax error (kubernetes-sigs#10667)
Browse files Browse the repository at this point in the history
Allow to fail early (pre-commit time) for jinja error, rather than
waiting until executing the playbook and the invalid template.

I could not find a simple jinja pre-commit hook in the wild.
  • Loading branch information
VannTen authored Dec 6, 2023
1 parent fe02d21 commit d2944d2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitlab-ci/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ ansible-lint:
- ansible-lint -v
except: ['triggers', 'master']

jinja-syntax-check:
extends: .job
stage: unit-tests
tags: [light]
script:
- "find -name '*.j2' -exec tests/scripts/check-templates.py {} +"
except: ['triggers', 'master']

syntax-check:
extends: .job
stage: unit-tests
Expand Down
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,12 @@ repos:
entry: tests/scripts/md-table/test.sh
language: script
pass_filenames: false

- id: jinja-syntax-check
name: jinja-syntax-check
entry: tests/scripts/check-templates.py
language: python
types:
- jinja
additional_dependencies:
- Jinja2
9 changes: 9 additions & 0 deletions tests/scripts/check-templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python

import sys
from jinja2 import Environment

env = Environment()
for template in sys.argv[1:]:
with open(template) as t:
env.parse(t.read())

0 comments on commit d2944d2

Please sign in to comment.