Use CircleCI version 2.1 at the top of your .circleci/config.yml file.
1
version: 2.1
Add the orbs
stanza below your version, invoking the orb:
1
2
orbs:
jinja: jtreutel/jinja@1.1.0
Use jinja
elements in your existing workflows and jobs.
Opt-in to use of uncertified orbs on your organization’s Security settings page.
The orbs is called three times in the config below. Twice it is called as a command in the "jobs" section where it renders a single template and a directory of template. It is also called once as a job in the "workflows" section.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
version: '2.1'
orbs:
jinja: jtreutel/jinja@0.1.1
jobs:
render-directory:
executor: jinja/default
steps:
- checkout
- jinja/render:
outputdir: rendered/
path: templates/
render-file:
executor: jinja/default
steps:
- checkout
- jinja/render:
outputdir: rendered/
path: templates/foo.j2
workflows:
main:
jobs:
- render-file
- render-directory
- jinja/render:
outputdir: rendered/
path: templates/
This job renders Jinja templates at the specified paths.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
outputdir | Local path to which render Jinja templates will be written. | Yes | - | string |
path | Path to template file or directory containing .j2 or .jinja files. | Yes | - | string |
persist-workspace | Persist files to (cci) workspace for use in subsequent jobs | No | false | boolean |
workspace-path | Path of the workspace to persist to relative to workspace-root. | No | . | string |
workspace-root | Workspace root path that is either an absolute path or a path relative to the working directory. Defaults to '.' (the working directory) | No | . | string |
This command renders Jinja templates at the specified paths.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
outputdir | Local path to which render Jinja templates will be written. | Yes | - | string |
path | Path to template file or directory containing .j2 or .jinja files. | Yes | - | string |
This is a Docker executor that includes the specified version of Python.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
tag | Pick a specific cimg/python image variant: https://hub.docker.com/r/cimg/python/tags
| No | '3.9' | string |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# This code is licensed from CircleCI to the user under the MIT license.
# See here for details: https://circleci.com/developer/orbs/licensing
version: 2.1
description: |
Render Jinja templates using a Python container.
display:
home_url: https://palletsprojects.com/p/jinja/
source_url: https://github.com/jtreutel/jinja-orb
commands:
render:
description: |
This command renders Jinja templates at the specified paths.
parameters:
outputdir:
description: Local path to which render Jinja templates will be written.
type: string
path:
description: Path to template file or directory containing .j2 or .jinja files.
type: string
steps:
- run:
command: pip3 install pyyaml jinja2
name: Install deps
- run:
command: "import re, os, glob, yaml, sys\nfrom jinja2 import Environment, FileSystemLoader\n\nenv = Environment(loader=FileSystemLoader(''))\n\ndef get_templates_in_dir(path):\n file_list = []\n for extension in [\"j2\", \"jinja\"]:\n file_list += glob.glob(f'{path}/*.{extension}')\n return file_list \n\n#Use the path and output directory supplied by the orb arguments\nsupplied_path = os.environ['PARAM_PATH'].rstrip(\"/\")\noutput_dir = os.environ['PARAM_OUTPUTDIR'].rstrip(\"/\")\n\n\ntemplates_to_render = []\nif os.path.isfile(supplied_path):\n templates_to_render.append(supplied_path)\n# If supplied path is a directory, scan it for .j2 and .jinja files\nelif os.path.isdir(supplied_path):\n templates_to_render = get_templates_in_dir(supplied_path)\n print(templates_to_render)\nelse:\n print(\"No file/directory found at path \\\"%s\\\"!\" % (supplied_path))\n sys.exit(1)\n\n\n#Create the output directory if it doesn't already exist\nif not os.path.exists(output_dir):\n os.makedirs(output_dir)\n\n# Render the templates\nfor template_path in templates_to_render:\n template = env.get_template(template_path)\n\n parsed_template = template.render(os.environ) #templatevars)\n\n #Write rendered template to output_dir\n #group(0) gets the filepath minus jinja extension; group(2) gets just the filename\n filename = re.search(r\"(.*\\/)(.*?)(?=(\\.jinja)|(\\.j2))\", template_path).group(2)\n with open('/'.join([output_dir, filename]), \"w\") as fh:\n fh.write(parsed_template)"
environment:
PARAM_OUTPUTDIR: <<parameters.outputdir>>
PARAM_PATH: <<parameters.path>>
name: Render templates
shell: /home/circleci/.pyenv/shims/python
executors:
default:
description: |
This is a Docker executor that includes the specified version of Python.
docker:
- image: cimg/python:<<parameters.tag>>
parameters:
tag:
default: "3.9"
description: |
Pick a specific cimg/python image variant: https://hub.docker.com/r/cimg/python/tags
type: string
jobs:
render:
description: |
This job renders Jinja templates at the specified paths.
executor: default
parameters:
outputdir:
description: Local path to which render Jinja templates will be written.
type: string
path:
description: Path to template file or directory containing .j2 or .jinja files.
type: string
persist-workspace:
default: false
description: Persist files to (cci) workspace for use in subsequent jobs
type: boolean
workspace-path:
default: .
description: Path of the workspace to persist to relative to workspace-root.
type: string
workspace-root:
default: .
description: Workspace root path that is either an absolute path or a path relative to the working directory. Defaults to '.' (the working directory)
type: string
steps:
- checkout
- run:
command: pip3 install pyyaml jinja2
name: Install deps
- run:
command: "import re, os, glob, yaml, sys\nfrom jinja2 import Environment, FileSystemLoader\n\nenv = Environment(loader=FileSystemLoader(''))\n\ndef get_templates_in_dir(path):\n file_list = []\n for extension in [\"j2\", \"jinja\"]:\n file_list += glob.glob(f'{path}/*.{extension}')\n return file_list \n\n#Use the path and output directory supplied by the orb arguments\nsupplied_path = os.environ['PARAM_PATH'].rstrip(\"/\")\noutput_dir = os.environ['PARAM_OUTPUTDIR'].rstrip(\"/\")\n\n\ntemplates_to_render = []\nif os.path.isfile(supplied_path):\n templates_to_render.append(supplied_path)\n# If supplied path is a directory, scan it for .j2 and .jinja files\nelif os.path.isdir(supplied_path):\n templates_to_render = get_templates_in_dir(supplied_path)\n print(templates_to_render)\nelse:\n print(\"No file/directory found at path \\\"%s\\\"!\" % (supplied_path))\n sys.exit(1)\n\n\n#Create the output directory if it doesn't already exist\nif not os.path.exists(output_dir):\n os.makedirs(output_dir)\n\n# Render the templates\nfor template_path in templates_to_render:\n template = env.get_template(template_path)\n\n parsed_template = template.render(os.environ) #templatevars)\n\n #Write rendered template to output_dir\n #group(0) gets the filepath minus jinja extension; group(2) gets just the filename\n filename = re.search(r\"(.*\\/)(.*?)(?=(\\.jinja)|(\\.j2))\", template_path).group(2)\n with open('/'.join([output_dir, filename]), \"w\") as fh:\n fh.write(parsed_template)"
environment:
PARAM_OUTPUTDIR: <<parameters.outputdir>>
PARAM_PATH: <<parameters.path>>
name: Render templates
shell: /home/circleci/.pyenv/shims/python
- when:
condition: << parameters.persist-workspace >>
steps:
- persist_to_workspace:
paths:
- << parameters.workspace-path >>
root: << parameters.workspace-root >>
examples:
example:
description: |
The orbs is called three times in the config below. Twice it is called as a command in the "jobs" section where it renders a single template and a directory of template. It is also called once as a job in the "workflows" section.
usage:
version: "2.1"
orbs:
jinja: jtreutel/jinja@0.1.1
jobs:
render-directory:
executor: jinja/default
steps:
- checkout
- jinja/render:
outputdir: rendered/
path: templates/
render-file:
executor: jinja/default
steps:
- checkout
- jinja/render:
outputdir: rendered/
path: templates/foo.j2
workflows:
main:
jobs:
- render-file
- render-directory
- jinja/render:
outputdir: rendered/
path: templates/