ci: fix preview workflow — single comment with changed page links #30
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: Preview Docs | |
| on: | |
| pull_request: | |
| types: [opened, synchronize, ready_for_review] | |
| jobs: | |
| preview: | |
| if: github.event.pull_request.head.repo.full_name == github.repository | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| pull-requests: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| persist-credentials: false | |
| - name: Checkout PR | |
| run: | | |
| git fetch origin pull/${{ github.event.pull_request.number }}/head:pr-${{ github.event.pull_request.number }} | |
| git checkout pr-${{ github.event.pull_request.number }} | |
| - name: Setup Fern CLI | |
| uses: fern-api/setup-fern-cli@v1 | |
| - name: Generate previews and build comment | |
| env: | |
| FERN_TOKEN: ${{ secrets.FERN_TOKEN }} | |
| PR_NUMBER: ${{ github.event.pull_request.number }} | |
| run: | | |
| PROJECTS="docs-starter graphql i18n multi-source/homepage multi-source/seeds multi-source/seeds-sunflower multi-source/seeds-tomato multi-source/greenhouses multi-source/nursery versioning" | |
| CHANGED_FILES=$(git diff --name-only origin/main...HEAD) | |
| : > comment.md | |
| HAS_CONTENT=false | |
| for project in $PROJECTS; do | |
| PROJECT_CHANGES=$(echo "$CHANGED_FILES" | grep "^${project}/" || true) | |
| if [ -z "$PROJECT_CHANGES" ]; then | |
| continue | |
| fi | |
| SAFE_PROJECT=$(echo "$project" | tr '/' '-') | |
| pushd "$project" > /dev/null | |
| OUTPUT=$(fern generate --docs --preview --id "pr${PR_NUMBER}-${SAFE_PROJECT}" 2>&1) || true | |
| popd > /dev/null | |
| echo "$OUTPUT" | |
| URL=$(echo "$OUTPUT" | grep -oP 'Published docs to \K.*(?= \()') | |
| if [ -z "$URL" ]; then | |
| continue | |
| fi | |
| BASE_URL=$(echo "$URL" | grep -oP 'https?://[^/]+') | |
| MDX_CHANGES=$(echo "$PROJECT_CHANGES" | grep '\.mdx$' | sed "s|^${project}/||" || true) | |
| PAGE_LINKS="" | |
| if [ -n "$MDX_CHANGES" ]; then | |
| FILES_PARAM=$(echo "$MDX_CHANGES" | tr '\n' ',' | sed 's/,$//') | |
| RESPONSE=$(curl -sf -H "FERN_TOKEN: $FERN_TOKEN" "${URL}/api/fern-docs/get-slug-for-file?files=${FILES_PARAM}" 2>/dev/null) || true | |
| if [ -n "$RESPONSE" ]; then | |
| PAGE_LINKS=$(echo "$RESPONSE" | jq -r --arg url "$BASE_URL" \ | |
| '.mappings[] | select(.slug != null) | "- [\(.slug)](\($url)/\(.slug))"' 2>/dev/null || true) | |
| fi | |
| fi | |
| echo ":herb: **Preview \`${project}\`:** <${URL}>" >> comment.md | |
| if [ -n "$PAGE_LINKS" ]; then | |
| echo "" >> comment.md | |
| echo "Here are the markdown pages you've updated:" >> comment.md | |
| echo "$PAGE_LINKS" >> comment.md | |
| fi | |
| echo "" >> comment.md | |
| HAS_CONTENT=true | |
| done | |
| if [ "$HAS_CONTENT" = false ]; then | |
| echo ":herb: No docs changes detected in this PR." > comment.md | |
| fi | |
| - name: Post PR comment | |
| uses: thollander/actions-comment-pull-request@v3 | |
| with: | |
| file-path: comment.md | |
| comment-tag: preview-docs | |
| mode: upsert |