Skip to content

Commit

Permalink
Merge pull request #271 from JoeyBarnes/main
Browse files Browse the repository at this point in the history
Added a GitHub Actions Workflow to generate ARM and Bicep templates
  • Loading branch information
arjenhuitema authored Jul 11, 2024
2 parents f69da29 + 05b1cb4 commit 1e7579c
Show file tree
Hide file tree
Showing 17 changed files with 1,769 additions and 65 deletions.
83 changes: 83 additions & 0 deletions .github/workflows/generate-templates.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Workflow for generating (arm/bicep/etc) templates for each alert
name: Generate Templates

on:
# Runs on pushes targeting the default branch
push:
branches:
- main
paths:
- 'services/**/alerts.yaml'
- 'tooling/generate-templates/**'

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch: {}

jobs:
generate-templates:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: main
submodules: recursive
fetch-depth: 0

- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.12' # install the python version needed

- name: Install Python Packages and Requirements
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
working-directory: tooling/generate-templates

- name: Generate Templates
id: generate
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git checkout -b github-action-generate-templates
# Generate templates for alerts
echo "Generating templates for alerts..."
python tooling/generate-templates/generate-templates.py --path services --output services --template_path tooling/generate-templates/templates
# Check if there are any changes in the services directory
git add services
# Check if there are any changes to commit
if [[ `git status --porcelain` ]]; then
git commit -m "[GitHub Action - Generate Templates] Generate templates for alerts"
# Push changes to the current branch
git push --set-upstream origin github-action-generate-templates --force
prs=$(gh pr list \
--repo "$GITHUB_REPOSITORY" \
--head 'github-action-generate-templates' \
--base 'main' \
--json title \
--jq 'length')
if ((prs > 0)); then
echo "skippr=true" >> "$GITHUB_OUTPUT"
fi
else
echo "skippr=true" >> "$GITHUB_OUTPUT"
fi
- name: Create pull request
if: '!steps.generate.outputs.skippr'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Create a pull request
echo "Creating a pull request..."
gh pr --repo ${{ github.repository }} create --title "[GitHub Action - Generate Templates] Generate templates for alerts" --body "This PR was automatically generated by the workflow." --base main --head github-action-generate-templates
5 changes: 5 additions & 0 deletions .github/workflows/hugo-build-pr-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ on:
- main
paths:
- 'docs/**'
- 'services/**/alerts.yaml'
- 'services/**/*.md'
- 'services/**/templates/**'
- 'tooling/export-alerts/**'
- 'tooling/generate-templates/**'
- '.github/workflows/hugo-build-pr-check.yml'
workflow_dispatch: {}

Expand Down
7 changes: 1 addition & 6 deletions .github/workflows/hugo-site-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:
- 'docs/**'
- 'services/**/alerts.yaml'
- 'services/**/*.md'
- 'services/**/templates/**'
- 'tooling/export-alerts/**'
- 'tooling/generate-templates/**'

Expand Down Expand Up @@ -69,12 +70,6 @@ jobs:
python export-alerts.py --path ../../services --template ./alerts-template.xlsx --output-xls ../../services/amba-alerts.xlsx --output-json ../../services/amba-alerts.json --output-yaml ../../services/amba-alerts.yaml
working-directory: tooling/export-alerts

- name: Generate Templates
run: |
pip install -r requirements.txt
python generate-templates.py --path ../../services --output ../../artifacts/templates
working-directory: tooling/generate-templates

- name: Setup Pages
id: pages
uses: actions/configure-pages@v5
Expand Down
11 changes: 6 additions & 5 deletions docs/layouts/partials/templates/template-tabs.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{{ $category := .category }}
{{ $type := .type }}
{{ $filename := printf "%s.json" .alert.guid | printf "%s"}}
{{ $version := readFile "artifacts/templates/latest_version.txt" }}
{{ $file := path.Join "artifacts/templates" $version $category $type $filename }}
{{ $alert_name := .alert.name | replaceRE `[^a-zA-Z0-9-]` "" }}
{{ $alert_name := printf "%s_%s" $alert_name .alert.guid }}
{{ $filename := printf "%s.json" $alert_name | printf "%s"}}
{{ $file := path.Join "services" $category $type "templates/arm" $filename }}
<div class="gdoc-tabs">
<input
type="radio"
Expand Down Expand Up @@ -45,8 +46,8 @@
Bicep
</label>
<div class="gdoc-markdown--nested gdoc-tabs__content">
{{ $filename := printf "%s.bicep" .alert.guid | printf "%s"}}
{{ $file := path.Join "artifacts/templates" $version $category $type $filename }}
{{ $filename := printf "%s.bicep" $alert_name | printf "%s"}}
{{ $file := path.Join "services" $category $type "templates/bicep" $filename }}
{{ $data := readFile $file }}
{{ highlight $data "bicep" }}
</div>
Expand Down
Loading

0 comments on commit 1e7579c

Please sign in to comment.