Skip to content

Instantly share code, notes, and snippets.

@elijahr
Last active December 6, 2020 03:37
Show Gist options
  • Save elijahr/12fe5e13b1e1bbcd81eeaa7c06429e19 to your computer and use it in GitHub Desktop.
Save elijahr/12fe5e13b1e1bbcd81eeaa7c06429e19 to your computer and use it in GitHub Desktop.
Interpolates aliases in YAML
_anchors:
checkout_repo: &checkout_repo
name: Checkout repo
uses: actions/checkout@v2
with:
fetch-depth: 1
submodules: recursive
install_pip_requirements: &install_pip_requirements
name: Install pip requirements
run: |
pip install -r requirements.txt
name: Build
on:
push:
branches: [ '*' ]
tags: [ '*' ]
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- *checkout_repo
- *install_pip_requirements
#!/usr/bin/env python
# Copyright © 2020 Elijah Shaw-Rutschman
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the “Software”), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""
Interpolates aliases in YAML.
Reads YAML from standard input, writes YAML to standard output. Comments and
formatting are retained.
This is useful if, for instance, you want to use aliases with a platform
that does not support them, such as Github Actions. You can define your anchors
in a top-level _anchors section, and that section will be omitted from the
output. The script can either be run manually prior to committing your changes,
or can be automated via a git hook.
Example input:
_anchors:
checkout_repo: &checkout_repo
name: Checkout repo
uses: actions/checkout@v2
with:
fetch-depth: 1
submodules: recursive
install_pip_requirements: &install_pip_requirements
name: Install pip requirements
run: |
pip install -r requirements.txt
name: Build
on:
push:
branches: [ '*' ]
tags: [ '*' ]
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- *checkout_repo
- *install_pip_requirements
The output would be:
name: Build
on: null
push:
branches: ['*']
tags: ['*']
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v2
with:
fetch-depth: 1
submodules: recursive
- name: Install pip requirements
run: |
pip install -r requirements.txt
"""
import sys
from ruamel import yaml
class InterpolatingDumper(yaml.RoundTripDumper):
def ignore_aliases(self, data):
# Always interpolate aliases
return True
def interpolate_aliases(in_stream, out_stream):
data = yaml.load(in_stream, Loader=yaml.RoundTripLoader)
if '_anchors' in data:
# Remove top-level _anchors section
del data['_anchors']
out_stream.write(yaml.dump(data, Dumper=InterpolatingDumper))
if __name__ == '__main__':
if '-h' in sys.argv:
print(__doc__)
sys.exit(0)
interpolate_aliases(sys.stdin, sys.stdout)
@elijahr
Copy link
Author

elijahr commented Nov 24, 2020

@rad-eric sure thing - I've just added an example.yml. If you download the files in this gist, run something like this in a shell:

pip install ruamel.yaml
cat example.yml | python interpolate-yaml-aliases.py > example-interpolated.yml

This would write the interpolated equivalent to example-interpolated.yml.

@yurishkuro
Copy link

In order to use this with GitHub Action, do you have to run the script manually and commit the interpolated files?

@elijahr
Copy link
Author

elijahr commented Nov 26, 2020

@yurishkuro You could use a git pre-commit hook to interpolate the file automatically. I have haven't tested this, but something like:

#!/bin/sh

set -ueo pipefail

files=( $(git diff-index --cached HEAD --name-only) )

for file in ${files[@]}
do
	if [[ "$file" == "build.yml" ]]
	then
		echo "Change detected, interpolating"
		cat build.yml | python interpolate-yaml-aliases.py > .github/workflows/build.yml
		git add .github/workflows/build.yml
	fi
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment