1- name : preview-docs
1+ name : Preview Docs
22
3- on : pull_request
3+ on :
4+ pull_request_target :
5+ types : [opened, synchronize, ready_for_review]
6+ branches :
7+ - main
48
59jobs :
610 run :
711 runs-on : ubuntu-latest
812 permissions :
9- contents : read
10- pull-requests : write
13+ pull-requests : write # Only for commenting
14+ contents : read # For checking out code
1115 steps :
1216 - name : Checkout repository
1317 uses : actions/checkout@v4
18+ with :
19+ fetch-depth : 0 # Fetch full history for git diff
20+
21+ - name : Checkout PR
22+ run : |
23+ git fetch origin pull/${{ github.event.pull_request.number }}/head:pr-${{ github.event.pull_request.number }}
24+ git checkout pr-${{ github.event.pull_request.number }}
1425
1526 - name : Setup Fern CLI
1627 uses : fern-api/setup-fern-cli@v1
@@ -20,15 +31,53 @@ jobs:
2031 env :
2132 FERN_TOKEN : ${{ secrets.FERN_TOKEN }}
2233 run : |
23- OUTPUT=$(fern generate --docs --preview 2>&1) || true
34+ OUTPUT=$(fern generate --docs --preview --instance fern-api.docs.buildwithfern.com/learn 2>&1) || true
2435 echo "$OUTPUT"
2536 URL=$(echo "$OUTPUT" | grep -oP 'Published docs to \K.*(?= \()')
37+ echo "preview_url=$URL" >> $GITHUB_OUTPUT
2638 echo "Preview URL: $URL"
27- echo "🌿 Preview your docs: $URL" > preview_url.txt
2839
29- - name : Comment URL in PR
40+ - name : Get page links for changed MDX files
41+ id : page-links
42+ env :
43+ FERN_TOKEN : ${{ secrets.FERN_TOKEN }}
44+ run : |
45+ PREVIEW_URL="${{ steps.generate-docs.outputs.preview_url }}"
46+ CHANGED_FILES=$(git diff --name-only origin/main...HEAD -- '*.mdx' 2>/dev/null || echo "")
47+
48+ if [ -z "$CHANGED_FILES" ] || [ -z "$PREVIEW_URL" ]; then
49+ echo "page_links=" >> $GITHUB_OUTPUT; exit 0
50+ fi
51+
52+ BASE_URL=$(echo "$PREVIEW_URL" | grep -oP 'https?://[^/]+')
53+
54+ FILES_PARAM=$(echo "$CHANGED_FILES" | tr '\n' ',' | sed 's/,$//')
55+ RESPONSE=$(curl -sf -H "FERN_TOKEN: $FERN_TOKEN" "${PREVIEW_URL}/api/fern-docs/get-slug-for-file?files=${FILES_PARAM}" 2>/dev/null) || {
56+ echo "page_links=" >> $GITHUB_OUTPUT; exit 0
57+ }
58+
59+ PAGE_LINKS=$(echo "$RESPONSE" | jq -r --arg url "$BASE_URL" \
60+ '.mappings[] | select(.slug != null) | "- [\(.slug)](\($url)/\(.slug))"')
61+
62+ if [ -n "$PAGE_LINKS" ]; then
63+ { echo "page_links<<EOF"; echo "$PAGE_LINKS"; echo "EOF"; } >> $GITHUB_OUTPUT
64+ else
65+ echo "page_links=" >> $GITHUB_OUTPUT
66+ fi
67+
68+ - name : Create comment content
69+ run : |
70+ echo ":herb: **Preview your docs:** <${{ steps.generate-docs.outputs.preview_url }}>" > comment.md
71+
72+ if [ -n "${{ steps.page-links.outputs.page_links }}" ]; then
73+ echo "" >> comment.md
74+ echo "Here are the markdown pages you've updated:" >> comment.md
75+ echo "${{ steps.page-links.outputs.page_links }}" >> comment.md
76+ fi
77+
78+ - name : Post PR comment
3079 uses : thollander/actions-comment-pull-request@v2.4.3
3180 with :
32- filePath : preview_url.txt
81+ filePath : comment.md
3382 comment_tag : preview-docs
3483 mode : upsert
0 commit comments