| 1 | name: Update last updated date |
| 2 | |
| 3 | # Trigger this workflow when PRs are opened or updated |
| 4 | on: |
| 5 | pull_request: |
| 6 | types: [opened, synchronize] |
| 7 | branches: |
| 8 | - main # Adjust to match your main branch name |
| 9 | |
| 10 | jobs: |
| 11 | update-last-updated: |
| 12 | runs-on: ubuntu-latest |
| 13 | permissions: |
| 14 | contents: write # Required to commit changes |
| 15 | pull-requests: write # Required to update the PR |
| 16 | steps: |
| 17 | # Step 1: Check out the PR branch |
| 18 | - name: Checkout repository |
| 19 | uses: actions/checkout@v4 |
| 20 | with: |
| 21 | ref: ${{ github.head_ref }} # Check out the PR's source branch |
| 22 | fetch-depth: 0 |
| 23 | token: ${{ secrets.GITHUB_TOKEN }} |
| 24 | |
| 25 | # Step 2: Identify which MDX files changed in this PR |
| 26 | - name: Get changed MDX files |
| 27 | id: changed-files |
| 28 | uses: tj-actions/changed-files@v45 |
| 29 | with: |
| 30 | files: | |
| 31 | **/*.mdx # Only track changes to .mdx files |
| 32 | |
| 33 | # Step 3: Update the last-updated field in each changed MDX file |
| 34 | - name: Update last-updated frontmatter |
| 35 | if: steps.changed-files.outputs.any_changed == 'true' |
| 36 | run: | |
| 37 | # Generate current date in "Month Day, Year" format (e.g., "December 11, 2025") |
| 38 | # Modify the date format here if you prefer a different style |
| 39 | CURRENT_DATE=$(date +"%B %-d, %Y") |
| 40 | echo "Current date: $CURRENT_DATE" |
| 41 | |
| 42 | # Process each changed MDX file |
| 43 | for file in ${{ steps.changed-files.outputs.all_changed_files }}; do |
| 44 | echo "Processing: $file" |
| 45 | |
| 46 | # Skip if file was deleted or doesn't exist |
| 47 | if [ ! -f "$file" ]; then |
| 48 | echo "File not found, skipping: $file" |
| 49 | continue |
| 50 | fi |
| 51 | |
| 52 | # Check if file has frontmatter (must start with ---) |
| 53 | if ! head -1 "$file" | grep -q "^---"; then |
| 54 | echo "No frontmatter found, skipping: $file" |
| 55 | continue |
| 56 | # If file already has a last-updated field, update it |
| 57 | elif grep -q "^last-updated:" "$file"; then |
| 58 | echo "Updating existing last-updated field" |
| 59 | sed -i "s/^last-updated:.*$/last-updated: $CURRENT_DATE/" "$file" |
| 60 | # If file has frontmatter but no last-updated field, add it |
| 61 | else |
| 62 | echo "Adding last-updated field to existing frontmatter" |
| 63 | # This awk command inserts the last-updated field just before the closing --- |
| 64 | awk -v date="$CURRENT_DATE" ' |
| 65 | BEGIN { in_frontmatter=0; added=0 } |
| 66 | NR==1 && /^---$/ { in_frontmatter=1; print; next } |
| 67 | in_frontmatter && /^---$/ && !added { print "last-updated: " date; added=1; print; in_frontmatter=0; next } |
| 68 | { print } |
| 69 | ' "$file" > "${file}.tmp" && mv "${file}.tmp" "$file" |
| 70 | fi |
| 71 | done |
| 72 | |
| 73 | # Step 4: Commit and push the updated files back to the PR |
| 74 | - name: Commit changes |
| 75 | if: steps.changed-files.outputs.any_changed == 'true' |
| 76 | run: | |
| 77 | git config --local user.email "github-actions[bot]@users.noreply.github.com" |
| 78 | git config --local user.name "github-actions[bot]" |
| 79 | git add -A |
| 80 | # Only commit if there are actual changes |
| 81 | if git diff --staged --quiet; then |
| 82 | echo "No changes to commit" |
| 83 | else |
| 84 | git commit -m "chore: update last-updated date in MDX files" |
| 85 | git push |
| 86 | fi |