Skip to content

Commit 5db3863

Browse files
dhruvkbBetaHuhn
authored andcommitted
✨ Add support for compiling Jinja-style templates with Nunjucks in sync process (BetaHuhn#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 2c97661 commit 5db3863

File tree

7 files changed

+9262
-507
lines changed

7 files changed

+9262
-507
lines changed

README.md

Lines changed: 49 additions & 0 deletions
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

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

207+
### Using templates
208+
209+
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).
210+
211+
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`:
212+
213+
```yml
214+
user/repo:
215+
- source: src/README.md
216+
template:
217+
user:
218+
name: 'Maxi'
219+
handle: '@BetaHuhn'
220+
```
221+
222+
In the source file you can then use these variables like this:
223+
224+
```yml
225+
# README.md
226+
227+
Created by {{ user.name }} ({{ user.handle }})
228+
```
229+
230+
Result:
231+
232+
```yml
233+
# README.md
234+
235+
Created by Maxi (@BetaHuhn)
236+
```
237+
238+
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.
239+
240+
```yml
241+
user/repo:
242+
- source: .github/workflows/child.yml
243+
template: true
244+
```
245+
246+
```yml
247+
# child.yml
248+
{% extends './parent.yml' %}
249+
250+
{% block some_block %}
251+
This is some content
252+
{% endblock %}
253+
```
254+
206255
### Delete orphaned files
207256

208257
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)