Skip to content

Merge pull request #25 from openteams-ai/test/canary-auto-record-v2 #26

Merge pull request #25 from openteams-ai/test/canary-auto-record-v2

Merge pull request #25 from openteams-ai/test/canary-auto-record-v2 #26

Workflow file for this run

name: Publish to WordPress
on:
push:
branches: [main]
paths:
- 'authors.yml'
- 'posts/**'
jobs:
detect:
runs-on: ubuntu-latest
outputs:
authors-changed: ${{ steps.detect.outputs.authors-changed }}
post-files: ${{ steps.detect.outputs.post-files }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2 # Need previous commit to detect changed files
- name: Detect changed paths
id: detect
run: |
# Did authors.yml change in this push?
if git diff --name-only HEAD~1 HEAD | grep -qx 'authors.yml'; then
echo "authors-changed=true" >> "$GITHUB_OUTPUT"
else
echo "authors-changed=false" >> "$GITHUB_OUTPUT"
fi
# Directly-changed post files (added or modified)
DIRECT=$(git diff --name-only --diff-filter=AM HEAD~1 HEAD -- 'posts/*.md' 'posts/*.qmd')
# Posts whose images changed: map posts/images/<slug>/... back to posts/<slug>.{md,qmd}
IMAGE_CHANGES=$(git diff --name-only --diff-filter=AM HEAD~1 HEAD -- 'posts/images/**')
FROM_IMAGES=()
while IFS= read -r img; do
[ -z "$img" ] && continue
slug=$(echo "$img" | awk -F/ '{print $3}')
[ -z "$slug" ] && continue
for ext in md qmd; do
if [ -f "posts/$slug.$ext" ]; then
FROM_IMAGES+=("posts/$slug.$ext")
break
fi
done
done <<< "$IMAGE_CHANGES"
IMAGE_POSTS=""
if [ ${#FROM_IMAGES[@]} -gt 0 ]; then
IMAGE_POSTS=$(printf '%s\n' "${FROM_IMAGES[@]}")
fi
FILES=$(printf '%s\n%s\n' "$DIRECT" "$IMAGE_POSTS" | sed '/^$/d' | sort -u)
echo "post-files<<EOF" >> "$GITHUB_OUTPUT"
echo "$FILES" >> "$GITHUB_OUTPUT"
echo "EOF" >> "$GITHUB_OUTPUT"
sync-authors:
needs: detect
if: needs.detect.outputs.authors-changed == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: astral-sh/setup-uv@v4
- name: Install dependencies
run: uv sync
- name: Sync authors
env:
WP_TOKEN: ${{ secrets.WP_TOKEN }}
WP_API_URL: ${{ secrets.WP_API_URL }}
WP_USERNAME: ${{ secrets.WP_USERNAME }}
run: uv run scripts/wordpress/sync_authors.py
publish:
needs: [detect, sync-authors]
# Run when there are posts to publish AND sync-authors either succeeded
# or was skipped (no authors change). !cancelled() lets this evaluate
# even when sync-authors is skipped, which would otherwise cascade-skip.
if: |
!cancelled() &&
needs.detect.result == 'success' &&
needs.detect.outputs.post-files != '' &&
(needs.sync-authors.result == 'success' || needs.sync-authors.result == 'skipped')
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- uses: actions/checkout@v4
- uses: astral-sh/setup-uv@v4
- name: Install dependencies
run: uv sync
- name: Publish articles
env:
WP_TOKEN: ${{ secrets.WP_TOKEN }}
WP_API_URL: ${{ secrets.WP_API_URL }}
WP_USERNAME: ${{ secrets.WP_USERNAME }}
SLACK_PUBLISH_WEBHOOK: ${{ secrets.SLACK_PUBLISH_WEBHOOK }}
run: |
while IFS= read -r file; do
[ -z "$file" ] && continue
echo "Publishing: $file"
uv run scripts/wordpress/publish.py --status publish "$file"
done <<< "${{ needs.detect.outputs.post-files }}"
- name: Open metadata PR
uses: peter-evans/create-pull-request@v7
with:
branch: auto/wordpress-metadata-${{ github.run_id }}
base: main
add-paths: posts/**
commit-message: "chore(publish): record wordpress_id/url"
title: "chore(publish): record wordpress_id/url"
body: |
Automated metadata write-back from publish workflow [run ${{ github.run_id }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}).
delete-branch: true