Skip to content

Commit a49c364

Browse files
authored
Create preview-docs.yml
1 parent d240140 commit a49c364

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

.github/workflows/preview-docs.yml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: Preview Docs
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, ready_for_review]
6+
branches:
7+
- main
8+
9+
jobs:
10+
run:
11+
runs-on: ubuntu-latest
12+
permissions:
13+
pull-requests: write
14+
contents: read
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@v4
18+
with:
19+
fetch-depth: 0
20+
21+
- name: Setup Fern CLI
22+
uses: fern-api/setup-fern-cli@v1
23+
24+
- name: Generate preview URL
25+
id: generate-docs
26+
env:
27+
FERN_TOKEN: ${{ secrets.FERN_TOKEN }}
28+
run: |
29+
OUTPUT=$(fern generate --docs --preview 2>&1) || true
30+
echo "$OUTPUT"
31+
URL=$(echo "$OUTPUT" | grep -oP 'Published docs to \K.*(?= \()')
32+
echo "preview_url=$URL" >> $GITHUB_OUTPUT
33+
echo "Preview URL: $URL"
34+
35+
- name: Get page links for changed MDX files
36+
id: page-links
37+
env:
38+
FERN_TOKEN: ${{ secrets.FERN_TOKEN }}
39+
run: |
40+
PREVIEW_URL="${{ steps.generate-docs.outputs.preview_url }}"
41+
CHANGED_FILES=$(git diff --name-only origin/main...HEAD -- '*.mdx' 2>/dev/null || echo "")
42+
43+
if [ -z "$CHANGED_FILES" ] || [ -z "$PREVIEW_URL" ]; then
44+
echo "page_links=" >> $GITHUB_OUTPUT; exit 0
45+
fi
46+
47+
BASE_URL=$(echo "$PREVIEW_URL" | grep -oP 'https?://[^/]+')
48+
49+
FILES_PARAM=$(echo "$CHANGED_FILES" | tr '\n' ',' | sed 's/,$//')
50+
RESPONSE=$(curl -sf -H "FERN_TOKEN: $FERN_TOKEN" "${PREVIEW_URL}/api/fern-docs/get-slug-for-file?files=${FILES_PARAM}" 2>/dev/null) || {
51+
echo "page_links=" >> $GITHUB_OUTPUT; exit 0
52+
}
53+
54+
PAGE_LINKS=$(echo "$RESPONSE" | jq -r --arg url "$BASE_URL" \
55+
'.mappings[] | select(.slug != null) | "- [\(.slug)](\($url)/\(.slug))"')
56+
57+
if [ -n "$PAGE_LINKS" ]; then
58+
{ echo "page_links<<EOF"; echo "$PAGE_LINKS"; echo "EOF"; } >> $GITHUB_OUTPUT
59+
else
60+
echo "page_links=" >> $GITHUB_OUTPUT
61+
fi
62+
63+
- name: Create comment content
64+
run: |
65+
echo ":herb: **Preview your docs:** <${{ steps.generate-docs.outputs.preview_url }}>" > comment.md
66+
67+
if [ -n "${{ steps.page-links.outputs.page_links }}" ]; then
68+
echo "" >> comment.md
69+
echo "Here are the markdown pages you've updated:" >> comment.md
70+
echo "${{ steps.page-links.outputs.page_links }}" >> comment.md
71+
fi
72+
73+
- name: Post PR comment
74+
uses: thollander/actions-comment-pull-request@v2.4.3
75+
with:
76+
filePath: comment.md
77+
comment_tag: preview-docs
78+
mode: upsert

0 commit comments

Comments
 (0)