Skip to content

Commit 1149b92

Browse files
authored
Merge pull request #6 from cuchi/env-vars
Enable the usage of environment variables
2 parents 6fa4650 + c15d4e8 commit 1149b92

File tree

11 files changed

+142
-16
lines changed

11 files changed

+142
-16
lines changed

.github/workflows/ci.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,49 @@ jobs:
5555
bar= \nthis\nvariable\nhas\nmany\n \nlines\n and trailing whitespace
5656
- name: '[test] many variables with messy whitespace'
5757
run: diff test/whitespace/result test/whitespace/expected
58+
59+
- name: '[run] yaml data file'
60+
uses: ./
61+
with:
62+
template: test/many-variables/template
63+
output_file: test/data-files/yaml-result
64+
data_file: test/data-files/data.yml
65+
- name: '[test] yaml data file'
66+
run: diff test/data-files/yaml-result test/many-variables/expected
67+
68+
- name: '[run] ini data file'
69+
uses: ./
70+
with:
71+
template: test/many-variables/template
72+
output_file: test/data-files/ini-result
73+
data_file: test/data-files/data.yml
74+
- name: '[test] ini data file'
75+
run: diff test/data-files/ini-result test/many-variables/expected
76+
77+
- name: '[run] json data file'
78+
uses: ./
79+
with:
80+
template: test/many-variables/template
81+
output_file: test/data-files/json-result
82+
data_file: test/data-files/data.yml
83+
- name: '[test] json data file'
84+
run: diff test/data-files/json-result test/many-variables/expected
85+
86+
- name: '[run] env data file'
87+
uses: ./
88+
with:
89+
template: test/many-variables/template
90+
output_file: test/data-files/env-result
91+
data_file: test/data-files/data.yml
92+
- name: '[test] env data file'
93+
run: diff test/data-files/env-result test/many-variables/expected
94+
95+
- name: '[run] env variables'
96+
uses: ./
97+
with:
98+
template: test/env-variables/template
99+
output_file: test/env-variables/result
100+
env:
101+
FOO: bar
102+
- name: '[test] env variables'
103+
run: diff test/env-variables/result test/env-variables/expected

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
FROM dinutac/jinja2docker:2.1.3
22

3+
RUN pip install \
4+
j2cli==0.3.10 \
5+
pyyaml==5.3.1
6+
37
COPY entrypoint.py /entrypoint.py
48

59
ENTRYPOINT ["/entrypoint.py"]

README.md

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
[![release](https://img.shields.io/github/v/release/cuchi/jinja2-action?style=flat-square)](https://github.com/cuchi/jinja2-action/releases/latest)
22
[![marketplace](https://img.shields.io/badge/marketplace-jinja2--action-blue?logo=github&style=flat-square)](https://github.com/marketplace/actions/jinja2-action)
33

4-
Jinja2 is a fast and straightforward templating engine.
4+
Jinja2 is a fast and straightforward templating engine. You can use this action
5+
to easily run it in your GitHub workflows.
56

6-
You can use this action to easily run the [Jinja2 CLI](https://github.com/mattrobenolt/jinja2-cli) inside your repository.
77

8-
9-
# Example
8+
# Using input variables
109
```yml
1110
- name: Setup nginx
12-
uses: cuchi/jinja2-action@v1.1.0
11+
uses: cuchi/jinja2-action@v1.2.0
1312
with:
1413
template: infra/nginx.conf.j2
1514
output_file: infra/nginx.conf
@@ -19,5 +18,35 @@ You can use this action to easily run the [Jinja2 CLI](https://github.com/mattro
1918
timeout=30s
2019
```
2120
21+
# Using data files
22+
```yml
23+
- name: Setup nginx
24+
uses: cuchi/jinja2-action@v1.2.0
25+
with:
26+
template: infra/nginx.conf.j2
27+
output_file: infra/nginx.conf
28+
data_file: staging_config.json
29+
data_format: json # Will try to guess from the extension instead (unnecessary in this case)
30+
```
31+
32+
# Using environment variables
33+
```yml
34+
- name: Setup nginx
35+
uses: cuchi/jinja2-action@v1.2.0
36+
with:
37+
template: infra/nginx.conf.j2
38+
output_file: infra/nginx.conf
39+
env:
40+
SERVER_HOST: staging.example.com
41+
```
42+
43+
Environment variables are used this way in the template file:
44+
```
45+
{{ env['SERVER_HOST'] }} <-- This is always strict
46+
```
47+
```
48+
{{ env.get('SERVER_HOST') }} <-- This is never strict, and displays `None` if you don't specify a default value
49+
```
50+
2251
# See also
2352
- [Jinja2 docs](https://jinja.palletsprojects.com/)

action.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
name: 'Jinja2 Action'
2-
description: 'Use the Jinja2 templating engine'
2+
description: 'Use the Jinja2 templating engine on your workflow'
33
inputs:
44
template:
55
required: true
66
output_file:
77
required: true
88
strict:
9+
description: 'Fails on missing values'
910
required: false
1011
default: false
1112
variables:
1213
required: false
1314
data_file:
15+
description: 'See "data_format" for supported formats'
1416
required: false
17+
data_format:
18+
description: 'To be used with the "data_file" input, can be one of: env, ini, yaml or json'
19+
required: false
20+
default: yaml
1521
runs:
1622
using: 'docker'
1723
image: 'Dockerfile'

entrypoint.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
11
#!/usr/bin/env python3
22

33
import os
4-
import re
5-
import subprocess
4+
from jinja2 import Template, StrictUndefined
5+
from j2cli.context import read_context_data
66

7-
params = [os.environ['INPUT_TEMPLATE']]
7+
def guess_format(file_name):
8+
_, extension = os.path.splitext(file_name)
9+
print(extension)
10+
formats = {
11+
'.yaml': 'yaml',
12+
'.yml': 'yaml',
13+
'.json': 'json',
14+
'.ini': 'ini',
15+
'.env': 'env',
16+
}
17+
return formats.get(extension, 'env')
818

19+
variables = {'env': os.environ}
920
for variable in os.environ.get('INPUT_VARIABLES', '').split('\n'):
1021
clean_variable = bytes(variable.strip(), 'utf-8').decode('unicode_escape')
1122
if clean_variable != '':
12-
params.extend(['-D', clean_variable])
23+
name, value = clean_variable.split('=', 1)
24+
variables.update({name: value})
1325

14-
if os.environ.get('INPUT_STRICT') == 'true':
15-
params.append('--strict')
26+
data_file = os.environ.get('INPUT_DATA_FILE')
27+
if data_file:
28+
format = os.environ.get('INPUT_DATA_FORMAT', guess_format(data_file))
29+
with open(data_file, 'r') as file:
30+
variables.update(read_context_data(format, file, None))
1631

17-
params.extend(['-o', os.environ['INPUT_OUTPUT_FILE']])
32+
with open(os.environ['INPUT_TEMPLATE'], 'r') as file:
33+
template_kwargs = {}
34+
if os.environ.get('INPUT_STRICT') == 'true':
35+
template_kwargs.update({'undefined': StrictUndefined})
36+
template = Template(str(file.read()), **template_kwargs)
1837

19-
params.extend([os.environ.get('INPUT_DATA_FILE','')])
20-
21-
subprocess.run(['jinja2'] + params, check = True)
38+
with open(os.environ['INPUT_OUTPUT_FILE'], 'w') as file:
39+
file.write(template.render(**variables) + '\n')

test/data-files/data.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
foo=bar
2+
baz=cux

test/data-files/data.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
foo=bar
2+
baz=cux
3+
4+
[someting_else]
5+
foo=bar2

test/data-files/data.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"foo": "bar",
3+
"baz": "cux"
4+
}

test/data-files/data.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
foo: bar
2+
baz: cux

test/env-variables/expected

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bar
2+
3+
None
4+
5+
hello

0 commit comments

Comments
 (0)