Skip to content

Post New Blog to Bluesky #81

Post New Blog to Bluesky

Post New Blog to Bluesky #81

name: Post New Blog to Bluesky
on:
workflow_run:
workflows: ["Scheduled Deploy"]
types:
- completed
schedule:
# 8 AM Eastern: 13:00 UTC (EST) / 12:00 UTC (EDT)
# Using 13:00 UTC = 8 AM EST, 9 AM EDT
- cron: '0 13 * * *'
workflow_dispatch:
inputs:
target_date:
description: 'Date to look for posts (YYYY-MM-DD). Defaults to current date.'
required: false
type: string
jobs:
check-trigger:
runs-on: ubuntu-latest
outputs:
should_continue: ${{ steps.check.outputs.should_continue }}
steps:
- name: Check if we should continue
id: check
run: |
if [ "${{ github.event_name }}" == "workflow_run" ]; then
if [ "${{ github.event.workflow_run.conclusion }}" != "success" ]; then
echo "Scheduled Deploy failed, skipping"
echo "should_continue=false" >> $GITHUB_OUTPUT
exit 0
fi
fi
echo "should_continue=true" >> $GITHUB_OUTPUT
detect:
needs: check-trigger
if: needs.check-trigger.outputs.should_continue == 'true'
uses: CodingWithCalvin/.github/.github/workflows/detect-blog-post-from-rss.yml@main
with:
rss_url: 'https://www.codingwithcalvin.net/rss.xml'
target_date: ${{ inputs.target_date }}
notify:
needs: detect
if: needs.detect.outputs.has_posts == 'true'
uses: CodingWithCalvin/.github/.github/workflows/bluesky-post.yml@main
with:
post_text: |
New Blog Post!
[${{ needs.detect.outputs.post_title }}](${{ needs.detect.outputs.post_url }})
${{ needs.detect.outputs.post_hashtags }}
embed_url: ${{ needs.detect.outputs.post_url }}
embed_title: ${{ needs.detect.outputs.post_title }}
embed_description: ${{ needs.detect.outputs.post_description }}
secrets:
BLUESKY_USERNAME: ${{ secrets.BLUESKY_USERNAME }}
BLUESKY_APP_PASSWORD: ${{ secrets.BLUESKY_APP_PASSWORD }}
update-frontmatter:
needs: [detect, notify]
if: needs.detect.outputs.has_posts == 'true' && needs.notify.outputs.post_id != ''
runs-on: ubuntu-latest
steps:
- name: Checkout blog repo
uses: actions/checkout@v4
with:
token: ${{ secrets.CONTRIBUTORS_TOKEN }}
- name: Find and update blog post
run: |
POST_URL="${{ needs.detect.outputs.post_url }}"
POST_ID="${{ needs.notify.outputs.post_id }}"
echo "Post URL: $POST_URL"
echo "Post ID: $POST_ID"
# Extract slug from URL (e.g., https://www.codingwithcalvin.net/my-post/ -> my-post)
SLUG=$(echo "$POST_URL" | sed -E 's|https?://[^/]+/([^/]+)/?|\1|')
echo "Extracted slug: $SLUG"
# Find the markdown file (could be in any year directory)
FILE=$(find src/content/blog -path "*/${SLUG}/index.md" | head -1)
if [ -z "$FILE" ]; then
echo "Could not find file for slug: $SLUG"
exit 1
fi
echo "Found file: $FILE"
# Check if blueskyPostId already exists
if grep -q "^blueskyPostId:" "$FILE"; then
echo "blueskyPostId already exists, skipping"
exit 0
fi
# Add blueskyPostId before the closing --- of frontmatter
# Using awk for more reliable YAML manipulation
awk -v post_id="$POST_ID" '
BEGIN { in_frontmatter = 0; frontmatter_end = 0 }
/^---$/ {
if (in_frontmatter == 0) {
in_frontmatter = 1
print
next
} else {
print "blueskyPostId: \"" post_id "\""
in_frontmatter = 0
frontmatter_end = 1
}
}
{ print }
' "$FILE" > "$FILE.tmp" && mv "$FILE.tmp" "$FILE"
echo "Updated $FILE with blueskyPostId: $POST_ID"
echo "New frontmatter:"
head -20 "$FILE"
- name: Commit and push
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add -A
if git diff --staged --quiet; then
echo "No changes to commit"
exit 0
fi
git commit -m "chore(blog): add blueskyPostId to post frontmatter"
git push