Skip to content

Commit 50fee74

Browse files
authored
DEV-2308: Render Terraform Modules and GitHub Actions (#628)
* adjust titles * Update module readme MD file with fixed URLs and content * Update custom loaders resolved path and add method to fetch Terraform files * Refactor terraform file fetching method for efficiency * Fix MDX format and <details><summary> html tags * Fix URL formatting in MDX content rendering * Disable category.json rendering and clean up URLs * Update Terraform docs rendering and collapse sidebar items * Update anchor tags for inputs and outputs in Terraform docs * Cache tmp directory and update sidebar label * Cache rendered content for website build process * Add logging for module provider and name, and docs directory * Render terraform docs and README for module directory * Update debug logging and exclude terraform-aws-components * Add debug logging for ls commands in ModuleRenderer * Refactor debug logging in ModuleRenderer.py * Refactor build target in Makefile and logging in Python script * Replace <= with &lt;= to avoid parsing issues * Update GitHub Action edit URL and fix MDX formatting * Update mdx formatting in rendering.py * Update sidebar directory path for GitHub actions * Update sidebar_label to 'actions' and add sidebar_class_name * Remove cache files from .gitignore * Update cache handling logic for GitHubProvider
1 parent cc21283 commit 50fee74

File tree

19 files changed

+368
-31
lines changed

19 files changed

+368
-31
lines changed

.github/actions/build-website/action.yml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,24 @@ runs:
5252
uses: actions/setup-python@v5
5353
with:
5454
python-version: '3.10'
55+
cache: 'pip'
5556

56-
- name: "Install Python Dependencies"
57+
- name: Cache rendered content
58+
uses: actions/cache@v4
59+
with:
60+
path: |
61+
.build-harness
62+
.cache
63+
key: ${{ github.workflow }}
64+
65+
- name: "Initialize Build Harness"
5766
shell: bash
5867
run: |
5968
make init
69+
70+
- name: "Install Python Dependencies"
71+
shell: bash
72+
run: |
6073
pip install -r scripts/docs-collator/requirements.txt
6174
6275
- name: "Install terraform-docs"
@@ -91,5 +104,4 @@ runs:
91104
GOOGLE_TAG_MANAGER: ${{ inputs.google_tag_manager }}
92105
GOOGLE_SITE_VERIFICATION_ID: ${{ inputs.google_site_verification_id }}
93106
run: |
94-
make init
95107
make build-production

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# Generated files
88
.docusaurus
99
.cache-loader
10+
.cache
1011
/layouts
1112
/public
1213
/resources

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ all: real-clean build
1010
deps:
1111
npm install
1212

13-
.PHONY: build
13+
.PHONY: init build
1414

1515
render:
1616
./scripts/render-docs-for-components.sh

docs/github-actions/library/actions/actions.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: GitHub Actions
3-
sidebar_label: Overview of GitHub Actions
3+
sidebar_label: actions
4+
sidebar_class_name: command
45
description: GitHub Actions
56
sidebar_position: 0
67
---

docs/github-actions/library/library.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: GitHub Actions
3-
sidebar_label: Overview of GitHub Actions
3+
sidebar_label: actions
44
sidebar_position: 0
55
sidebar_class_name: hidden
66
description: GitHub Actions Library

scripts/docs-collator/AbstractRenderer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def _post_rendering_fixes(self, repo, readme_md_file, submodule_dir=""):
3838
content = rendering.replace_relative_links_with_github_links(
3939
repo, content, submodule_dir
4040
)
41+
content = rendering.fix_mdx_format(content)
4142
io.save_string_to_file(readme_md_file, content)
4243

4344
def _copy_extra_resources_for_docs(self, module_download_dir, module_docs_dir):

scripts/docs-collator/ComponentRenderer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def __render_doc(self, component, file):
117117
io.save_string_to_file(result_file, doc_content)
118118

119119
# Breaking builds, not sure why and not adding value. Disabling.
120-
#self.__create_indexes_for_subfolder(component)
120+
# self.__create_indexes_for_subfolder(component)
121121

122122
def __create_indexes_for_subfolder(self, component):
123123
for root, dirs, files in os.walk(os.path.join(self.docs_dir, component)):

scripts/docs-collator/GitHubProvider.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import logging
33
import os
44
import re
5-
5+
import pickle
66
from github import Github, GithubException
7-
7+
from functools import lru_cache
88
from utils import io
99

1010
GITHUB_ORG = "cloudposse"
@@ -15,15 +15,27 @@
1515
"^github-action-.*"
1616
) # convention is github-action-<NAME>
1717

18+
CACHE_DIR = ".cache"
19+
CACHE_FILE = os.path.join(CACHE_DIR, "file_contents_cache.pkl")
20+
REPOS_CACHE_FILE = os.path.join(CACHE_DIR, "repos_cache.pkl")
21+
1822

1923
class GitHubProvider:
2024
def __init__(self, github_api_token):
2125
self.github = Github(github_api_token)
26+
self.cache = self.load_cache(CACHE_FILE)
27+
self.repos_cache = self.load_cache(REPOS_CACHE_FILE)
2228

2329
def get_terraform_repos(self, includes_csv, excludes_csv):
24-
return self.__get_repos(
30+
key = (includes_csv, excludes_csv)
31+
if key in self.repos_cache:
32+
return self.repos_cache[key]
33+
repos = self.__get_repos(
2534
includes_csv, excludes_csv, TERRAFORM_MODULE_NAME_PATTERN
2635
)
36+
self.repos_cache[key] = repos
37+
self.save_cache(REPOS_CACHE_FILE, self.repos_cache)
38+
return repos
2739

2840
def get_github_actions_repos(self, includes_csv, excludes_csv):
2941
return self.__get_repos(includes_csv, excludes_csv, GITHUB_ACTION_NAME_PATTERN)
@@ -75,12 +87,27 @@ def list_repo_dir(self, repo, remote_dir, recursive=True):
7587

7688
return result
7789

78-
def fetch_file(self, repo, remote_file, output_dir):
79-
io.create_dirs(os.path.join(output_dir, os.path.dirname(remote_file)))
90+
def get_file_content(self, repo, remote_file):
91+
"""
92+
Fetches the content of a file from a GitHub repository AND cache the result
93+
"""
94+
key = (repo.full_name, remote_file)
95+
if key in self.cache:
96+
return self.cache[key]
8097
content_encoded = repo.get_contents(
8198
remote_file, ref=repo.default_branch
8299
).content
83100
content = base64.b64decode(content_encoded)
101+
self.cache[key] = content
102+
self.save_cache(CACHE_FILE, self.cache)
103+
return content
104+
105+
def fetch_file(self, repo, remote_file, output_dir):
106+
io.create_dirs(os.path.join(output_dir, os.path.dirname(remote_file)))
107+
108+
# fetch file content, supported by caching
109+
content = self.get_file_content(repo, remote_file)
110+
84111
output_file = os.path.join(output_dir, remote_file)
85112
io.save_to_file(output_file, content)
86113
logging.info(f"Fetched file: {remote_file}")
@@ -99,3 +126,14 @@ def __is_valid(self, repo, pattern):
99126
return False
100127

101128
return True
129+
130+
def load_cache(self, file_path):
131+
if os.path.exists(file_path):
132+
with open(file_path, "rb") as f:
133+
return pickle.load(f)
134+
return {}
135+
136+
def save_cache(self, file_path, cache):
137+
os.makedirs(CACHE_DIR, exist_ok=True)
138+
with open(file_path, "wb") as f:
139+
pickle.dump(cache, f)

scripts/docs-collator/ModuleFetcher.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ def fetch(self, repo):
3232
if SUBMODULES_DIR in remote_files:
3333
self.__fetch_submodules(repo, module_download_dir)
3434

35+
self.__fetch_terraform_files(repo, module_download_dir, remote_files)
36+
3537
def __fetch_images(self, repo, module_download_dir):
3638
remote_files = self.github_provider.list_repo_dir(repo, IMAGES_DIR)
3739

@@ -59,3 +61,12 @@ def __fetch_submodules(self, repo, module_download_dir):
5961
module_download_dir,
6062
submodule_dir=os.path.dirname(readme_file),
6163
)
64+
65+
def __fetch_terraform_files(self, repo, module_download_dir, remote_files):
66+
for remote_file in remote_files:
67+
# We only need variables and output files to render terraform-docs.
68+
# _Hopefully_ the module follows the convention of having these files with all varialbes and outputs,
69+
# but that is an assumption we're making here for the sake of deployment speed.
70+
# if remote_file in ["variables.tf", "outputs.tf"]:
71+
if remote_file.endswith(".tf"):
72+
self.github_provider.fetch_file(repo, remote_file, module_download_dir)

scripts/docs-collator/ModuleRenderer.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ def render(self, repo):
3636
self._pre_rendering_fixes(repo, module_download_dir)
3737

3838
provider, module_name = rendering.parse_terraform_repo_name(repo.name)
39+
logging.debug(f"Provider: {provider}, Module: {module_name}")
40+
3941
module_docs_dir = os.path.join(self.docs_dir, provider, module_name)
42+
logging.debug(f"Module docs dir: {module_docs_dir}")
4043

4144
self.__render_readme(module_download_dir, module_docs_dir)
4245

@@ -52,8 +55,9 @@ def render(self, repo):
5255
repo, module_download_dir, module_docs_dir
5356
)
5457

55-
self.__create_index_for_provider(repo)
56-
self.__create_indexes_for_subfolders(repo)
58+
# Disable category.json for now
59+
# self.__create_index_for_provider(repo)
60+
# self.__create_indexes_for_subfolders(repo)
5761

5862
def __render_readme(self, module_download_dir, module_docs_dir):
5963
readme_yaml_file = os.path.join(module_download_dir, README_YAML)
@@ -62,6 +66,21 @@ def __render_readme(self, module_download_dir, module_docs_dir):
6266

6367
io.create_dirs(module_docs_dir)
6468

69+
# Re-render terraform docs with this repo's terraform-docs template for modules.
70+
# This replaces docs/terraform.md for the given module in place
71+
logging.debug(f"Rendering terraform docs for: {module_download_dir}")
72+
rendering.render_terraform_docs(
73+
module_download_dir, os.path.join(TEMPLATES_DIR, "terraform-docs.yml")
74+
)
75+
76+
# Run the make readme command in the module directory to compile README.md
77+
logging.debug(f"Rendering README.md for: {module_download_dir}")
78+
logging.debug(f"make readme")
79+
logging.debug(f"README_TEMPLATE_FILE: {readme_tmpl_file}")
80+
logging.debug(f"README_FILE: {readme_md_file}")
81+
logging.debug(f"README_YAML: {readme_yaml_file}")
82+
logging.debug(f"README_TEMPLATE_YAML: {readme_yaml_file}")
83+
logging.debug(f"README_INCLUDES: {module_download_dir}")
6584
response = subprocess.run(
6685
[
6786
"make",
@@ -185,4 +204,5 @@ def __post_rendering_fixes_for_submodule(self, readme_md_file):
185204
content = io.read_file_to_string(readme_md_file)
186205
content = rendering.fix_self_non_closing_br_tags(content)
187206
content = rendering.fix_custom_non_self_closing_tags_in_pre(content)
207+
content = rendering.fix_mdx_format(content)
188208
io.save_string_to_file(readme_md_file, content)

scripts/docs-collator/render_docs_for_modules.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ def main(
3131
logging.info(f"Found {len(repos)} repositories to process")
3232

3333
for repo in repos:
34+
# logging.info(f"Debugging module: {repo.full_name}")
35+
# if repo.full_name != "cloudposse/terraform-aws-ec2-ami-backup":
36+
# logging.info(f"skipping...")
37+
# continue
38+
3439
try:
3540
logging.info(f"Fetching files for: {repo.full_name}")
3641
fetcher.fetch(repo)

scripts/docs-collator/templates/components/terraform-docs.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ content: |-
44
55
{{ .Header }}
66
7-
## Contents
7+
## Terraform
88
99
{{ if ne (len .Module.Requirements) 0 -}}
1010
### Version Requirements
@@ -70,7 +70,7 @@ content: |-
7070
<dl>
7171
{{- range .Module.Inputs }}
7272
{{- if and (not (has .Name $context_variables)) .Required }}
73-
<dt>`{{ .Name }}`{{ if lt (len (split "\n" (tostring .Type))) 2 }} (`{{ tostring .Type }}`){{end}} <i>required</i>{{ if contains "OBSOLETE: " (tostring .Description) }} <strong>OBSOLETE</strong>{{ end }}</dt>
73+
<dt><a id="{{ .Name }}" href="#{{ .Name }}">`{{ .Name }}`</a>{{ if lt (len (split "\n" (tostring .Type))) 2 }} (`{{ tostring .Type }}`){{end}} <i>required</i>{{ if contains "OBSOLETE: " (tostring .Description) }} <strong>OBSOLETE</strong>{{ end }}</dt>
7474
<dd>
7575
{{- $lines := regexSplit "\n" (tostring .Description | replace "OBSOLETE: " "") -1 -}}
7676
{{- range $lines }}
@@ -100,7 +100,7 @@ content: |-
100100
<dl>
101101
{{- range .Module.Inputs }}
102102
{{- if and (not (has .Name $context_variables)) (not .Required) }}
103-
<dt>`{{ .Name }}`{{ if lt (len (split "\n" (tostring .Type))) 2 }} (`{{ tostring .Type }}`){{end}} <i>optional</i>{{ if contains "OBSOLETE: " (tostring .Description) }} <strong>OBSOLETE</strong>{{ end }}</dt>
103+
<dt><a id="{{ .Name }}" href="#{{ .Name }}">`{{ .Name }}`</a>{{ if lt (len (split "\n" (tostring .Type))) 2 }} (`{{ tostring .Type }}`){{end}} <i>optional</i>{{ if contains "OBSOLETE: " (tostring .Description) }} <strong>OBSOLETE</strong>{{ end }}</dt>
104104
<dd>
105105
{{- $lines := regexSplit "\n" (tostring .Description | replace "OBSOLETE: " "") -1 -}}
106106
{{- range $lines }}
@@ -114,7 +114,7 @@ content: |-
114114
```
115115
<br/>
116116
{{ end }}
117-
**Default value:** {{ if lt (len (split "\n" .GetValue)) 2 }}`{{ .GetValue }}`{{ else }}
117+
**Default value:** {{ if lt (len (split "\n" .GetValue)) 2 }}`{{ .GetValue | replace "{}" "{ }" | replace "[]" "[ ]" }}`{{ else }}
118118
```hcl
119119
{{- $lines := regexSplit "\n" .GetValue -1 -}}
120120
{{- range $lines }}
@@ -138,7 +138,7 @@ content: |-
138138
<dl>
139139
{{- range .Module.Inputs }}
140140
{{- if and (has .Name $context_variables) }}
141-
<dt>`{{ .Name }}`{{ if lt (len (split "\n" (tostring .Type))) 2 }} (`{{ tostring .Type }}`){{end}} <i>{{ ternary .Required "required" "optional" }}</i>{{ if contains "OBSOLETE: " (tostring .Description) }} <strong>OBSOLETE</strong>{{ end }}</dt>
141+
<dt><a id="{{ .Name }}" href="#{{ .Name }}">`{{ .Name }}`</a>{{ if lt (len (split "\n" (tostring .Type))) 2 }} (`{{ tostring .Type }}`){{end}} <i>{{ ternary .Required "required" "optional" }}</i>{{ if contains "OBSOLETE: " (tostring .Description) }} <strong>OBSOLETE</strong>{{ end }}</dt>
142142
<dd>
143143
{{- $lines := regexSplit "\n" (tostring .Description | replace "OBSOLETE: " "") -1 -}}
144144
{{- range $lines }}
@@ -152,7 +152,7 @@ content: |-
152152
```
153153
<br/>
154154
{{ end }}
155-
**Default value:** {{ if lt (len (split "\n" .GetValue)) 2 }}`{{ .GetValue }}`{{ else }}
155+
**Default value:** {{ if lt (len (split "\n" .GetValue)) 2 }}`{{ .GetValue | replace "{}" "{ }" | replace "[]" "[ ]" }}`{{ else }}
156156
```hcl
157157
{{- $lines := regexSplit "\n" .GetValue -1 -}}
158158
{{- range $lines }}
@@ -171,7 +171,7 @@ content: |-
171171
172172
<dl>
173173
{{- range .Module.Outputs }}
174-
<dt>`{{ .Name }}`{{ if contains "OBSOLETE: " (tostring .Description) }} <strong>OBSOLETE</strong>{{ end }}</dt>
174+
<dt><a id="{{ .Name }}" href="#{{ .Name }}">`{{ .Name }}`</a>{{ if contains "OBSOLETE: " (tostring .Description) }} <strong>OBSOLETE</strong>{{ end }}</dt>
175175
<dd>
176176
{{- $lines := regexSplit "\n" (tostring .Description | replace "OBSOLETE: " "" | default "n/a" ) -1 -}}
177177
{{- range $lines }}

scripts/docs-collator/templates/github-actions/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ description: |-
1515
tags:
1616
{{ (ds "config").tags | data.ToYAML | strings.Indent 2 -}}
1717
{{- end }}
18-
custom_edit_url: https://github.com/cloudposse/{{ $fullName }}/edit/master/README.md
18+
custom_edit_url: https://github.com/cloudposse/{{ $fullName }}/edit/main/README.md
1919
---
2020

2121
# GitHub Action: `{{ $shortName }}`
@@ -75,7 +75,7 @@ custom_edit_url: https://github.com/cloudposse/{{ $fullName }}/edit/master/READM
7575

7676
{{ if has (ds "config") "include" }}
7777
{{ range $file := (datasource "config").include -}}
78-
{{ (include "includes" $file) }}
78+
{{ (include "includes" (printf "%s/%s" $fullName $file)) }}
7979
{{- end }}
8080
{{- end }}
8181
{{- end }}

scripts/docs-collator/templates/modules/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ description: |-
1515
tags:
1616
{{ (ds "config").tags | data.ToYAML | strings.Indent 2 -}}
1717
{{- end }}
18-
custom_edit_url: https://github.com/cloudposse/{{ $fullModuleName }}/edit/master/README.md
18+
custom_edit_url: https://github.com/cloudposse/{{ $fullModuleName }}/edit/main/README.md
1919
---
2020

2121
# Module: `{{ $shortModuleName }}`
@@ -75,7 +75,7 @@ custom_edit_url: https://github.com/cloudposse/{{ $fullModuleName }}/edit/master
7575

7676
{{ if has (ds "config") "include" }}
7777
{{ range $file := (datasource "config").include -}}
78-
{{ (include "includes" $file) }}
78+
{{ (include "includes" (printf "%s/%s" $fullModuleName $file)) }}
7979
{{- end }}
8080
{{- end }}
8181
{{- end }}

0 commit comments

Comments
 (0)