2023-10-17 14:05:00 +08:00
|
|
|
# Each PR will build preview site that help to check code is work as expect.
|
|
|
|
|
|
|
|
name: Preview Deploy
|
|
|
|
|
|
|
|
on:
|
|
|
|
workflow_run:
|
2024-09-19 03:30:19 +08:00
|
|
|
workflows: [Preview Build]
|
2023-10-17 14:05:00 +08:00
|
|
|
types:
|
|
|
|
- completed
|
|
|
|
|
|
|
|
permissions:
|
|
|
|
contents: read
|
|
|
|
|
|
|
|
jobs:
|
2023-10-20 18:03:58 +08:00
|
|
|
upstream-workflow-summary:
|
|
|
|
name: upstream workflow summary
|
|
|
|
runs-on: ubuntu-latest
|
|
|
|
if: github.event.workflow_run.event == 'pull_request'
|
|
|
|
outputs:
|
|
|
|
jobs: ${{ steps.prep-summary.outputs.result }}
|
|
|
|
build-success: ${{ steps.prep-summary.outputs.build-success }}
|
|
|
|
build-failure: ${{ steps.prep-summary.outputs.build-failure }}
|
|
|
|
steps:
|
|
|
|
- name: summary jobs status
|
2024-02-01 19:05:45 +08:00
|
|
|
uses: actions/github-script@v7
|
2023-10-20 18:03:58 +08:00
|
|
|
id: prep-summary
|
|
|
|
with:
|
|
|
|
script: |
|
|
|
|
const response = await github.rest.actions.listJobsForWorkflowRun({
|
|
|
|
owner: context.repo.owner,
|
|
|
|
repo: context.repo.repo,
|
|
|
|
run_id: ${{ github.event.workflow_run.id }},
|
|
|
|
});
|
|
|
|
|
|
|
|
// { [name]: [conclusion] }, e.g. { 'build preview': 'success' }
|
|
|
|
const jobs = (response.data?.jobs ?? []).reduce((acc, job) => {
|
|
|
|
if(job?.status === 'completed' && 'name' in job && 'conclusion' in job) {
|
|
|
|
acc[job.name] = job.conclusion;
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
const total = Object.keys(jobs).length;
|
|
|
|
if(total === 0) core.setFailed('no jobs found');
|
|
|
|
|
|
|
|
// the name here must be the same as `jobs.xxx.{name}` in preview-build.yml
|
|
|
|
// set output
|
|
|
|
core.setOutput('build-success', jobs['build preview'] === 'success');
|
|
|
|
core.setOutput('build-failure', jobs['build preview'] === 'failure');
|
|
|
|
return jobs;
|
|
|
|
|
|
|
|
deploy-preview:
|
2023-10-17 14:05:00 +08:00
|
|
|
name: deploy preview
|
2023-10-20 18:03:58 +08:00
|
|
|
permissions:
|
|
|
|
actions: read # for dawidd6/action-download-artifact to query and download artifacts
|
|
|
|
issues: write # for actions-cool/maintain-one-comment to modify or create issue comments
|
|
|
|
pull-requests: write # for actions-cool/maintain-one-comment to modify or create PR comments
|
2023-10-17 14:05:00 +08:00
|
|
|
runs-on: ubuntu-latest
|
2023-10-20 18:03:58 +08:00
|
|
|
needs: upstream-workflow-summary
|
|
|
|
if: github.event.workflow_run.event == 'pull_request'
|
2023-10-17 14:05:00 +08:00
|
|
|
steps:
|
|
|
|
# We need get PR id first
|
|
|
|
- name: download pr artifact
|
2024-06-12 20:09:16 +08:00
|
|
|
uses: dawidd6/action-download-artifact@v6
|
2023-10-17 14:05:00 +08:00
|
|
|
with:
|
|
|
|
workflow: ${{ github.event.workflow_run.workflow_id }}
|
|
|
|
run_id: ${{ github.event.workflow_run.id }}
|
|
|
|
name: pr
|
|
|
|
|
|
|
|
# Save PR id to output
|
|
|
|
- name: save PR id
|
|
|
|
id: pr
|
2024-07-22 10:45:43 +08:00
|
|
|
run: |
|
|
|
|
pr_id=$(<pr-id.txt)
|
|
|
|
if ! [[ "$pr_id" =~ ^[0-9]+$ ]]; then
|
|
|
|
echo "Error: pr-id.txt does not contain a valid numeric PR id. Please check."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
echo "id=$pr_id" >> $GITHUB_OUTPUT
|
2023-10-17 14:05:00 +08:00
|
|
|
|
|
|
|
# Download site artifact
|
|
|
|
- name: download site artifact
|
2023-11-03 18:27:01 +08:00
|
|
|
if: ${{ fromJSON(needs.upstream-workflow-summary.outputs.build-success) }}
|
2024-06-12 20:09:16 +08:00
|
|
|
uses: dawidd6/action-download-artifact@v6
|
2023-10-17 14:05:00 +08:00
|
|
|
with:
|
|
|
|
workflow: ${{ github.event.workflow_run.workflow_id }}
|
|
|
|
run_id: ${{ github.event.workflow_run.id }}
|
|
|
|
name: site
|
|
|
|
|
|
|
|
- name: upload surge service
|
|
|
|
id: deploy
|
2023-10-20 18:03:58 +08:00
|
|
|
continue-on-error: true
|
2024-07-22 10:45:43 +08:00
|
|
|
env:
|
|
|
|
PR_ID: ${{ steps.pr.outputs.id }}
|
2023-10-17 14:05:00 +08:00
|
|
|
run: |
|
2024-07-22 10:45:43 +08:00
|
|
|
export DEPLOY_DOMAIN=https://preview-${PR_ID}-ant-design.surge.sh
|
2024-08-12 14:18:27 +08:00
|
|
|
npx surge --project ./ --domain $DEPLOY_DOMAIN --token ${{ secrets.SURGE_TOKEN }}
|
2023-10-17 14:05:00 +08:00
|
|
|
|
2023-10-20 18:03:58 +08:00
|
|
|
- name: success comment
|
2023-10-17 14:05:00 +08:00
|
|
|
uses: actions-cool/maintain-one-comment@v3
|
2023-10-20 18:03:58 +08:00
|
|
|
if: ${{ steps.deploy.outcome == 'success' }}
|
2023-10-17 14:05:00 +08:00
|
|
|
with:
|
|
|
|
token: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
body: |
|
2024-02-06 16:14:13 +08:00
|
|
|
[<img width="300" alt="Preview is ready" src="https://user-images.githubusercontent.com/5378891/72400743-23dbb200-3785-11ea-9d13-1a2d92743846.png">](https://preview-${{ steps.pr.outputs.id }}-ant-design.surge.sh)
|
2023-10-17 14:05:00 +08:00
|
|
|
<!-- AUTO_PREVIEW_HOOK -->
|
2024-09-19 03:30:19 +08:00
|
|
|
body-include: <!-- AUTO_PREVIEW_HOOK -->
|
2023-10-17 14:05:00 +08:00
|
|
|
number: ${{ steps.pr.outputs.id }}
|
|
|
|
|
2023-10-20 18:03:58 +08:00
|
|
|
- name: failed comment
|
|
|
|
if: ${{ fromJSON(needs.upstream-workflow-summary.outputs.build-failure) || steps.deploy.outcome == 'failure' || failure() }}
|
2023-10-17 14:05:00 +08:00
|
|
|
uses: actions-cool/maintain-one-comment@v3
|
|
|
|
with:
|
|
|
|
token: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
body: |
|
2024-02-06 16:14:13 +08:00
|
|
|
[<img width="300" alt="Preview failed" src="https://user-images.githubusercontent.com/5378891/75333447-1e63a280-58c1-11ea-975d-235367fd1522.png">](https://preview-${{ steps.pr.outputs.id }}-ant-design.surge.sh)
|
2023-10-17 14:05:00 +08:00
|
|
|
<!-- AUTO_PREVIEW_HOOK -->
|
2024-09-19 03:30:19 +08:00
|
|
|
body-include: <!-- AUTO_PREVIEW_HOOK -->
|
2023-10-17 14:05:00 +08:00
|
|
|
number: ${{ steps.pr.outputs.id }}
|
2024-08-11 15:29:37 +08:00
|
|
|
|
|
|
|
- name: Check surge deploy result and exit if failed
|
|
|
|
run: |
|
|
|
|
if [ "${{ steps.deploy.outcome }}" != "success" ]; then
|
2024-08-11 17:35:59 +08:00
|
|
|
echo "Surge Deploy failed."
|
2024-08-11 15:29:37 +08:00
|
|
|
exit 1
|
|
|
|
fi
|