Skip to content
This repository was archived by the owner on Aug 29, 2023. It is now read-only.

Commit 0ca23d6

Browse files
add template engine functionality to support generic templates (#262)
* move config*.json files to configs directory * rename configs/*.json * make checks aware of all the configs/*.json * turn configs into objects with repositories key * make files part of the config * move language specific copy-workflow setup to actions * make dispatch workflow support more than one config json * create fewer batches if possible * set up copy-workflow actions properly * extract defaults from config separately * bring back double toJson * use copy-workflow actions from same branch * use compact json representation for storing FILES * fix files and local actions * run actions from template repo * add missing shell property to actions * add configs README * set deploy_versioning=true for go repositories * address review comments * restore hardcoded template-repo reference * add section on config testing * fix batches creation * fix command that produces batches * fix needs update logic * add template and reusable workflow for sync-release-assets * do not include the entire config in copied file * escape { in regexp pattern so that Actions don't interpret it * fix context variable setup * escape slashes * echo context instead of input redirect * use backticks instead of qx syntax * remove repo object from github context * add templates README * fix inputs usage in sync-release-assets workflow * simplify dist config * add all fields that might appear in config context * escape single quotes when executing bash from perl * add comments explaining the perl script * trust workflows published in ipfs org * move go-ipfs config from go.json to custom.json * Update templates/README.md Co-authored-by: Marten Seemann <[email protected]> * describe how context container is created * Revert: creation of sync-release-assets.yml workflow * chore: apply review requests * chore: simplify template engine and update js workflow --------- Co-authored-by: Marten Seemann <[email protected]>
1 parent 798c7de commit 0ca23d6

File tree

3 files changed

+140
-5
lines changed

3 files changed

+140
-5
lines changed

.github/workflows/copy-workflow.yml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ jobs:
5555
# See https://github.com/protocol/.github/blob/38135c75e47839623bf9b2748275d8c6167a8fa8/.github/workflows/copy-workflow.yml#L163-L168 for an example, how we used to make use of this.
5656
run: |
5757
files=${{ toJson(toJson(matrix.cfg.files)) }}
58-
files=$(echo -e "$files" | jq -c '.')
58+
extra_files=${{ toJson(toJson(matrix.cfg.extra_files)) }}
59+
files=$(echo -e "$files" "$extra_files" | jq -nc '[inputs] | add')
5960
echo "FILES=$files" >> $GITHUB_ENV
6061
- name: is initial workflow deployment
6162
# INITIAL_WORKFLOW_DEPLOYMENT=1 iff none of the files in the target repository exist yet
@@ -100,14 +101,23 @@ jobs:
100101
if: matrix.cfg.deploy_versioning
101102
uses: ./template-repo/.github/actions/copy-workflow-versioning
102103
- name: Add files
104+
env:
105+
CONTEXT: |
106+
{
107+
"config": ${{ toJSON(matrix.cfg) }},
108+
"github": {
109+
"default_branch": "${{ env.DEFAULT_BRANCH }}"
110+
}
111+
}
112+
TEMPLATE_ENGINE: s#\$\{\{\{\s*(.*?)\s*\}\}\}#`jq -cjn 'env.CONTEXT | fromjson.$1'`#ge
103113
run: |
104114
for f in $(jq -r '.[]' <<< ${{ toJson(env.FILES) }}); do
105115
echo -e "\nProcessing $f."
106116
# add DO NOT EDIT header
107117
tmp=$(mktemp)
108118
cat $TEMPLATE_REPO_DIR/$TEMPLATE_DIR/header.yml $TEMPLATE_REPO_DIR/$TEMPLATE_DIR/$f > $tmp
109-
# replace $default-branch with this repo's GitHub default branch
110-
sed -i "s:\$default-branch:${{ env.DEFAULT_BRANCH }}:g" $tmp
119+
# expand the template with values from the JSON context object
120+
perl -pi -e "$TEMPLATE_ENGINE" $tmp
111121
# create commit, if necessary
112122
commit_msg=""
113123
if [[ ! -f "$TARGET_REPO_DIR/$f" ]]; then

templates/.github/workflows/js-test-and-release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: test & maybe release
22
on:
33
push:
44
branches:
5-
- $default-branch # with #262 - ${{{ github.default_branch }}}
5+
- ${{{ github.default_branch }}}
66
pull_request:
77

88
jobs:
@@ -124,7 +124,7 @@ jobs:
124124
release:
125125
needs: [test-node, test-chrome, test-chrome-webworker, test-firefox, test-firefox-webworker, test-electron-main, test-electron-renderer]
126126
runs-on: ubuntu-latest
127-
if: github.event_name == 'push' && github.ref == 'refs/heads/$default-branch' # with #262 - 'refs/heads/${{{ github.default_branch }}}'
127+
if: github.event_name == 'push' && github.ref == 'refs/heads/${{{ github.default_branch }}}'
128128
steps:
129129
- uses: actions/checkout@v3
130130
with:

templates/README.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Templates
2+
3+
This directory contains template files that can be expanded and copied over to the configured repositories.
4+
5+
## Header
6+
7+
The [header](header.yml) is prepended to all the files before they are copied to the target repositories.
8+
9+
## Contexts
10+
11+
You can access context information during template expansion (before copy is performed).
12+
13+
### About contexts
14+
15+
Contexts are a way to access information about the repository configuration object and repository GitHub settings. Contexts use the following syntax:
16+
17+
```
18+
${{{ <context> }}}
19+
```
20+
21+
| Context name | Type | Description |
22+
| --- | --- | --- |
23+
| `config` | `object` | Configuration object because of which the file is being copied. For more information, see [config context](#config-context) |
24+
| `github` | `object` | Information about the target repository the file is being copied to. For more information, see [github context](#github-context) |
25+
26+
The context container is generated automatically before template expansion is performed.
27+
28+
#### `config` context
29+
30+
The `config` context is the configuration object because of which the file is being copied. It is created by merging the `defaults` object with a `repository` object (from the `repositories` array) from a JSON configuration file.
31+
32+
| Property name | Type | Always present | Description |
33+
| --- | --- | --- | --- |
34+
| `config` | `object` | `true` | The top-level context. | true |
35+
| `config.files` | `array` | `true` | The files that are being copied. |
36+
| `config.extra_files` | `array` | `false` | The additional files that are also being copied. |
37+
| `config.target` | `string` | `true` | The name of the target repository in `{owner}/{repo}` format. |
38+
| `config.deploy_go` | `boolean` | `false` | Flag controling if Go specific setup should be applied to the repository. |
39+
| `config.deploy_versioning` | `boolean` | `false` | Flag controling if `versions.json` file should be deployed to the repository. |
40+
| `config.dist` | `string` | `false` | The name of the distribution built from the repository. |
41+
42+
#### `github` context
43+
44+
The `github` context contains information about the target repository the file is being copied to. It is created on the fly before template expansion is performed.
45+
46+
| Property name | Type | Always present | Description |
47+
| --- | --- | --- | --- |
48+
| `github` | `object` | `true` | The top-level context. |
49+
| `github.default_branch` | `string` | `true` | The name of the default branch of the target repository. |
50+
51+
### Examples
52+
53+
#### JSON configuration
54+
55+
```json
56+
{
57+
"defaults": {
58+
"files": [".github/workflows/automerge.yml"],
59+
"is_example": false
60+
},
61+
"repositories": [
62+
{
63+
"target": "protocol/.github-test-target",
64+
"extra_files": [".github/workflows/example.yml"],
65+
"example": {
66+
"greeting": "Hello"
67+
},
68+
"is_example": true
69+
}
70+
]
71+
}
72+
```
73+
74+
#### Context container
75+
76+
*created automatically before template expansion is performed*
77+
78+
```json
79+
{
80+
"config": {
81+
"target": "protocol/.github-test-target",
82+
"files": [".github/workflows/automerge.yml"],
83+
"extra_files": [".github/workflows/example.yml"],
84+
"example": {
85+
"greeting": "Hello"
86+
},
87+
"is_example": true
88+
},
89+
"github": {
90+
"default_branch": "master"
91+
}
92+
}
93+
```
94+
95+
#### Template
96+
97+
```yaml
98+
name: Hello
99+
on:
100+
push:
101+
branches:
102+
- ${{{ github.default_branch }}}
103+
jobs:
104+
echo:
105+
runs-on: ubuntu-latest
106+
steps:
107+
- run: echo ${{{ config.example.greeting }}}
108+
shell: bash
109+
```
110+
111+
#### Expanded template
112+
113+
```yaml
114+
name: Hello
115+
on:
116+
push:
117+
branches:
118+
- master
119+
jobs:
120+
echo:
121+
runs-on: ubuntu-latest
122+
steps:
123+
- run: echo Hello
124+
shell: bash
125+
```

0 commit comments

Comments
 (0)