|
| 1 | +name: Notify when docs preview is ready |
| 2 | + |
| 3 | +on: |
| 4 | + # Trigger after the docs build workflow completes |
| 5 | + workflow_run: |
| 6 | + workflows: ["Build tutorials"] |
| 7 | + types: |
| 8 | + - completed |
| 9 | + |
| 10 | + # Manual trigger for testing |
| 11 | + workflow_dispatch: |
| 12 | + inputs: |
| 13 | + pr_number: |
| 14 | + description: 'PR number to test (e.g., 3664)' |
| 15 | + required: true |
| 16 | + type: string |
| 17 | + skip_comment: |
| 18 | + description: 'Skip posting comment (dry run)' |
| 19 | + required: false |
| 20 | + type: boolean |
| 21 | + default: true |
| 22 | + |
| 23 | +permissions: |
| 24 | + pull-requests: write |
| 25 | + issues: write |
| 26 | + |
| 27 | +jobs: |
| 28 | + # Main job - triggered by workflow_run |
| 29 | + notify-preview-ready: |
| 30 | + # Only run for pull requests and successful builds |
| 31 | + if: > |
| 32 | + github.event_name == 'workflow_run' && |
| 33 | + github.event.workflow_run.event == 'pull_request' && |
| 34 | + github.event.workflow_run.conclusion == 'success' |
| 35 | + runs-on: ubuntu-latest |
| 36 | + |
| 37 | + steps: |
| 38 | + - name: Get PR number |
| 39 | + id: get-pr |
| 40 | + env: |
| 41 | + GH_TOKEN: ${{ github.token }} |
| 42 | + run: | |
| 43 | + # Get the PR number from the workflow run |
| 44 | + PR_NUMBER=$(gh api \ |
| 45 | + "/repos/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}" \ |
| 46 | + --jq '.pull_requests[0].number') |
| 47 | +
|
| 48 | + if [ -z "$PR_NUMBER" ] || [ "$PR_NUMBER" = "null" ]; then |
| 49 | + echo "No PR found for this workflow run" |
| 50 | + exit 0 |
| 51 | + fi |
| 52 | +
|
| 53 | + echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT |
| 54 | + echo "Found PR #$PR_NUMBER" |
| 55 | +
|
| 56 | + - name: Wait and check if docs preview is ready |
| 57 | + id: check-preview |
| 58 | + if: steps.get-pr.outputs.pr_number != '' |
| 59 | + env: |
| 60 | + PR_NUMBER: ${{ steps.get-pr.outputs.pr_number }} |
| 61 | + run: | |
| 62 | + # Construct the preview URL for tutorials |
| 63 | + PREVIEW_URL="https://docs-preview.pytorch.org/pytorch/tutorials/${PR_NUMBER}/index.html" |
| 64 | +
|
| 65 | + echo "Checking preview URL: $PREVIEW_URL" |
| 66 | +
|
| 67 | + # Wait up to 40 minutes for the preview to be ready |
| 68 | + MAX_ATTEMPTS=80 |
| 69 | + SLEEP_TIME=30 |
| 70 | + attempt=0 |
| 71 | +
|
| 72 | + while [ $attempt -lt $MAX_ATTEMPTS ]; do |
| 73 | + echo "Attempt $((attempt + 1))/$MAX_ATTEMPTS: Checking if preview is ready..." |
| 74 | +
|
| 75 | + # Check if the URL returns 200 OK |
| 76 | + HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" "$PREVIEW_URL" || echo "000") |
| 77 | +
|
| 78 | + if [ "$HTTP_CODE" = "200" ]; then |
| 79 | + echo "✅ Preview is ready! HTTP $HTTP_CODE" |
| 80 | + echo "preview_ready=true" >> $GITHUB_OUTPUT |
| 81 | + echo "preview_url=$PREVIEW_URL" >> $GITHUB_OUTPUT |
| 82 | + exit 0 |
| 83 | + else |
| 84 | + echo "⏳ Preview not ready yet (HTTP $HTTP_CODE). Waiting $SLEEP_TIME seconds..." |
| 85 | + sleep $SLEEP_TIME |
| 86 | + attempt=$((attempt + 1)) |
| 87 | + fi |
| 88 | + done |
| 89 | +
|
| 90 | + echo "❌ Preview did not become ready within the timeout period" |
| 91 | + echo "preview_ready=false" >> $GITHUB_OUTPUT |
| 92 | +
|
| 93 | + - name: Comment on PR with preview link |
| 94 | + if: steps.check-preview.outputs.preview_ready == 'true' |
| 95 | + uses: actions/github-script@v7 |
| 96 | + with: |
| 97 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 98 | + script: | |
| 99 | + const prNumber = ${{ steps.get-pr.outputs.pr_number }}; |
| 100 | + const previewUrl = '${{ steps.check-preview.outputs.preview_url }}'; |
| 101 | +
|
| 102 | + // Check if we already commented |
| 103 | + const { data: comments } = await github.rest.issues.listComments({ |
| 104 | + owner: context.repo.owner, |
| 105 | + repo: context.repo.repo, |
| 106 | + issue_number: prNumber, |
| 107 | + }); |
| 108 | +
|
| 109 | + const botComment = comments.find(comment => |
| 110 | + comment.user.login === 'github-actions[bot]' && |
| 111 | + comment.body.includes('📄 Documentation preview is ready') |
| 112 | + ); |
| 113 | +
|
| 114 | + // Build comment body |
| 115 | + let commentBody = `## 📄 Documentation preview is ready! 🎉\n\n`; |
| 116 | + commentBody += `Your tutorial changes have been built and are ready for review:\n\n`; |
| 117 | + commentBody += `👉 **[View Preview](${previewUrl})**\n`; |
| 118 | + commentBody += `\n---\n`; |
| 119 | + commentBody += `<sub>This preview will be available for 14 days.</sub>`; |
| 120 | +
|
| 121 | + if (botComment) { |
| 122 | + // Update existing comment |
| 123 | + await github.rest.issues.updateComment({ |
| 124 | + owner: context.repo.owner, |
| 125 | + repo: context.repo.repo, |
| 126 | + comment_id: botComment.id, |
| 127 | + body: commentBody |
| 128 | + }); |
| 129 | + console.log('Updated existing comment'); |
| 130 | + } else { |
| 131 | + // Create new comment |
| 132 | + await github.rest.issues.createComment({ |
| 133 | + owner: context.repo.owner, |
| 134 | + repo: context.repo.repo, |
| 135 | + issue_number: prNumber, |
| 136 | + body: commentBody |
| 137 | + }); |
| 138 | + console.log('Created new comment'); |
| 139 | + } |
| 140 | +
|
| 141 | + - name: Comment if preview failed |
| 142 | + if: steps.get-pr.outputs.pr_number != '' && steps.check-preview.outputs.preview_ready == 'false' |
| 143 | + uses: actions/github-script@v7 |
| 144 | + with: |
| 145 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 146 | + script: | |
| 147 | + const prNumber = ${{ steps.get-pr.outputs.pr_number }}; |
| 148 | +
|
| 149 | + let commentBody = `## ⚠️ Documentation preview timeout\n\n`; |
| 150 | + commentBody += `The documentation preview did not become available within the expected time.\n`; |
| 151 | + commentBody += `This could be due to:\n`; |
| 152 | + commentBody += `- Longer than usual build times\n`; |
| 153 | + commentBody += `- Build failures\n`; |
| 154 | + commentBody += `- Infrastructure issues\n\n`; |
| 155 | + commentBody += `Please check the [workflow runs](https://github.com/${context.repo.owner}/${context.repo.repo}/actions) for more details.`; |
| 156 | +
|
| 157 | + await github.rest.issues.createComment({ |
| 158 | + owner: context.repo.owner, |
| 159 | + repo: context.repo.repo, |
| 160 | + issue_number: prNumber, |
| 161 | + body: commentBody |
| 162 | + }); |
| 163 | + console.log('Posted timeout notification'); |
| 164 | +
|
| 165 | + # Manual test job - triggered by workflow_dispatch |
| 166 | + test-preview-notification: |
| 167 | + if: github.event_name == 'workflow_dispatch' |
| 168 | + runs-on: ubuntu-latest |
| 169 | + |
| 170 | + steps: |
| 171 | + - name: Test preview URL |
| 172 | + id: check-preview |
| 173 | + env: |
| 174 | + PR_NUMBER: ${{ inputs.pr_number }} |
| 175 | + run: | |
| 176 | + echo "🧪 Running manual test for PR #$PR_NUMBER" |
| 177 | +
|
| 178 | + # Construct the preview URL for tutorials |
| 179 | + PREVIEW_URL="https://docs-preview.pytorch.org/pytorch/tutorials/${PR_NUMBER}/index.html" |
| 180 | +
|
| 181 | + echo "Checking preview URL: $PREVIEW_URL" |
| 182 | +
|
| 183 | + # Check if the URL returns 200 OK |
| 184 | + HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" "$PREVIEW_URL" || echo "000") |
| 185 | +
|
| 186 | + echo "HTTP Status: $HTTP_CODE" |
| 187 | +
|
| 188 | + if [ "$HTTP_CODE" = "200" ]; then |
| 189 | + echo "✅ Preview is ready!" |
| 190 | + echo "preview_ready=true" >> $GITHUB_OUTPUT |
| 191 | + echo "preview_url=$PREVIEW_URL" >> $GITHUB_OUTPUT |
| 192 | + else |
| 193 | + echo "❌ Preview not available (HTTP $HTTP_CODE)" |
| 194 | + echo "preview_ready=false" >> $GITHUB_OUTPUT |
| 195 | + fi |
| 196 | +
|
| 197 | + - name: Display results |
| 198 | + run: | |
| 199 | + echo "================================" |
| 200 | + echo "Test Results for PR #${{ inputs.pr_number }}" |
| 201 | + echo "================================" |
| 202 | + echo "Preview Ready: ${{ steps.check-preview.outputs.preview_ready }}" |
| 203 | + echo "Preview URL: ${{ steps.check-preview.outputs.preview_url }}" |
| 204 | + echo "" |
| 205 | + if [ "${{ inputs.skip_comment }}" = "true" ]; then |
| 206 | + echo "ℹ️ Dry run mode - no comment will be posted" |
| 207 | + fi |
| 208 | +
|
| 209 | + - name: Post test comment (if not dry run) |
| 210 | + if: steps.check-preview.outputs.preview_ready == 'true' && inputs.skip_comment == false |
| 211 | + uses: actions/github-script@v7 |
| 212 | + with: |
| 213 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 214 | + script: | |
| 215 | + const prNumber = ${{ inputs.pr_number }}; |
| 216 | + const previewUrl = '${{ steps.check-preview.outputs.preview_url }}'; |
| 217 | +
|
| 218 | + // Build comment body |
| 219 | + let commentBody = `## 📄 Documentation preview is ready! 🎉\n\n`; |
| 220 | + commentBody += `Your tutorial changes have been built and are ready for review:\n\n`; |
| 221 | + commentBody += `👉 **[View Preview](${previewUrl})**\n`; |
| 222 | + commentBody += `\n---\n`; |
| 223 | + commentBody += `<sub>This preview will be available for 14 days.</sub>`; |
| 224 | +
|
| 225 | + await github.rest.issues.createComment({ |
| 226 | + owner: context.repo.owner, |
| 227 | + repo: context.repo.repo, |
| 228 | + issue_number: prNumber, |
| 229 | + body: commentBody |
| 230 | + }); |
| 231 | + console.log('Posted test comment'); |
0 commit comments