chore(project): use github workflow for website preview (#3387)

* chore(project): use github workflow for website preview

* chore: update PULL_REQUEST_NUMBER

* fix: typo

* chore: add pull_request for test

* fix: json format

* fix: add robot test

* fix: update request

* chore: update var

* chore: update var & remove test

* chore: improve workflow

* chore: upgrade workflow
This commit is contained in:
Aex
2021-09-14 19:37:14 +08:00
committed by GitHub
parent 29545d1ea2
commit a2be1973ea
3 changed files with 130 additions and 64 deletions

50
.github/workflows/preview-build.yml vendored Normal file
View File

@@ -0,0 +1,50 @@
name: Website Preview
on: pull_request
jobs:
build:
name: Build
runs-on: ubuntu-latest
env:
PULL_REQUEST_NUMBER: ${{ github.event.number }}
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup node
uses: actions/setup-node@v2
- name: Cache
uses: actions/cache@v2
with:
path: node_modules
key: node_modules-${{ hashFiles('**/yarn.lock') }}
- name: Install dependencies
run: yarn bootstrap
- name: Build website
run: yarn website-build
# share website dist
- name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: website
path: website-dist/
retention-days: 1
# write pr.txt for share
- name: Save pr number
if: ${{ always() }}
run: echo ${PULL_REQUEST_NUMBER} > ./pr.txt
# share pr number
- name: Upload pr number
if: ${{ always() }}
uses: actions/upload-artifact@v2
with:
name: pr
path: ./pr.txt
retention-days: 1

80
.github/workflows/preview-deploy.yml vendored Normal file
View File

@@ -0,0 +1,80 @@
# This workflow runs on target, so there is no need to worry about secrets
name: Website Preview Deploy
on:
workflow_run:
workflows: ['Website Preview']
types: [completed]
jobs:
# Build successfully, start deployment
on-success:
name: Build successfully
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- name: Download pr number
uses: dawidd6/action-download-artifact@v2
with:
workflow: ${{ github.event.workflow_run.workflow_id }}
name: pr
- name: Output pr number
id: pr
run: echo "::set-output name=id::$(<pr.txt)"
- name: Download artifact
uses: dawidd6/action-download-artifact@v2
with:
workflow: ${{ github.event.workflow_run.workflow_id }}
workflow_conclusion: success
name: website
- name: Deploy Site
run: |
export DEPLOY_DOMAIN=https://preview-${PULL_REQUEST_NUMBER}-element-plus.surge.sh
echo "Deploy to $DEPLOY_DOMAIN"
npx surge --project ./ --domain $DEPLOY_DOMAIN --token $SURGE_TOKEN
env:
SURGE_TOKEN: ${{ secrets.SURGE_TOKEN }}
PULL_REQUEST_NUMBER: ${{ steps.pr.outputs.id }}
- name: Deploy has succeeded
if: ${{ success() }}
run: |
export DEPLOY_DOMAIN=https://preview-${PULL_REQUEST_NUMBER}-element-plus.surge.sh
curl -X POST ${BOT_URL} -H 'Content-Type:application/json' --data-raw '{"botAction":"Preview", "pr":'${PULL_REQUEST_NUMBER}', "url":"'${DEPLOY_DOMAIN}'"}'
env:
BOT_URL: ${{ secrets.BOT_URL }}
PULL_REQUEST_NUMBER: ${{ steps.pr.outputs.id }}
- name: Deploy has failed
if: ${{ failure() }}
run: |
curl -X POST ${BOT_URL} -H 'Content-Type:application/json' --data-raw '{"status":"error", "botAction":"Preview", "pr":'${PULL_REQUEST_NUMBER}'}'
env:
BOT_URL: ${{ secrets.BOT_URL }}
PULL_REQUEST_NUMBER: ${{ steps.pr.outputs.id }}
# Build failed, update comment
on-failure:
name: Build failed
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
steps:
- name: Download pr number
uses: dawidd6/action-download-artifact@v2
with:
workflow: ${{ github.event.workflow_run.workflow_id }}
name: pr
- name: Output pr number
id: pr
run: echo "::set-output name=id::$(<pr.txt)"
- name: Update comment
run: |
curl -X POST ${BOT_URL} -H 'Content-Type:application/json' --data-raw '{"status":"error", "botAction":"Preview", "pr":'${PULL_REQUEST_NUMBER}'}'
env:
BOT_URL: ${{ secrets.BOT_URL }}
PULL_REQUEST_NUMBER: ${{ steps.pr.outputs.id }}