Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix surge preview workflow #6764

Merged
merged 7 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 27 additions & 42 deletions .github/workflows/preview-build.yml
Original file line number Diff line number Diff line change
@@ -1,60 +1,45 @@
# Each PR will build preview site that help to check code is work as expect.

name: Preview Build

on:
pull_request:
types: [opened, synchronize, reopened]

# Cancel prev CI if new commit come
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

permissions:
contents: read

jobs:
build-preview:
build-site:
name: build preview
runs-on: ubuntu-latest

steps:
- name: Check
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}

- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: '16'

- name: Install pnpm
id: pnpm-install
uses: pnpm/action-setup@v4
- uses: actions/checkout@v4
afc163 marked this conversation as resolved.
Show resolved Hide resolved
- uses: actions/setup-node@v4
with:
version: 7
run_install: false

- name: Get pnpm store directory
id: pnpm-cache
run: |
echo "::set-output name=pnpm_cache_dir::$(pnpm store path)"

- name: Setup pnpm cache
uses: actions/cache@v4
with:
path: ${{ steps.pnpm-cache.outputs.pnpm_cache_dir }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-

- name: Install dependencies
run: pnpm install

- name: Run Build Script
run: |
pnpm build-doc

node-version: 16

- uses: oven-sh/setup-bun@v2
- run: bun install
- name: build site
id: site
run: bun run build-doc
env:
NODE_OPTIONS: --max_old_space_size=4096
afc163 marked this conversation as resolved.
Show resolved Hide resolved
- run: |
afc163 marked this conversation as resolved.
Show resolved Hide resolved
zip -r dist.zip dist

- name: Upload dist artifact
zip -r dist.zip dist
- name: upload site artifact
uses: actions/upload-artifact@v4
with:
name: dist
path: dist.zip
retention-days: 5

# Upload PR id for next workflow use
- name: Save PR number
if: ${{ always() }}
run: echo ${{ github.event.number }} > ./pr-id.txt
Expand Down
149 changes: 99 additions & 50 deletions .github/workflows/preview-deploy.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Each PR will build preview site that help to check code is work as expect.

name: Preview Deploy

on:
Expand All @@ -6,72 +8,119 @@ on:
types:
- completed

permissions:
contents: read

jobs:
success:
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
uses: actions/github-script@v7
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:
name: deploy preview
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
runs-on: ubuntu-latest
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
needs: upstream-workflow-summary
if: github.event.workflow_run.event == 'pull_request'
steps:
# We need get PR id first
- name: download pr artifact
uses: dawidd6/action-download-artifact@v4
uses: dawidd6/action-download-artifact@v6
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
run: echo "::set-output name=id::$(<pr-id.txt)"
- name: download dist artifact
uses: dawidd6/action-download-artifact@v4
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

# Download site artifact
- name: download site artifact
if: ${{ fromJSON(needs.upstream-workflow-summary.outputs.build-success) }}
uses: dawidd6/action-download-artifact@v6
with:
workflow: ${{ github.event.workflow_run.workflow_id }}
workflow_conclusion: success
run_id: ${{ github.event.workflow_run.id }}
name: dist
- run: |
unzip dist.zip
- name: upload to surge

- name: upload surge service
id: deploy
continue-on-error: true
env:
PR_ID: ${{ steps.pr.outputs.id }}
run: |
export DEPLOY_DOMAIN=https://antd-mobile-preview-pr-${{ steps.pr.outputs.id }}.surge.sh
npx surge --project ./dist --domain $DEPLOY_DOMAIN --token ${{ secrets.SURGE_TOKEN }}
- name: find comment
uses: peter-evans/find-comment@v2
id: fc
with:
issue-number: ${{ steps.pr.outputs.id }}
comment-author: 'github-actions[bot]'
body-includes: PR preview
- name: create comment
uses: peter-evans/create-or-update-comment@v2
with:
comment-id: ${{ steps.fc.outputs.comment-id }}
issue-number: ${{ steps.pr.outputs.id }}
edit-mode: replace
body: |
PR preview has been successfully built and deployed to https://antd-mobile-preview-pr-${{ steps.pr.outputs.id }}.surge.sh
- run: |
rm -rf dist/
- name: The job failed
if: ${{ failure() }}
uses: peter-evans/create-or-update-comment@v2
export DEPLOY_DOMAIN=https://preview-${PR_ID}-ant-design-mobile.surge.sh
npx surge --project ./ --domain $DEPLOY_DOMAIN --token ${{ secrets.SURGE_TOKEN }}

- name: success comment
uses: actions-cool/maintain-one-comment@v3
if: ${{ steps.deploy.outcome == 'success' }}
with:
issue-number: ${{ steps.pr.outputs.id }}
token: ${{ secrets.GITHUB_TOKEN }}
body: |
Deploy PR preview failed.
[<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-mobile.surge.sh)
<!-- AUTO_PREVIEW_HOOK -->
body-include: <!-- AUTO_PREVIEW_HOOK -->
number: ${{ steps.pr.outputs.id }}

failed:
runs-on: ubuntu-latest
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'failure'
steps:
- name: download pr artifact
uses: dawidd6/action-download-artifact@v4
- name: failed comment
if: ${{ fromJSON(needs.upstream-workflow-summary.outputs.build-failure) || steps.deploy.outcome == 'failure' || failure() }}
uses: actions-cool/maintain-one-comment@v3
with:
workflow: ${{ github.event.workflow_run.workflow_id }}
name: pr
- name: save PR id
id: pr
run: echo "::set-output name=id::$(<pr-id.txt)"
- name: The job failed
uses: peter-evans/create-or-update-comment@v2
with:
issue-number: ${{ steps.pr.outputs.id }}
token: ${{ secrets.GITHUB_TOKEN }}
body: |
Deploy PR preview failed.
[<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-mobile.surge.sh)
<!-- AUTO_PREVIEW_HOOK -->
body-include: <!-- AUTO_PREVIEW_HOOK -->
number: ${{ steps.pr.outputs.id }}

- name: Check surge deploy result and exit if failed
run: |
if [ "${{ steps.deploy.outcome }}" != "success" ]; then
echo "Surge Deploy failed."
exit 1
fi
31 changes: 31 additions & 0 deletions .github/workflows/preview-start.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# When `preview-build` start. Leave a message on the PR
#
# 🚨🚨🚨 Important 🚨🚨🚨
# Never do any `checkout` or `npm install` action!
# `pull_request_target` will enable PR to access the secrets!

name: Preview Start

on:
pull_request_target:
types: [opened, synchronize, reopened]

permissions:
contents: read

jobs:
preview-start:
permissions:
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
name: start preview info
runs-on: ubuntu-latest
steps:
- name: update status comment
uses: actions-cool/maintain-one-comment@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
body: |
[<img width="500" alt="Prepare preview" src="https://user-images.githubusercontent.com/5378891/72351368-2c979e00-371b-11ea-9652-eb4e825d745e.gif">](https://preview-${{ github.event.number }}-ant-design-mobile.surge.sh)
<!-- AUTO_PREVIEW_HOOK -->
body-include: <!-- AUTO_PREVIEW_HOOK -->
Loading