|
| 1 | +name: Preview Build |
| 2 | + |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + types: [created] |
| 6 | + workflow_dispatch: |
| 7 | + inputs: |
| 8 | + pr_number: |
| 9 | + description: 'PR number to build' |
| 10 | + required: true |
| 11 | + type: number |
| 12 | + |
| 13 | +permissions: |
| 14 | + contents: write |
| 15 | + pull-requests: write |
| 16 | + issues: write |
| 17 | + |
| 18 | +concurrency: |
| 19 | + group: preview-build-pr-${{ github.event.issue.number || inputs.pr_number }} |
| 20 | + cancel-in-progress: true |
| 21 | + |
| 22 | +jobs: |
| 23 | + trigger: |
| 24 | + name: Trigger Preview Build |
| 25 | + runs-on: ubuntu-latest |
| 26 | + if: | |
| 27 | + github.event_name == 'workflow_dispatch' || |
| 28 | + (github.event.issue.pull_request && contains(github.event.comment.body, '/build-preview')) |
| 29 | + outputs: |
| 30 | + sha: ${{ steps.pr.outputs.sha }} |
| 31 | + short_sha: ${{ steps.pr.outputs.short_sha }} |
| 32 | + release_tag: ${{ steps.pr.outputs.release_tag }} |
| 33 | + pr_number: ${{ steps.pr_number.outputs.number }} |
| 34 | + |
| 35 | + steps: |
| 36 | + - name: Set PR number |
| 37 | + id: pr_number |
| 38 | + run: | |
| 39 | + if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then |
| 40 | + echo "number=${{ inputs.pr_number }}" >> $GITHUB_OUTPUT |
| 41 | + else |
| 42 | + echo "number=${{ github.event.issue.number }}" >> $GITHUB_OUTPUT |
| 43 | + fi |
| 44 | +
|
| 45 | + - name: Check permissions |
| 46 | + if: github.event_name == 'issue_comment' |
| 47 | + uses: actions/github-script@v7 |
| 48 | + with: |
| 49 | + script: | |
| 50 | + const { data: permission } = await github.rest.repos.getCollaboratorPermissionLevel({ |
| 51 | + owner: context.repo.owner, |
| 52 | + repo: context.repo.repo, |
| 53 | + username: context.payload.comment.user.login |
| 54 | + }); |
| 55 | +
|
| 56 | + if (!['admin', 'write'].includes(permission.permission)) { |
| 57 | + await github.rest.reactions.createForIssueComment({ |
| 58 | + owner: context.repo.owner, |
| 59 | + repo: context.repo.repo, |
| 60 | + comment_id: context.payload.comment.id, |
| 61 | + content: '-1' |
| 62 | + }); |
| 63 | + core.setFailed('❌ Only collaborators with write access can trigger preview builds'); |
| 64 | + } |
| 65 | +
|
| 66 | + - name: Get PR information |
| 67 | + id: pr |
| 68 | + uses: actions/github-script@v7 |
| 69 | + with: |
| 70 | + script: | |
| 71 | + const { data: pr } = await github.rest.pulls.get({ |
| 72 | + owner: context.repo.owner, |
| 73 | + repo: context.repo.repo, |
| 74 | + pull_number: ${{ steps.pr_number.outputs.number }} |
| 75 | + }); |
| 76 | +
|
| 77 | + const shortSha = pr.head.sha.substring(0, 8); |
| 78 | + const releaseTag = `autobuild-preview-pr-${{ steps.pr_number.outputs.number }}-${shortSha}`; |
| 79 | +
|
| 80 | + core.setOutput('sha', pr.head.sha); |
| 81 | + core.setOutput('short_sha', shortSha); |
| 82 | + core.setOutput('release_tag', releaseTag); |
| 83 | +
|
| 84 | + - name: React to comment |
| 85 | + if: github.event_name == 'issue_comment' |
| 86 | + uses: actions/github-script@v7 |
| 87 | + with: |
| 88 | + script: | |
| 89 | + await github.rest.reactions.createForIssueComment({ |
| 90 | + owner: context.repo.owner, |
| 91 | + repo: context.repo.repo, |
| 92 | + comment_id: context.payload.comment.id, |
| 93 | + content: 'eyes' |
| 94 | + }); |
| 95 | +
|
| 96 | + - name: Post starting message |
| 97 | + if: github.event_name == 'issue_comment' |
| 98 | + uses: actions/github-script@v7 |
| 99 | + with: |
| 100 | + script: | |
| 101 | + const body = "🔄 Preview build started\n\n" + |
| 102 | + "**Release tag**: `${{ steps.pr.outputs.release_tag }}`\n" + |
| 103 | + "**Workflow**: [View progress](" + context.serverUrl + "/" + context.repo.owner + "/" + context.repo.repo + "/actions/runs/" + context.runId + ")"; |
| 104 | +
|
| 105 | + await github.rest.issues.createComment({ |
| 106 | + owner: context.repo.owner, |
| 107 | + repo: context.repo.repo, |
| 108 | + issue_number: ${{ steps.pr_number.outputs.number }}, |
| 109 | + body: body |
| 110 | + }); |
| 111 | +
|
| 112 | + build: |
| 113 | + name: Build |
| 114 | + needs: trigger |
| 115 | + permissions: |
| 116 | + contents: write |
| 117 | + uses: ./.github/workflows/build-reusable.yml |
| 118 | + with: |
| 119 | + build_ref: ${{ needs.trigger.outputs.sha }} |
| 120 | + release_tag: ${{ needs.trigger.outputs.release_tag }} |
| 121 | + is_prerelease: true |
| 122 | + llvm_version: '19' |
| 123 | + |
| 124 | + notify: |
| 125 | + name: Notify Completion |
| 126 | + needs: [trigger, build] |
| 127 | + runs-on: ubuntu-latest |
| 128 | + if: always() |
| 129 | + permissions: |
| 130 | + issues: write |
| 131 | + pull-requests: write |
| 132 | + steps: |
| 133 | + - name: Post success comment |
| 134 | + if: needs.build.result == 'success' |
| 135 | + uses: actions/github-script@v7 |
| 136 | + with: |
| 137 | + script: | |
| 138 | + const releaseTag = '${{ needs.trigger.outputs.release_tag }}'; |
| 139 | + const body = "✅ Preview build completed\n\n" + |
| 140 | + "**Release**: [" + releaseTag + "](https://github.com/" + context.repo.owner + "/" + context.repo.repo + "/releases/tag/" + releaseTag + ")"; |
| 141 | +
|
| 142 | + await github.rest.issues.createComment({ |
| 143 | + owner: context.repo.owner, |
| 144 | + repo: context.repo.repo, |
| 145 | + issue_number: ${{ needs.trigger.outputs.pr_number }}, |
| 146 | + body: body |
| 147 | + }); |
| 148 | +
|
| 149 | + - name: React to failure |
| 150 | + if: needs.build.result == 'failure' && github.event_name == 'issue_comment' |
| 151 | + uses: actions/github-script@v7 |
| 152 | + with: |
| 153 | + script: | |
| 154 | + await github.rest.reactions.createForIssueComment({ |
| 155 | + owner: context.repo.owner, |
| 156 | + repo: context.repo.repo, |
| 157 | + comment_id: context.payload.comment.id, |
| 158 | + content: '-1' |
| 159 | + }); |
| 160 | +
|
| 161 | + - name: Post failure comment |
| 162 | + if: needs.build.result == 'failure' |
| 163 | + uses: actions/github-script@v7 |
| 164 | + with: |
| 165 | + script: | |
| 166 | + const body = "❌ Preview build failed\n\n" + |
| 167 | + "Check the [workflow run](" + context.serverUrl + "/" + context.repo.owner + "/" + context.repo.repo + "/actions/runs/" + context.runId + ") for details."; |
| 168 | +
|
| 169 | + await github.rest.issues.createComment({ |
| 170 | + owner: context.repo.owner, |
| 171 | + repo: context.repo.repo, |
| 172 | + issue_number: ${{ needs.trigger.outputs.pr_number }}, |
| 173 | + body: body |
| 174 | + }); |
| 175 | +
|
| 176 | + - name: Post cancellation comment |
| 177 | + if: needs.build.result == 'cancelled' |
| 178 | + uses: actions/github-script@v7 |
| 179 | + with: |
| 180 | + script: | |
| 181 | + const body = "⚠️ Preview build was cancelled\n\n" + |
| 182 | + "This may have been caused by a newer build being triggered for the same PR."; |
| 183 | +
|
| 184 | + await github.rest.issues.createComment({ |
| 185 | + owner: context.repo.owner, |
| 186 | + repo: context.repo.repo, |
| 187 | + issue_number: ${{ needs.trigger.outputs.pr_number }}, |
| 188 | + body: body |
| 189 | + }); |
0 commit comments