|
| 1 | +name: Deploy preview environment |
| 2 | +on: |
| 3 | + workflow_run: |
| 4 | + workflows: [CI] |
| 5 | + types: [completed] |
| 6 | + workflow_dispatch: |
| 7 | + inputs: |
| 8 | + pr: |
| 9 | + required: true |
| 10 | + type: string |
| 11 | + description: PR number |
| 12 | + pull_request_target: |
| 13 | + types: [labeled] |
| 14 | + |
| 15 | +permissions: |
| 16 | + contents: read |
| 17 | + pull-requests: write |
| 18 | + actions: read |
| 19 | + |
| 20 | +jobs: |
| 21 | + deploy: |
| 22 | + runs-on: ubuntu-latest |
| 23 | + steps: |
| 24 | + - name: Get PR/workflow run info |
| 25 | + id: get-info |
| 26 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
| 27 | + with: |
| 28 | + script: | |
| 29 | + console.dir(context, { depth: null }); |
| 30 | +
|
| 31 | + let pr; |
| 32 | + let workflowRun; |
| 33 | + let isLabel = false; |
| 34 | + switch (context.eventName) { |
| 35 | + case "workflow_dispatch": |
| 36 | + console.log("Workflow dispatch event"); |
| 37 | + pr = (await github.rest.pulls.get({ |
| 38 | + owner: context.repo.owner, |
| 39 | + repo: context.repo.repo, |
| 40 | + pull_number: +context.payload.inputs.pr, |
| 41 | + })).data; |
| 42 | + break; |
| 43 | + case "pull_request_target": |
| 44 | + console.log("Pull request target event"); |
| 45 | + pr = context.payload.pull_request; |
| 46 | + break; |
| 47 | + case "workflow_run": |
| 48 | + console.log("Workflow run event"); |
| 49 | + workflowRun = context.payload.workflow_run; |
| 50 | + pr = workflowRun.pull_requests[0]; |
| 51 | + if (pr) { |
| 52 | + console.log("PR found in workflow run"); |
| 53 | + // Reload the PR to get the labels. |
| 54 | + pr = (await github.rest.pulls.get({ |
| 55 | + owner: context.repo.owner, |
| 56 | + repo: context.repo.repo, |
| 57 | + pull_number: pr.number, |
| 58 | + })).data; |
| 59 | + } else { |
| 60 | + const q = `is:pr is:open repo:${context.repo.owner}/${context.repo.repo} sha:${workflowRun.head_sha}`; |
| 61 | + console.log(`PR not found in workflow run, searching for it with ${q}`); |
| 62 | + // PRs sent from forks do not get the pull_requests field set. |
| 63 | + // https://github.com/orgs/community/discussions/25220 |
| 64 | + const response = await github.rest.search.issuesAndPullRequests({ q }); |
| 65 | + if (response.data.items.length > 0) { |
| 66 | + pr = response.data.items[0]; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +
|
| 71 | + if (!pr) { |
| 72 | + console.log("Could not find PR"); |
| 73 | + return null; |
| 74 | + } |
| 75 | +
|
| 76 | + console.log(`Found PR ${pr.html_url}`); |
| 77 | + console.dir(pr, { depth: null }); |
| 78 | +
|
| 79 | + if (pr.state !== "open") { |
| 80 | + console.log(`PR ${pr.number} is not open`); |
| 81 | + return null; |
| 82 | + } |
| 83 | +
|
| 84 | + if (!pr.labels.some((label) => label.name === "deploy-preview")) { |
| 85 | + console.log(`PR ${pr.number} does not have the deploy-preview label`); |
| 86 | + return null; |
| 87 | + } |
| 88 | +
|
| 89 | + if (!workflowRun) { |
| 90 | + console.log(`No workflow run found in event, searching for it with ${pr.head.sha}`); |
| 91 | + try { |
| 92 | + workflowRun = (await github.rest.actions.listWorkflowRuns({ |
| 93 | + owner: context.repo.owner, |
| 94 | + repo: context.repo.repo, |
| 95 | + workflow_id: "CI.yml", |
| 96 | + head_sha: pr.head.sha, |
| 97 | + per_page: 1, |
| 98 | + })).data.workflow_runs[0]; |
| 99 | + } catch (e) { |
| 100 | + console.log(e); |
| 101 | + } |
| 102 | + } |
| 103 | +
|
| 104 | + if (!workflowRun) { |
| 105 | + console.log(`Could not find workflow run for PR ${pr.number}`); |
| 106 | + return null; |
| 107 | + } |
| 108 | +
|
| 109 | + console.log(`Found workflow run ${workflowRun.html_url}`); |
| 110 | + console.dir(workflowRun, { depth: null }); |
| 111 | +
|
| 112 | + try { |
| 113 | + const artifacts = await github.rest.actions.listWorkflowRunArtifacts({ |
| 114 | + owner: "microsoft", |
| 115 | + repo: "TypeScript-Website", |
| 116 | + run_id: workflowRun.id, |
| 117 | + }); |
| 118 | + console.dir(artifacts, { depth: null }); |
| 119 | + if (!artifacts.data.artifacts.some(x => x.name === "site")) { |
| 120 | + console.log("No artifact found in workflow run"); |
| 121 | + return null; |
| 122 | + } |
| 123 | + } catch (e) { |
| 124 | + console.log(e); |
| 125 | + return null; |
| 126 | + } |
| 127 | +
|
| 128 | + const result = { pr: pr.number, runId: workflowRun.id }; |
| 129 | + console.log(result); |
| 130 | + return result; |
| 131 | +
|
| 132 | + - name: Download site build from PR |
| 133 | + if: ${{ steps.get-info.outputs.result != 'null' }} |
| 134 | + uses: actions/download-artifact@95815c38cf2ff2164869cbab79da8d1f422bc89e # v4.2.1 |
| 135 | + with: |
| 136 | + name: site |
| 137 | + path: site |
| 138 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 139 | + run-id: ${{ fromJson(steps.get-info.outputs.result).runId }} |
| 140 | + |
| 141 | + - name: Deploy |
| 142 | + id: deploy |
| 143 | + if: ${{ steps.get-info.outputs.result != 'null' }} |
| 144 | + uses: Azure/static-web-apps-deploy@v1 |
| 145 | + with: |
| 146 | + azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_PREVIEW }} |
| 147 | + # Unsetting the repo token so we can control commenting ourselves. |
| 148 | + # repo_token: ${{ secrets.GITHUB_TOKEN }} |
| 149 | + action: "upload" |
| 150 | + app_location: "site" |
| 151 | + skip_app_build: true |
| 152 | + production_branch: v2 |
| 153 | + deployment_environment: ${{ fromJson(steps.get-info.outputs.result).pr }} |
| 154 | + |
| 155 | + - name: Comment on PR |
| 156 | + if: ${{ steps.get-info.outputs.result != 'null' }} |
| 157 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
| 158 | + env: |
| 159 | + PR_NUMBER: ${{ fromJson(steps.get-info.outputs.result).pr }} |
| 160 | + SITE_URL: ${{ steps.deploy.outputs.static_web_app_url }} |
| 161 | + with: |
| 162 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 163 | + script: | |
| 164 | + const prefix = "Azure Static Web Apps: Your stage site is ready!"; |
| 165 | + const comments = await github.paginate(github.rest.issues.listComments, { |
| 166 | + owner: context.repo.owner, |
| 167 | + repo: context.repo.repo, |
| 168 | + issue_number: +process.env.PR_NUMBER, |
| 169 | + per_page: 100, |
| 170 | + }); |
| 171 | +
|
| 172 | + for (const comment of comments) { |
| 173 | + if (comment.user?.login === "github-actions[bot]" && comment.body?.startsWith(prefix)) { |
| 174 | + console.log(`Deleting comment ${comment.id}`); |
| 175 | + await github.rest.issues.deleteComment({ |
| 176 | + owner: context.repo.owner, |
| 177 | + repo: context.repo.repo, |
| 178 | + comment_id: comment.id, |
| 179 | + }); |
| 180 | + } |
| 181 | + } |
| 182 | +
|
| 183 | + await github.rest.issues.createComment({ |
| 184 | + owner: context.repo.owner, |
| 185 | + repo: context.repo.repo, |
| 186 | + issue_number: +process.env.PR_NUMBER, |
| 187 | + body: `${prefix} Visit it here: ${process.env.SITE_URL}` |
| 188 | + }); |
0 commit comments