-
-
Notifications
You must be signed in to change notification settings - Fork 0
Allow repository to be used in GitHub Actions #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
b59721f
feat(gha): download + run binary with given dirs
allejo 0d568db
feat(gha): add opt-in commiting functionality
allejo bf14b8e
feat(gha): add fail-on-diff mode
allejo eb47028
feat(gha): save JSON representation of each processed module as output
allejo d490b58
feat(gha): treat fail_on_diff and commit as booleans
allejo 74540ea
docs(gha): update README with new action documentation
allejo 3bab149
feat(gha): support * and ** for directory paths
allejo 3a8db2a
feat(gha): download latest pre-release if no stable
allejo 65a30eb
docs: update actions/checkout version in README
allejo 3336954
fix(gha): address Copilot review feedback
allejo 0e68c54
fix(gha): implement unused commit_branch arg
allejo 18d8f09
feat(gha): add format + linting jobs to workflows
allejo dc2dff7
chore(gha): address first batch of linting errors
allejo 5c77eca
docs: format README + address linter warnings
allejo 4f6f6b7
fix(gha): don't fail silently on GH API calls
allejo 488bf12
docs: show GH template syntax for token in arg table
allejo feac1c4
feat(gha): apply PR feedback from Copilot
allejo 8e98cb2
feat(gha): use GH_TOKEN for gh/git operations
allejo 55df454
fix(ci/cd): setup Go before formatting step
allejo 03faae3
feat(gha): enable Bash debug when runner.debug is true
allejo 9e9a271
feat(gha): don't silence gh errors
allejo 13b0969
fix(gha): don't kill workflow when 'gh' command fails
allejo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,235 @@ | ||
| name: 'tfdocs-extras' | ||
| description: 'Generate Terraform object() type documentation in README.md files using tfdocs-extras' | ||
| author: 'Vladimir "allejo" Jimenez' | ||
|
|
||
| branding: | ||
| icon: 'book-open' | ||
| color: 'purple' | ||
|
|
||
| inputs: | ||
| directories: | ||
| description: | | ||
| Newline-separated list of Terraform module directories to process. Each directory | ||
| will be checked for a README.md containing TFDOCS_EXTRAS_START/TFDOCS_EXTRAS_END | ||
| markers and processed if found. | ||
|
allejo marked this conversation as resolved.
|
||
| required: true | ||
| version: | ||
| description: 'Version of tfdocs-extras to download (e.g. "v0.1.0"). Defaults to the latest release.' | ||
| required: false | ||
| default: 'latest' | ||
| token: | ||
| description: 'GitHub token used to download the release binary and avoid API rate limits.' | ||
| required: false | ||
| default: ${{ github.token }} | ||
|
allejo marked this conversation as resolved.
Outdated
|
||
| commit: | ||
| description: 'Commit any README.md changes after processing.' | ||
| required: false | ||
| default: 'false' | ||
| commit_message: | ||
| description: 'Commit message to use when commit is true.' | ||
| required: false | ||
| default: 'chore: update tfdocs-extras documentation' | ||
| commit_author: | ||
| description: 'Author identity for the commit in "Name <email>" format. Only used when commit is true.' | ||
| required: false | ||
| default: 'github-actions[bot] <github-actions[bot]@users.noreply.github.com>' | ||
| commit_branch: | ||
| description: 'Branch to push the commit to. Only used when commit is true. Defaults to the current branch.' | ||
| required: false | ||
| default: '' | ||
|
allejo marked this conversation as resolved.
Outdated
|
||
| fail_on_diff: | ||
| description: 'Exit with a non-zero status if any README.md files were modified. Mutually exclusive with commit.' | ||
| required: false | ||
| default: 'false' | ||
| json_output_file: | ||
| description: 'Path to write the aggregated JSON output. If not specified, JSON is only written to the step output (subject to size limits).' | ||
| required: false | ||
| default: '' | ||
|
|
||
| outputs: | ||
| result: | ||
| description: 'JSON array of module manifests, one object per processed directory.' | ||
| value: ${{ steps.collect-json.outputs.result }} | ||
|
|
||
| runs: | ||
| using: composite | ||
| steps: | ||
| - name: Validate inputs | ||
| shell: bash | ||
| run: | | ||
| if ${{ fromJSON(inputs.commit) }} && ${{ fromJSON(inputs.fail_on_diff) }}; then | ||
| echo "::error::commit and fail_on_diff are mutually exclusive" | ||
| exit 1 | ||
| fi | ||
|
|
||
| - name: Download tfdocs-extras | ||
| shell: bash | ||
| env: | ||
| GH_TOKEN: ${{ inputs.token }} | ||
|
allejo marked this conversation as resolved.
Outdated
|
||
| VERSION: ${{ inputs.version }} | ||
| run: | | ||
| # Detect OS | ||
| case "${{ runner.os }}" in | ||
| Linux) BINARY_OS="linux" ;; | ||
| macOS) BINARY_OS="darwin" ;; | ||
| Windows) BINARY_OS="windows" ;; | ||
| *) echo "::error::Unsupported OS: ${{ runner.os }}"; exit 1 ;; | ||
| esac | ||
|
|
||
| # Detect architecture | ||
| case "${{ runner.arch }}" in | ||
| X64) BINARY_ARCH="amd64" ;; | ||
| ARM64) BINARY_ARCH="arm64" ;; | ||
| *) echo "::error::Unsupported architecture: ${{ runner.arch }}"; exit 1 ;; | ||
| esac | ||
|
|
||
| # Resolve 'latest' to the actual version tag | ||
| if [ "$VERSION" = "latest" ]; then | ||
| # Prefer the latest stable release; gh release view only returns a | ||
| # release marked "latest" on GitHub, which is never a pre-release. | ||
| VERSION=$(gh release view \ | ||
| --repo FriendsOfTerraform/tfdocs-extras \ | ||
| --json tagName \ | ||
| --jq '.tagName' 2>/dev/null) || true | ||
|
|
||
| if [ -z "$VERSION" ]; then | ||
| # No stable release exists; fall back to the most recent pre-release. | ||
| echo "No stable release found, checking for pre-releases..." | ||
| VERSION=$(gh release list \ | ||
| --repo FriendsOfTerraform/tfdocs-extras \ | ||
| --json tagName,isPrerelease \ | ||
| --jq '[.[] | select(.isPrerelease)] | first | .tagName') | ||
|
allejo marked this conversation as resolved.
Outdated
|
||
|
|
||
| if [ -z "$VERSION" ] || [ "$VERSION" = "null" ]; then | ||
| echo "::error::No stable release or pre-release found" | ||
| exit 1 | ||
| fi | ||
| echo "Resolved latest pre-release: $VERSION" | ||
| else | ||
| echo "Resolved latest stable release: $VERSION" | ||
| fi | ||
| fi | ||
|
|
||
| # Build binary name matching the release asset naming convention | ||
| EXT="" | ||
| [ "$BINARY_OS" = "windows" ] && EXT=".exe" | ||
| BINARY_NAME="tfdocs-extras-${VERSION}-${BINARY_OS}-${BINARY_ARCH}${EXT}" | ||
| DEST="${RUNNER_TEMP}/tfdocs-extras${EXT}" | ||
|
|
||
| echo "Downloading $BINARY_NAME..." | ||
| gh release download "$VERSION" \ | ||
| --repo FriendsOfTerraform/tfdocs-extras \ | ||
| --pattern "$BINARY_NAME" \ | ||
| --output "$DEST" | ||
| chmod +x "$DEST" | ||
|
|
||
| echo "TFDOCS_EXTRAS_BIN=$DEST" >> "$GITHUB_ENV" | ||
|
|
||
| - name: Process README.md files | ||
| id: collect-json | ||
| shell: bash | ||
| env: | ||
| DIRECTORIES: ${{ inputs.directories }} | ||
| JSON_OUTPUT_FILE: ${{ inputs.json_output_file }} | ||
| run: | | ||
| # globstar enables ** for recursive matching; nullglob silently drops | ||
| # patterns that match nothing rather than treating them as literals | ||
| shopt -s globstar nullglob | ||
|
|
||
| MARKER_START="<!-- TFDOCS_EXTRAS_START -->" | ||
| MARKER_END="<!-- TFDOCS_EXTRAS_END -->" | ||
| PROCESSED=0 | ||
| JSON_ITEMS="" | ||
|
|
||
| while IFS= read -r pattern; do | ||
| # Skip blank/whitespace-only lines | ||
| [ -z "$(echo "$pattern" | tr -d '[:space:]')" ] && continue | ||
|
|
||
| # Unquoted expansion triggers glob/globstar resolution | ||
| for dir in $pattern; do | ||
|
allejo marked this conversation as resolved.
Outdated
|
||
| [ -d "$dir" ] || continue | ||
|
|
||
| readme="${dir}/README.md" | ||
| [ -f "$readme" ] || continue | ||
| grep -qF "$MARKER_START" "$readme" || continue | ||
| grep -qF "$MARKER_END" "$readme" || continue | ||
|
allejo marked this conversation as resolved.
Outdated
|
||
|
|
||
| echo "Processing: $dir" | ||
| "$TFDOCS_EXTRAS_BIN" "$dir" | ||
|
|
||
| JSON=$("$TFDOCS_EXTRAS_BIN" -json "$dir") | ||
|
allejo marked this conversation as resolved.
Outdated
|
||
| [ -n "$JSON_ITEMS" ] && JSON_ITEMS+="," | ||
| JSON_ITEMS+="$JSON" | ||
|
|
||
| PROCESSED=$((PROCESSED + 1)) | ||
| done | ||
| done <<< "$DIRECTORIES" | ||
|
|
||
| echo "Done: processed $PROCESSED director(ies)" | ||
|
allejo marked this conversation as resolved.
Outdated
|
||
|
|
||
| # Build the final JSON array | ||
| FINAL_JSON="[${JSON_ITEMS}]" | ||
|
|
||
| # Write to file if path is specified | ||
| if [ -n "$JSON_OUTPUT_FILE" ]; then | ||
| echo "Writing JSON output to: $JSON_OUTPUT_FILE" | ||
|
allejo marked this conversation as resolved.
|
||
| echo "$FINAL_JSON" > "$JSON_OUTPUT_FILE" | ||
| fi | ||
|
|
||
| # GitHub Actions outputs have a size limit of ~1MB. Only set the output | ||
| # if the JSON is below this threshold. | ||
| JSON_SIZE=${#FINAL_JSON} | ||
| MAX_OUTPUT_SIZE=1000000 # ~1MB | ||
|
|
||
| if [ $JSON_SIZE -lt $MAX_OUTPUT_SIZE ]; then | ||
| { | ||
| echo 'result<<EOF' | ||
| echo "$FINAL_JSON" | ||
| echo 'EOF' | ||
| } >> "$GITHUB_OUTPUT" | ||
| else | ||
| echo "::warning::JSON output size ($JSON_SIZE bytes) exceeds GitHub Actions output limit (~1MB). Output not set. Use json_output_file input to write to a file instead." | ||
| fi | ||
|
|
||
| - name: Commit changes | ||
| if: ${{ fromJSON(inputs.commit) }} | ||
| shell: bash | ||
| env: | ||
| COMMIT_MESSAGE: ${{ inputs.commit_message }} | ||
| COMMIT_AUTHOR: ${{ inputs.commit_author }} | ||
| run: | | ||
| if git diff --quiet; then | ||
| echo "No changes to commit" | ||
| exit 0 | ||
| fi | ||
|
|
||
| AUTHOR_NAME="${COMMIT_AUTHOR% <*}" | ||
| AUTHOR_EMAIL="${COMMIT_AUTHOR#*<}"; AUTHOR_EMAIL="${AUTHOR_EMAIL%>}" | ||
| git config user.name "$AUTHOR_NAME" | ||
| git config user.email "$AUTHOR_EMAIL" | ||
| git add -u | ||
| git commit -m "$COMMIT_MESSAGE" | ||
|
|
||
| BRANCH="$(git rev-parse --abbrev-ref HEAD)" | ||
| if [ "$BRANCH" = "HEAD" ]; then | ||
| # Detached HEAD; try to derive a branch name from GitHub context | ||
| if [ -n "${{ github.head_ref }}" ]; then | ||
| BRANCH="${{ github.head_ref }}" | ||
| elif [ -n "${{ github.ref_name }}" ]; then | ||
| BRANCH="${{ github.ref_name }}" | ||
| else | ||
| echo "::error::Cannot determine branch to push to (detached HEAD and no commit_branch, github.head_ref, or github.ref_name)" | ||
| exit 1 | ||
| fi | ||
| fi | ||
|
|
||
| git push origin "$BRANCH" | ||
|
allejo marked this conversation as resolved.
Outdated
allejo marked this conversation as resolved.
|
||
|
|
||
| - name: Check for diff | ||
| if: ${{ fromJSON(inputs.fail_on_diff) }} | ||
| shell: bash | ||
| run: | | ||
| if ! git diff --quiet; then | ||
| echo "::error::README.md files have uncommitted changes after processing" | ||
| exit 1 | ||
| fi | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.