chore: update GitHub Action #2
This file contains 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 Modified Date | ||
on: | ||
push: | ||
branches: | ||
- blog | ||
workflow_dispatch: | ||
jobs: | ||
updateLastModified: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v3 | ||
- name: Set up Ruby | ||
uses: ruby/setup-ruby@v1 | ||
with: | ||
ruby-version: '3.3' | ||
- name: Install dependencies | ||
run: bundle install | ||
- name: Update last_modified_at field | ||
run: | | ||
posts_dir="_posts" | ||
for post_path in $(find "$posts_dir" -name "*.md"); do | ||
commit_count=$(git rev-list --count HEAD "$post_path") | ||
last_modified=$(git log -1 --pretty="%ad" --date=format:'%Y-%m-%d %H:%M %z' "$post_path" | xargs) | ||
content=$(cat "$post_path") | ||
if [[ "$content" =~ ^date:\ (.+) ]]; then | ||
if [[ "$content" =~ ^last_modified_at: ]]; then | ||
sed -i "s/^last_modified_at:.*/last_modified_at: $last_modified/" "$post_path" | ||
elif [[ $commit_count -gt 1 ]]; then | ||
sed -i "/^date: /a last_modified_at: $last_modified" "$post_path" | ||
fi | ||
fi | ||
done | ||
- name: Commit changes | ||
run: | | ||
git config --global user.name "GitHub Action" | ||
git config --global user.email "[email protected]" | ||
git add _posts/*.md | ||
git commit -m "Update last_modified_at field in posts" || echo "No changes to commit" | ||
deployToCloudflare: | ||
runs-on: ubuntu-latest | ||
needs: updateLastModified # Ensure this runs after the updateLastModified job | ||
steps: | ||
- name: Push changes | ||
run: git push origin blog | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
with: | ||
apiToken: ${{ secrets.CF_API_TOKEN }} | ||
accountId: ${{ secrets.CF_ACCOUNT_ID }} | ||
projectName: "stevehoang" | ||
directory: "_site" |