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

add template engine functionality to support generic templates #262

Merged
merged 48 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from 45 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
526400f
move config*.json files to configs directory
galargh Dec 7, 2021
f727003
rename configs/*.json
galargh Dec 8, 2021
f434f15
make checks aware of all the configs/*.json
galargh Dec 7, 2021
496348f
turn configs into objects with repositories key
galargh Dec 7, 2021
67da4b2
make files part of the config
galargh Dec 7, 2021
b9aaa52
move language specific copy-workflow setup to actions
galargh Dec 7, 2021
02436cf
make dispatch workflow support more than one config json
galargh Dec 7, 2021
0d71b9e
create fewer batches if possible
galargh Dec 7, 2021
0828267
set up copy-workflow actions properly
galargh Dec 8, 2021
1dfeb94
extract defaults from config separately
galargh Dec 8, 2021
16e4ca8
bring back double toJson
galargh Dec 8, 2021
dbe2f9c
use copy-workflow actions from same branch
galargh Dec 8, 2021
f5cf5ab
use compact json representation for storing FILES
galargh Dec 8, 2021
6955318
fix files and local actions
galargh Dec 8, 2021
c95cbff
run actions from template repo
galargh Dec 8, 2021
5c86ae0
add missing shell property to actions
galargh Dec 8, 2021
0b3c5cb
add configs README
galargh Dec 8, 2021
07f9202
set deploy_versioning=true for go repositories
galargh Dec 15, 2021
9fdc4eb
address review comments
galargh Dec 15, 2021
60d1f29
restore hardcoded template-repo reference
galargh Dec 15, 2021
a71f87a
add section on config testing
galargh Dec 15, 2021
0f34080
fix batches creation
galargh Dec 16, 2021
b2c1dd9
fix command that produces batches
galargh Dec 16, 2021
664f8b7
fix needs update logic
galargh Dec 16, 2021
d54c585
add template and reusable workflow for sync-release-assets
galargh Dec 17, 2021
756a68c
do not include the entire config in copied file
galargh Dec 20, 2021
557dd05
escape { in regexp pattern so that Actions don't interpret it
galargh Dec 20, 2021
95fc5c8
fix context variable setup
galargh Dec 20, 2021
1dcd301
escape slashes
galargh Dec 20, 2021
6d14255
echo context instead of input redirect
galargh Dec 20, 2021
8696afd
use backticks instead of qx syntax
galargh Dec 21, 2021
b708708
remove repo object from github context
galargh Dec 21, 2021
ba2b680
add templates README
galargh Dec 21, 2021
d44d862
fix inputs usage in sync-release-assets workflow
galargh Dec 21, 2021
359f6a5
simplify dist config
galargh Dec 21, 2021
1100c54
add all fields that might appear in config context
galargh Dec 21, 2021
9706e9c
escape single quotes when executing bash from perl
galargh Dec 21, 2021
486f183
add comments explaining the perl script
galargh Dec 21, 2021
0f14645
trust workflows published in ipfs org
galargh Dec 22, 2021
0ee771b
Merge remote-tracking branch 'origin/master' into sync-release-assets
galargh Jan 10, 2022
5ddc3be
move go-ipfs config from go.json to custom.json
galargh Jan 10, 2022
406b183
Update templates/README.md
galargh Jan 10, 2022
676e490
describe how context container is created
galargh Jan 10, 2022
2fbe6c8
Merge remote-tracking branch 'origin/master' into sync-release-assets
galargh Jan 3, 2023
01192c5
Revert: creation of sync-release-assets.yml workflow
galargh Jan 3, 2023
d72d162
Merge remote-tracking branch 'origin/master' into sync-release-assets
galargh Feb 1, 2023
62f9128
chore: apply review requests
galargh Feb 1, 2023
ddb2ba2
chore: simplify template engine and update js workflow
galargh Feb 1, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions .github/workflows/copy-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ jobs:
# 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.
run: |
files=${{ toJson(toJson(matrix.cfg.files)) }}
files=$(echo -e "$files" | jq -c '.')
extra_files=${{ toJson(toJson(matrix.cfg.extra_files)) }}
files=$(echo -e "$files" "$extra_files" | jq -nc '[inputs] | add')
echo "FILES=$files" >> $GITHUB_ENV
- name: is initial workflow deployment
# INITIAL_WORKFLOW_DEPLOYMENT=1 iff none of the files in the target repository exist yet
Expand Down Expand Up @@ -101,13 +102,26 @@ jobs:
uses: ./template-repo/.github/actions/copy-workflow-versioning
- name: Add files
run: |
config=${{ toJson(toJson(matrix.cfg)) }}
config=$(echo -e "$config" | jq -c '.')
github="{\"default_branch\":\"${{ env.DEFAULT_BRANCH }}\"}"
context="{\"config\":$config,\"github\":$github}"
# matches trimmed string between template context delimiters
regexp='\$\{\{\{\s*(.*?)\s*\}\}\}'
# replacement is a perl script that:
# 1. prepends . to the matched string to create a filter
# 2. escapes single quotes by replacing ' with '"'"' in filter
# 3. prints context escaped in the same manner
# 4. applies jq with filter to the stdout
replacement="\$filter = \".\$1\"; \$filter =~ s/'/'\"'\"'/g; \`echo '${context//\'/\'\"\'\"\'}' | jq -jc '\$filter'\`"
for f in $(jq -r '.[]' <<< ${{ toJson(env.FILES) }}); do
echo -e "\nProcessing $f."
# add DO NOT EDIT header
tmp=$(mktemp)
cat $TEMPLATE_REPO_DIR/$TEMPLATE_DIR/header.yml $TEMPLATE_REPO_DIR/$TEMPLATE_DIR/$f > $tmp
# replace $default-branch with this repo's GitHub default branch
sed -i "s:\$default-branch:${{ env.DEFAULT_BRANCH }}:g" $tmp
# replace template contexts with values from the JSON context object
perl -pi -e "s#$regexp#$replacement#ge" $tmp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mv $tmp $TEMPLATE_REPO_DIR/$TEMPLATE_DIR/$f
galargh marked this conversation as resolved.
Show resolved Hide resolved
# create commit, if necessary
commit_msg=""
if [[ ! -f "$TARGET_REPO_DIR/$f" ]]; then
Expand Down
125 changes: 125 additions & 0 deletions templates/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# Templates

This directory contains template files that can be expanded and copied over to the configured repositories.

## Header

The [header](header.yml) is prepended to all the files before they are copied to the target repositories.

## Contexts

You can access context information during template expansion (before copy is performed).

### About contexts

Contexts are a way to access information about the repository configuration object and repository GitHub settings. Contexts use the following syntax:

```
${{{ <context> }}}
```

| Context name | Type | Description |
| --- | --- | --- |
| `config` | `object` | Configuration object because of which the file is being copied. For more information, see [config context](#config-context) |
| `github` | `object` | Information about the target repository the file is being copied to. For more information, see [github context](#github-context) |

The context container is generated automatically before template expansion is performed.

#### `config` context

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.

| Property name | Type | Always present | Description |
| --- | --- | --- | --- |
| `config` | `object` | `true` | The top-level context. | true |
| `config.files` | `array` | `true` | The files that are being copied. |
| `config.extra_files` | `array` | `false` | The additional files that are also being copied. |
| `config.target` | `string` | `true` | The name of the target repository in `{owner}/{repo}` format. |
| `config.deploy_go` | `boolean` | `false` | Flag controling if Go specific setup should be applied to the repository. |
| `config.deploy_versioning` | `boolean` | `false` | Flag controling if `versions.json` file should be deployed to the repository. |
| `config.dist` | `string` | `false` | The name of the distribution built from the repository. |

#### `github` context

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.

| Property name | Type | Always present | Description |
| --- | --- | --- | --- |
| `github` | `object` | `true` | The top-level context. |
| `github.default_branch` | `string` | `true` | The name of the default branch of the target repository. |

### Examples

#### JSON configuration

```json
{
"defaults": {
"files": [".github/workflows/automerge.yml"],
"is_example": false
},
"repositories": [
{
"target": "protocol/.github-test-target",
"extra_files": [".github/workflows/example.yml"],
"example": {
"greeting": "Hello"
},
"is_example": true
}
]
}
```

#### Context container

*created automatically before template expansion is performed*

```json
{
"config": {
"target": "protocol/.github-test-target",
"files": [".github/workflows/automerge.yml"],
"extra_files": [".github/workflows/example.yml"],
"example": {
"greeting": "Hello"
},
"is_example": true
},
"github": {
"default_branch": "master"
}
}
```

#### Template

```yaml
name: Hello
on:
push:
branches:
- ${{{ github.default_branch }}}
jobs:
echo:
runs-on: ubuntu-latest
steps:
- run: echo ${{{ config.example.greeting }}}
shell: bash
```

#### Expanded template

```yaml
name: Hello
on:
push:
branches:
- master
jobs:
echo:
runs-on: ubuntu-latest
steps:
- run: echo Hello
shell: bash
```