ci: fix preview workflow — single comment with changed page links #27
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 | |
| jobs: | |
| generate-preview: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| project: | |
| - 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 | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: ${{ github.event.pull_request.head.sha }} | |
| - name: Setup Fern CLI | |
| uses: fern-api/setup-fern-cli@v1 | |
| - name: Generate preview URL | |
| id: generate-docs | |
| working-directory: ${{ matrix.project }} | |
| env: | |
| FERN_TOKEN: ${{ secrets.FERN_TOKEN }} | |
| HEAD_REF: ${{ github.head_ref }} | |
| run: | | |
| OUTPUT=$(fern generate --docs --preview --id "${HEAD_REF}-${{ matrix.project }}" 2>&1) || true | |
| echo "$OUTPUT" | |
| URL=$(echo "$OUTPUT" | grep -oP 'Published docs to \K.*(?= \()') | |
| echo "preview_url=$URL" >> $GITHUB_OUTPUT | |
| - name: Save preview URL | |
| id: artifact-meta | |
| run: | | |
| SAFE_NAME=$(echo "${{ matrix.project }}" | tr '/' '-') | |
| echo "safe_name=${SAFE_NAME}" >> $GITHUB_OUTPUT | |
| echo "${{ matrix.project }}=${{ steps.generate-docs.outputs.preview_url }}" > "${SAFE_NAME}.txt" | |
| - name: Upload preview URL | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: preview-url-${{ steps.artifact-meta.outputs.safe_name }} | |
| path: "${{ steps.artifact-meta.outputs.safe_name }}.txt" | |
| retention-days: 1 | |
| comment: | |
| needs: generate-preview | |
| if: ${{ !cancelled() }} | |
| runs-on: ubuntu-latest | |
| permissions: | |
| pull-requests: write | |
| steps: | |
| - name: Download all preview URLs | |
| uses: actions/download-artifact@v4 | |
| with: | |
| pattern: preview-url-* | |
| merge-multiple: true | |
| - name: Build comment | |
| run: | | |
| { | |
| echo "## :herb: Docs Previews" | |
| echo "" | |
| echo "| Example | Preview |" | |
| echo "| --- | --- |" | |
| } > comment.md | |
| for project in 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; do | |
| SAFE_NAME=$(echo "$project" | tr '/' '-') | |
| FILE="${SAFE_NAME}.txt" | |
| if [ -f "$FILE" ]; then | |
| URL=$(cut -d'=' -f2- "$FILE") | |
| if [ -n "$URL" ]; then | |
| echo "| \`${project}\` | [Preview](${URL}) |" >> comment.md | |
| else | |
| echo "| \`${project}\` | :warning: Failed |" >> comment.md | |
| fi | |
| else | |
| echo "| \`${project}\` | :warning: Failed |" >> comment.md | |
| fi | |
| done | |
| - name: Post PR comment | |
| uses: thollander/actions-comment-pull-request@v2.4.3 | |
| with: | |
| filePath: comment.md | |
| comment_tag: preview-docs | |
| mode: upsert |