github: modify github action to have only one comment on previews #154
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: | | |
| 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}) | Previous preview: [${PREVIEW_URL}](${PREVIEW_URL})" | |
| fi | |
| BODY="### Docs Preview | |
| - Status: ${STATUS} | |
| - ${LINK} | |
| **Changed pages:**" | |
| # Simple approach - no fancy loops | |
| echo "${{ steps.changes.outputs.files }}" | while read -r f; do | |
| if [ -n "$f" ]; then | |
| REL_PATH="${f#docs/}" | |
| NAME=$(basename "${REL_PATH%.*}") | |
| URL="${PREVIEW_URL}/${REL_PATH%.*}.html" | |
| BODY="${BODY} | |
| - [${NAME}](${URL})" | |
| fi | |
| done | |
| echo "body<<EOF" >> $GITHUB_OUTPUT | |
| echo "$BODY" >> $GITHUB_OUTPUT | |
| echo "EOF" >> $GITHUB_OUTPUT |