Skip to content

Commit 553a38a

Browse files
Merge pull request #52 from NHSDigital/CCM-8568_TFDocsAutomation
CCM-8568 tf docs automation
2 parents 031a0c8 + ebaf081 commit 553a38a

File tree

13 files changed

+290
-4
lines changed

13 files changed

+290
-4
lines changed

.github/workflows/cicd-1-pull-request.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ jobs:
4040
echo "build_datetime=$datetime" >> $GITHUB_OUTPUT
4141
echo "build_timestamp=$(date --date=$datetime -u +'%Y%m%d%H%M%S')" >> $GITHUB_OUTPUT
4242
echo "build_epoch=$(date --date=$datetime -u +'%s')" >> $GITHUB_OUTPUT
43-
echo "nodejs_version=$(grep "^nodejs" .tool-versions | cut -f2 -d' ')" >> $GITHUB_OUTPUT
44-
echo "python_version=$(grep "^nodejs" .tool-versions | cut -f2 -d' ')" >> $GITHUB_OUTPUT
45-
echo "terraform_version=$(grep "^terraform" .tool-versions | cut -f2 -d' ')" >> $GITHUB_OUTPUT
43+
echo "nodejs_version=$(grep "^nodejs " .tool-versions | cut -f2 -d' ')" >> $GITHUB_OUTPUT
44+
echo "python_version=$(grep "^python " .tool-versions | cut -f2 -d' ')" >> $GITHUB_OUTPUT
45+
echo "terraform_version=$(grep "^terraform " .tool-versions | cut -f2 -d' ')" >> $GITHUB_OUTPUT
4646
echo "version=$(echo $version)" >> $GITHUB_OUTPUT
4747
echo "is_version_prerelease=$(if [[ $version == *-* ]]; then echo "true"; else echo "false"; fi)" >> $GITHUB_OUTPUT
4848

.github/workflows/stage-1-commit.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,30 @@ jobs:
6666
fetch-depth: 0 # Full history is needed to compare branches
6767
- name: "Check Markdown format"
6868
uses: ./.github/actions/check-markdown-format
69+
terraform-docs:
70+
name: "Run terraform-docs"
71+
runs-on: ubuntu-latest
72+
needs: detect-terraform-changes
73+
if: needs.detect-terraform-changes.outputs.terraform_changed == 'true'
74+
permissions:
75+
contents: write
76+
steps:
77+
- name: "Checkout code"
78+
uses: actions/checkout@v4
79+
with:
80+
fetch-depth: 0 # Full history is needed to compare branches
81+
- name: "Check to see if Terraform Docs are up-to-date"
82+
run: |
83+
make terraform-docs
84+
- name: "Stage changes"
85+
run: |
86+
git add infrastructure/terraform/**/*.md
87+
- name: "Check for changes in Terraform Docs"
88+
run: |
89+
if git diff --cached --name-only | grep -qE '\.md$'; then
90+
echo "Markdown files have changed. Please run 'make terraform-docs' and commit the changes."
91+
exit 1
92+
fi
6993
check-english-usage:
7094
name: "Check English usage"
7195
runs-on: ubuntu-latest

.tool-versions

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ act 0.2.64
22
gitleaks 8.18.4
33
pre-commit 3.6.0
44
terraform 1.9.2
5+
terraform-docs 0.19.0
56
tfsec 1.28.10
67
vale 3.6.0
78

infrastructure/images/.gitkeep

Whitespace-only changes.

infrastructure/modules/.gitkeep

Whitespace-only changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!-- BEGIN_TF_DOCS -->
2+
<!-- markdownlint-disable -->
3+
<!-- vale off -->
4+
5+
## Requirements
6+
7+
| Name | Version |
8+
|------|---------|
9+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.9.0 |
10+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | ~> 5.50 |
11+
## Inputs
12+
13+
| Name | Description | Type | Default | Required |
14+
|------|-------------|------|---------|:--------:|
15+
| <a name="input_aws_account_id"></a> [aws\_account\_id](#input\_aws\_account\_id) | The AWS Account ID (numeric) | `string` | n/a | yes |
16+
| <a name="input_component"></a> [component](#input\_component) | The variable encapsulating the name of this component | `string` | `"acct"` | no |
17+
| <a name="input_default_tags"></a> [default\_tags](#input\_default\_tags) | A map of default tags to apply to all taggable resources within the component | `map(string)` | `{}` | no |
18+
| <a name="input_environment"></a> [environment](#input\_environment) | The name of the tfscaffold environment | `string` | n/a | yes |
19+
| <a name="input_group"></a> [group](#input\_group) | The group variables are being inherited from (often synonmous with account short-name) | `string` | n/a | yes |
20+
| <a name="input_log_retention_in_days"></a> [log\_retention\_in\_days](#input\_log\_retention\_in\_days) | The retention period in days for the Cloudwatch Logs events to be retained, default of 0 is indefinite | `number` | `0` | no |
21+
| <a name="input_project"></a> [project](#input\_project) | The name of the tfscaffold project | `string` | n/a | yes |
22+
| <a name="input_region"></a> [region](#input\_region) | The AWS Region | `string` | n/a | yes |
23+
| <a name="input_root_domain_name"></a> [root\_domain\_name](#input\_root\_domain\_name) | The service's root DNS root nameespace, like nonprod.nhsnotify.national.nhs.uk | `string` | `"nonprod.nhsnotify.national.nhs.uk"` | no |
24+
## Modules
25+
26+
No modules.
27+
## Outputs
28+
29+
| Name | Description |
30+
|------|-------------|
31+
| <a name="output_dns_zone"></a> [dns\_zone](#output\_dns\_zone) | n/a |
32+
<!-- vale on -->
33+
<!-- markdownlint-enable -->
34+
<!-- END_TF_DOCS -->
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!-- BEGIN_TF_DOCS -->
2+
<!-- markdownlint-disable -->
3+
<!-- vale off -->
4+
5+
## Requirements
6+
7+
No requirements.
8+
## Inputs
9+
10+
| Name | Description | Type | Default | Required |
11+
|------|-------------|------|---------|:--------:|
12+
| <a name="input_aws_account_id"></a> [aws\_account\_id](#input\_aws\_account\_id) | The AWS Account ID (numeric) | `string` | n/a | yes |
13+
| <a name="input_component"></a> [component](#input\_component) | The variable encapsulating the name of this component | `string` | `"examplecomponent"` | no |
14+
| <a name="input_default_tags"></a> [default\_tags](#input\_default\_tags) | A map of default tags to apply to all taggable resources within the component | `map(string)` | `{}` | no |
15+
| <a name="input_environment"></a> [environment](#input\_environment) | The name of the tfscaffold environment | `string` | n/a | yes |
16+
| <a name="input_group"></a> [group](#input\_group) | The group variables are being inherited from (often synonmous with account short-name) | `string` | n/a | yes |
17+
| <a name="input_log_retention_in_days"></a> [log\_retention\_in\_days](#input\_log\_retention\_in\_days) | The retention period in days for the Cloudwatch Logs events to be retained, default of 0 is indefinite | `number` | `0` | no |
18+
| <a name="input_project"></a> [project](#input\_project) | The name of the tfscaffold project | `string` | n/a | yes |
19+
| <a name="input_region"></a> [region](#input\_region) | The AWS Region | `string` | n/a | yes |
20+
## Modules
21+
22+
No modules.
23+
## Outputs
24+
25+
No outputs.
26+
<!-- vale on -->
27+
<!-- markdownlint-enable -->
28+
<!-- END_TF_DOCS -->

scripts/config/pre-commit.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,10 @@ repos:
5757
entry: ./scripts/githooks/check-terraform-format.sh
5858
language: script
5959
pass_filenames: false
60+
- repo: local
61+
hooks:
62+
- id: generate-terraform-docs
63+
name: Generate Terraform Docs
64+
entry: ./scripts/githooks/check-terraform-docs.sh
65+
language: script
66+
pass_filenames: false

scripts/config/terraform-docs.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
formatter: 'markdown' # this is required
2+
3+
version: ''
4+
5+
recursive:
6+
enabled: false
7+
8+
sections:
9+
hide: []
10+
show: []
11+
12+
content: |-
13+
{{ .Header }}
14+
{{ .Requirements }}
15+
{{ .Inputs }}
16+
{{ .Modules }}
17+
{{ .Outputs }}
18+
{{ .Footer }}
19+
20+
output:
21+
file: 'README.md'
22+
mode: inject
23+
template: |-
24+
<!-- BEGIN_TF_DOCS -->
25+
<!-- markdownlint-disable -->
26+
<!-- vale off -->
27+
{{ .Content }}
28+
<!-- vale on -->
29+
<!-- markdownlint-enable -->
30+
<!-- END_TF_DOCS -->
31+
32+
output-values:
33+
enabled: false
34+
from: ''
35+
36+
sort:
37+
enabled: true
38+
by: name
39+
40+
settings:
41+
anchor: true
42+
color: true
43+
default: true
44+
description: false
45+
escape: true
46+
hide-empty: false
47+
html: true
48+
indent: 2
49+
lockfile: true
50+
read-comments: true
51+
required: true
52+
sensitive: true
53+
type: true
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
3+
# WARNING: Please DO NOT edit this file! It is maintained in the Repository Template (https://github.com/nhs-england-tools/repository-template). Raise a PR instead.
4+
5+
set -euo pipefail
6+
7+
# Pre-commit git hook to check Terraform documentation.
8+
#
9+
# Usage:
10+
# $ [options] ./check-terraform-documentation.sh
11+
#
12+
# Options:
13+
# VERBOSE=true # Show all the executed commands, default is 'false'
14+
15+
# ==============================================================================
16+
17+
function main() {
18+
19+
cd "$(git rev-parse --show-toplevel)"
20+
21+
terraform-docs
22+
}
23+
24+
# Generate Terraform documentation.
25+
# Arguments (provided as environment variables):
26+
# check_only=[do not format, run check only]
27+
function terraform-docs() {
28+
29+
make terraform-docs
30+
git add infrastructure/terraform/**/*.md
31+
}
32+
33+
# ==============================================================================
34+
35+
function is-arg-true() {
36+
37+
if [[ "$1" =~ ^(true|yes|y|on|1|TRUE|YES|Y|ON)$ ]]; then
38+
return 0
39+
else
40+
return 1
41+
fi
42+
}
43+
44+
# ==============================================================================
45+
46+
is-arg-true "${VERBOSE:-false}" && set -x
47+
48+
main "$@"
49+
50+
exit 0

0 commit comments

Comments
 (0)