Skip to content

Commit a2677cb

Browse files
dhruvkbBetaHuhn
andauthored
✨ Add support for compiling Jinja-style templates with Nunjucks in sync process (#271)
* Add `nunjucks` and update `@actions/core` * Read template info from YAML config file * Render templates when copying files * Compile JS files into action * Add documentation about the templating feature * update readme Co-authored-by: Maximilian Schiller <[email protected]>
1 parent f5e582f commit a2677cb

File tree

7 files changed

+9262
-507
lines changed

7 files changed

+9262
-507
lines changed

README.md

+49
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ With [repo-file-sync-action](https://github.com/BetaHuhn/repo-file-sync-action)
2020
- Create a pull request in the target repo so you have the last say on what gets merged
2121
- Automatically label pull requests to integrate with other actions like [automerge-action](https://github.com/pascalgn/automerge-action)
2222
- Assign users to the pull request
23+
- Render [Jinja](https://jinja.palletsprojects.com/)-style templates as use variables thanks to [Nunjucks](https://mozilla.github.io/nunjucks/)
2324

2425
## 📚 Usage
2526

@@ -202,6 +203,54 @@ user/repo:
202203
replace: false
203204
```
204205

206+
### Using templates
207+
208+
You can render templates before syncing by using the [Jinja](https://jinja.palletsprojects.com/)-style template syntax. It will be compiled using [Nunjucks](https://mozilla.github.io/nunjucks/) and the output written to the specific file(s) or folder(s).
209+
210+
Nunjucks supports variables and blocks among other things. To enable, set the `template` field to a context dictionary, or in case of no variables, `true`:
211+
212+
```yml
213+
user/repo:
214+
- source: src/README.md
215+
template:
216+
user:
217+
name: 'Maxi'
218+
handle: '@BetaHuhn'
219+
```
220+
221+
In the source file you can then use these variables like this:
222+
223+
```yml
224+
# README.md
225+
226+
Created by {{ user.name }} ({{ user.handle }})
227+
```
228+
229+
Result:
230+
231+
```yml
232+
# README.md
233+
234+
Created by Maxi (@BetaHuhn)
235+
```
236+
237+
You can also use `extends` with a relative path to inherit other templates. Take a look at Nunjucks [template syntax](https://mozilla.github.io/nunjucks/templating.html) for more info.
238+
239+
```yml
240+
user/repo:
241+
- source: .github/workflows/child.yml
242+
template: true
243+
```
244+
245+
```yml
246+
# child.yml
247+
{% extends './parent.yml' %}
248+
249+
{% block some_block %}
250+
This is some content
251+
{% endblock %}
252+
```
253+
205254
### Delete orphaned files
206255

207256
With the `deleteOrphaned` option you can choose to delete files in the target repository if they are deleted in the source repository. The option defaults to `false` and only works when [syncing entire directories](#sync-entire-directories):

0 commit comments

Comments
 (0)