11name : Preview Docs
22
3- on : pull_request
3+ on :
4+ pull_request :
5+ types : [opened, synchronize, ready_for_review]
46
57jobs :
6- run :
8+ preview :
9+ if : github.event.pull_request.head.repo.full_name == github.repository
710 runs-on : ubuntu-latest
811 permissions :
912 contents : read
1013 pull-requests : write
11- strategy :
12- fail-fast : false
13- matrix :
14- project : [docs-starter, i18n, multi-source/homepage, multi-source/seeds, multi-source/seeds-sunflower, multi-source/seeds-tomato, multi-source/greenhouses, multi-source/nursery, versioning]
1514 steps :
1615 - name : Checkout repository
1716 uses : actions/checkout@v4
1817 with :
19- fetch-depth : 0 # Fetch full history for git diff
18+ fetch-depth : 0
19+ persist-credentials : false
2020
2121 - name : Checkout PR
2222 run : |
@@ -26,60 +26,65 @@ jobs:
2626 - name : Setup Fern CLI
2727 uses : fern-api/setup-fern-cli@v1
2828
29- - name : Generate preview URL
30- id : generate-docs
31- working-directory : ${{ matrix.project }}
29+ - name : Generate previews and build comment
3230 env :
3331 FERN_TOKEN : ${{ secrets.FERN_TOKEN }}
34- HEAD_REF : ${{ github.head_ref }}
32+ PR_NUMBER : ${{ github.event.pull_request.number }}
3533 run : |
36- OUTPUT=$(fern generate --docs --preview --id "${HEAD_REF}-${{ matrix.project }}" 2>&1) || true
37- echo "$OUTPUT"
38- URL=$(echo "$OUTPUT" | grep -oP 'Published docs to \K.*(?= \()')
39- echo "preview_url=$URL" >> $GITHUB_OUTPUT
40- echo "Preview URL: $URL"
34+ 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"
35+ CHANGED_FILES=$(git diff --name-only origin/main...HEAD)
4136
42- - name : Get page links for changed MDX files
43- id : page-links
44- env :
45- FERN_TOKEN : ${{ secrets.FERN_TOKEN }}
46- run : |
47- PREVIEW_URL="${{ steps.generate-docs.outputs.preview_url }}"
48- CHANGED_FILES=$(git diff --name-only origin/main...HEAD -- '${{ matrix.project }}/**/*.mdx' 2>/dev/null || echo "")
49-
50- if [ -z "$CHANGED_FILES" ] || [ -z "$PREVIEW_URL" ]; then
51- echo "page_links=" >> $GITHUB_OUTPUT; exit 0
52- fi
53-
54- BASE_URL=$(echo "$PREVIEW_URL" | grep -oP 'https?://[^/]+')
55-
56- FILES_PARAM=$(echo "$CHANGED_FILES" | tr '\n' ',' | sed 's/,$//')
57- RESPONSE=$(curl -sf -H "FERN_TOKEN: $FERN_TOKEN" "${PREVIEW_URL}/api/fern-docs/get-slug-for-file?files=${FILES_PARAM}" 2>/dev/null) || {
58- echo "page_links=" >> $GITHUB_OUTPUT; exit 0
59- }
60-
61- PAGE_LINKS=$(echo "$RESPONSE" | jq -r --arg url "$BASE_URL" \
62- '.mappings[] | select(.slug != null) | "- [\(.slug)](\($url)/\(.slug))"')
63-
64- if [ -n "$PAGE_LINKS" ]; then
65- { echo "page_links<<EOF"; echo "$PAGE_LINKS"; echo "EOF"; } >> $GITHUB_OUTPUT
66- else
67- echo "page_links=" >> $GITHUB_OUTPUT
68- fi
37+ : > comment.md
38+ HAS_CONTENT=false
6939
70- - name : Create comment content
71- run : |
72- echo ":herb: **Preview your docs (${{ matrix.project }}):** <${{ steps.generate-docs.outputs.preview_url }}>" > comment.md
40+ for project in $PROJECTS; do
41+ PROJECT_CHANGES=$(echo "$CHANGED_FILES" | grep "^${project}/" || true)
42+ if [ -z "$PROJECT_CHANGES" ]; then
43+ continue
44+ fi
7345
74- if [ -n "${{ steps.page-links.outputs.page_links }}" ]; then
46+ SAFE_PROJECT=$(echo "$project" | tr '/' '-')
47+
48+ pushd "$project" > /dev/null
49+ OUTPUT=$(fern generate --docs --preview --id "pr${PR_NUMBER}-${SAFE_PROJECT}" 2>&1) || true
50+ popd > /dev/null
51+ echo "$OUTPUT"
52+ URL=$(echo "$OUTPUT" | grep -oP 'Published docs to \K.*(?= \()')
53+
54+ if [ -z "$URL" ]; then
55+ continue
56+ fi
57+
58+ BASE_URL=$(echo "$URL" | grep -oP 'https?://[^/]+')
59+
60+ MDX_CHANGES=$(echo "$PROJECT_CHANGES" | grep '\.mdx$' | sed "s|^${project}/||" || true)
61+ PAGE_LINKS=""
62+ if [ -n "$MDX_CHANGES" ]; then
63+ FILES_PARAM=$(echo "$MDX_CHANGES" | tr '\n' ',' | sed 's/,$//')
64+ RESPONSE=$(curl -sf -H "FERN_TOKEN: $FERN_TOKEN" "${URL}/api/fern-docs/get-slug-for-file?files=${FILES_PARAM}" 2>/dev/null) || true
65+ if [ -n "$RESPONSE" ]; then
66+ PAGE_LINKS=$(echo "$RESPONSE" | jq -r --arg url "$BASE_URL" \
67+ '.mappings[] | select(.slug != null) | "- [\(.slug)](\($url)/\(.slug))"' 2>/dev/null || true)
68+ fi
69+ fi
70+
71+ echo ":herb: **Preview \`${project}\`:** <${URL}>" >> comment.md
72+ if [ -n "$PAGE_LINKS" ]; then
73+ echo "" >> comment.md
74+ echo "Here are the markdown pages you've updated:" >> comment.md
75+ echo "$PAGE_LINKS" >> comment.md
76+ fi
7577 echo "" >> comment.md
76- echo "Here are the markdown pages you've updated:" >> comment.md
77- echo "${{ steps.page-links.outputs.page_links }}" >> comment.md
78+ HAS_CONTENT=true
79+ done
80+
81+ if [ "$HAS_CONTENT" = false ]; then
82+ echo ":herb: No docs changes detected in this PR." > comment.md
7883 fi
7984
8085 - name : Post PR comment
81- uses : thollander/actions-comment-pull-request@v2.4.3
86+ uses : thollander/actions-comment-pull-request@v3
8287 with :
83- filePath : comment.md
84- comment_tag : preview-docs-${{ matrix.project }}
88+ file-path : comment.md
89+ comment-tag : preview-docs
8590 mode : upsert
0 commit comments