[GitHub] Unused partials check #11
Workflow file for this run
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
| name: Partial audit | |
| # **What it does**: Regularly audits our documentation for unused image files. | |
| # **Why we have it**: Good hygiene + helps simplify future image audits. | |
| # **Who does it impact**: PCX team | |
| on: | |
| pull_request: | |
| branches: | |
| - production | |
| workflow_dispatch: | |
| schedule: | |
| - cron: "0 0 1 * *" | |
| jobs: | |
| partial-audit: | |
| permissions: | |
| issues: write | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout Repository | |
| uses: actions/checkout@v4 | |
| - name: Find Unused Files | |
| id: find-files | |
| run: | | |
| # Find all .mdx files, but only in the ./src/content/partials directory | |
| FILES=$(find ./src/content/partials -type f -name "*.mdx") | |
| # Check if files are referenced in any Render component | |
| UNUSED_FILES="" | |
| for FILE in $FILES; do | |
| # Extract product and file name from the path | |
| TEMP_STRING=${FILE#*partials/} | |
| PRODUCT=${TEMP_STRING%%/*} | |
| TEMP_PATH=${FILE#*src/content/partials/*/} | |
| FILENAME=${TEMP_PATH%.mdx} | |
| # Construct the individual search strings for product and file | |
| SEARCH_PRODUCT="product=\"$PRODUCT\"" | |
| SEARCH_FILE="file=\"$FILENAME\"" | |
| # --- START LOGGING --- | |
| echo "----------------------------------------" | |
| echo "Checking file: $FILE" | |
| echo " - SEARCH_PRODUCT: $SEARCH_PRODUCT" | |
| echo " - SEARCH_FILE: $SEARCH_FILE" | |
| # --- END LOGGING --- | |
| # This command checks if the combination of file and product exists. | |
| if ! perl -ne 'BEGIN{undef $/;} ($found=0); if (m/<Render.*?file="file".*?product="product".*?\/>/s) {$found=1;} print "FOUND\n" if $found; ' YourFile.mdx | grep -q 'FOUND'; then | |
| UNUSED_FILES+="$FILE\n" | |
| echo " -> UNUSED! Adding to list." | |
| else | |
| echo " -> Used. Skipping." | |
| fi | |
| done | |
| # If there are unused files, output them | |
| if [ -n "$UNUSED_FILES" ]; then | |
| echo "unused_files=$UNUSED_FILES" >> "$GITHUB_OUTPUT" | |
| fi |