github: modify github action to have only one comment on previews #151
Workflow file for this run
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: Build Docs Preview | |
| permissions: | |
| pull-requests: write | |
| on: | |
| pull_request: | |
| jobs: | |
| build: | |
| name: Build Docs Preview | |
| runs-on: ubuntu-latest | |
| env: | |
| ALGOLIA_APPLICATION_ID: ${{ secrets.ALGOLIA_APPLICATION_ID }} | |
| ALGOLIA_INDEX_NAME: ${{ secrets.ALGOLIA_INDEX_NAME }} | |
| ALGOLIA_SEARCH_API_KEY: ${{ secrets.ALGOLIA_SEARCH_API_KEY }} | |
| NODE_OPTIONS: --max_old_space_size=8192 | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - uses: pnpm/action-setup@v4 | |
| - name: Initial preview comment | |
| uses: peter-evans/create-or-update-comment@v4 | |
| with: | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| issue-number: ${{ github.event.pull_request.number }} | |
| identifier: docs-preview | |
| body: | | |
| ### Docs Preview | |
| - Status: ⏳ Creating preview… | |
| - name: Install dependencies | |
| run: pnpm install | |
| - name: Identify changed docs | |
| id: changes | |
| run: | | |
| git fetch origin ${{ github.event.pull_request.base.ref }} | |
| CHANGED=$(git diff --name-only origin/${{ github.event.pull_request.base.ref }} ${{ github.sha }} \ | |
| | grep -E '^docs/.*\.(md)$' || true) | |
| echo "files<<EOF" >> $GITHUB_OUTPUT | |
| echo "$CHANGED" >> $GITHUB_OUTPUT | |
| echo "EOF" >> $GITHUB_OUTPUT | |
| - name: Build site | |
| id: build | |
| run: | | |
| pnpm run build-prod | |
| continue-on-error: true | |
| - name: Generate comment body | |
| id: comment | |
| run: | | |
| # compute branch‑based preview URL | |
| BRANCH="${{ github.event.pull_request.head.ref }}" | |
| PREVIEW_URL="https://${BRANCH}.documentation-21k.pages.dev" | |
| LOG_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" | |
| if [ "${{ steps.build.outcome }}" = "success" ]; then | |
| STATUS="✅ Build succeeded" | |
| LINK="Preview: [${PREVIEW_URL}](${PREVIEW_URL})" | |
| else | |
| STATUS="❌ Build failed" | |
| LINK="Logs: [View logs](${LOG_URL})" | |
| # always link to last branch preview if available | |
| LINK="${LINK} | Previous preview: [${PREVIEW_URL}](${PREVIEW_URL})" | |
| fi | |
| BODY="### Docs Preview | |
| - Status: ${STATUS} | |
| - ${LINK} | |
| **Changed pages:**" | |
| # Process changed files using a more reliable approach | |
| if [ -n "${{ steps.changes.outputs.files }}" ]; then | |
| echo "${{ steps.changes.outputs.files }}" | while IFS= read -r f; do | |
| [ -z "$f" ] && continue | |
| REL_PATH="${f#docs/}" | |
| NAME=$(basename "${REL_PATH%.*}") | |
| URL="${PREVIEW_URL}/${REL_PATH%.*}.html" | |
| echo "- [${NAME}](${URL})" | |
| done > /tmp/changed_files_list.txt | |
| # Read the processed content and append to BODY | |
| if [ -f /tmp/changed_files_list.txt ]; then | |
| CHANGED_PAGES=$(cat /tmp/changed_files_list.txt) | |
| BODY="${BODY} | |
| ${CHANGED_PAGES}" | |
| fi | |
| fi | |
| echo "body<<EOF" >> $GITHUB_OUTPUT | |
| echo "$BODY" >> $GITHUB_OUTPUT | |
| echo "EOF" >> $GITHUB_OUTPUT | |
| - name: Update preview comment | |
| uses: peter-evans/create-or-update-comment@v4 | |
| with: | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| issue-number: ${{ github.event.pull_request.number }} | |
| identifier: docs-preview | |
| body: ${{ steps.comment.outputs.body }} |