try ci workflow #2
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: Update Last Updated Date | |
| on: | |
| pull_request: | |
| types: [opened, synchronize] | |
| branches: | |
| - main | |
| jobs: | |
| update-last-updated: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: ${{ github.head_ref }} | |
| fetch-depth: 0 | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Get changed MDX files | |
| id: changed-files | |
| uses: tj-actions/changed-files@v45 | |
| with: | |
| files: | | |
| **/*.mdx | |
| - name: Update last-updated frontmatter | |
| if: steps.changed-files.outputs.any_changed == 'true' | |
| run: | | |
| # Get current date in "Month Day, Year" format | |
| CURRENT_DATE=$(date +"%B %-d, %Y") | |
| echo "Current date: $CURRENT_DATE" | |
| for file in ${{ steps.changed-files.outputs.all_changed_files }}; do | |
| echo "Processing: $file" | |
| if [ ! -f "$file" ]; then | |
| echo "File not found, skipping: $file" | |
| continue | |
| fi | |
| # Check if file has frontmatter (starts with ---) | |
| if ! head -1 "$file" | grep -q "^---"; then | |
| echo "No frontmatter found, skipping: $file" | |
| continue | |
| elif grep -q "^last-updated:" "$file"; then | |
| echo "Updating existing last-updated field" | |
| # Update existing last-updated field | |
| sed -i "s/^last-updated:.*$/last-updated: $CURRENT_DATE/" "$file" | |
| else | |
| echo "Adding last-updated field to existing frontmatter" | |
| # Add last-updated before the closing --- (bottom of frontmatter) | |
| awk -v date="$CURRENT_DATE" ' | |
| BEGIN { in_frontmatter=0; added=0 } | |
| NR==1 && /^---$/ { in_frontmatter=1; print; next } | |
| in_frontmatter && /^---$/ && !added { print "last-updated: " date; added=1; print; in_frontmatter=0; next } | |
| { print } | |
| ' "$file" > "${file}.tmp" && mv "${file}.tmp" "$file" | |
| fi | |
| done | |
| - name: Commit changes | |
| if: steps.changed-files.outputs.any_changed == 'true' | |
| run: | | |
| git config --local user.email "github-actions[bot]@users.noreply.github.com" | |
| git config --local user.name "github-actions[bot]" | |
| git add -A | |
| if git diff --staged --quiet; then | |
| echo "No changes to commit" | |
| else | |
| git commit -m "chore: update last-updated date in MDX files" | |
| git push | |
| fi |