chore: add PR preview workflow #1474
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Node CI | |
on: | |
pull_request: | |
types: [opened, reopened, synchronize, closed] | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.event.number }} | |
cancel-in-progress: true | |
jobs: | |
lint: | |
if: ${{ github.event.action != 'closed' }} | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
node-version: [18.x] | |
steps: | |
- name: Fetch commit count | |
env: | |
PR_COMMIT_COUNT: ${{ github.event.pull_request.commits }} | |
run: | | |
echo "FETCH_DEPTH=$(($PR_COMMIT_COUNT + 1))" >> $GITHUB_ENV | |
- uses: actions/checkout@v3 | |
with: | |
fetch-depth: ${{ env.FETCH_DEPTH }} | |
- name: Use Node.js ${{ matrix.node-version }} | |
uses: actions/setup-node@v3 | |
with: | |
node-version: ${{ matrix.node-version }} | |
- name: Cache node modules | |
id: cache-dep | |
uses: actions/cache@v3 | |
env: | |
cache-name: cache-node-modules | |
with: | |
path: node_modules | |
key: ${{ runner.os }}-lint-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} | |
- name: Install dependencies | |
if: steps.cache-dep.outputs.cache-hit != 'true' | |
run: npm ci | |
- name: Collect changed files | |
run: | | |
mkdir ~/tmp/ | |
git diff ${{ github.event.pull_request.base.sha }} ${{ github.sha }} --diff-filter=ACM --name-only --relative '*src/**/*.ts' > ~/tmp/changed_files | |
echo -e "Changed files: \n$(cat ~/tmp/changed_files)" | |
- name: Lint | |
run: npx eslint $(cat ~/tmp/changed_files) | |
- name: Check types | |
run: npm run checktype | |
build: | |
if: ${{ github.event.action != 'closed' }} | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
node-version: [18.x] | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Use Node.js ${{ matrix.node-version }} | |
uses: actions/setup-node@v3 | |
with: | |
node-version: ${{ matrix.node-version }} | |
- name: Cache node modules | |
id: cache-dep | |
uses: actions/cache@v3 | |
env: | |
cache-name: cache-node-modules | |
with: | |
path: node_modules | |
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} | |
- name: Install dependencies | |
if: steps.cache-dep.outputs.cache-hit != 'true' | |
run: npm ci | |
- name: Unit Test | |
run: npm run test | |
- name: Build release | |
run: npm run release | |
- name: Test generated DTS | |
run: npm run test:dts | |
- name: Pack npm tarball | |
id: pack-tarball | |
run: | | |
export NPM_PKG_DIR='.npm-pkg' | |
mkdir $NPM_PKG_DIR | |
npm pack -pack-destination $NPM_PKG_DIR | |
cd $NPM_PKG_DIR | |
find . -type f -regex ".*\.tgz" -exec tar xvzf {} \; | |
echo "PR_DIST_DIR=$NPM_PKG_DIR/package" >> $GITHUB_OUTPUT | |
echo "PR_DIST_CACHE_KEY=echarts-pr-dist-cache-${{ github.event.number }}@${{ github.event.pull_request.head.sha }}" >> $GITHUB_OUTPUT | |
- name: Cache PR dist files | |
uses: actions/cache@v3 | |
id: cache-pr-dist | |
with: | |
key: ${{ steps.pack-tarball.outputs.PR_DIST_CACHE_KEY }} | |
path: ${{ steps.pack-tarball.outputs.PR_DIST_DIR }} | |
outputs: | |
PR_DIST_CACHE_KEY: ${{ steps.pack-tarball.outputs.PR_DIST_CACHE_KEY }} | |
PR_DIST_DIR: ${{ steps.pack-tarball.outputs.PR_DIST_DIR }} | |
pr-preview: | |
runs-on: ubuntu-latest | |
if: ${{ github.repository_owner == 'apache' && needs.build.result == 'success' }} | |
needs: ['build'] | |
steps: | |
- name: Fetch cached PR dist files | |
uses: actions/cache@v3 | |
with: | |
key: ${{ needs.build.outputs.PR_DIST_CACHE_KEY }} | |
path: ${{ needs.build.outputs.PR_DIST_DIR }} | |
fail-on-cache-miss: true | |
- name: Deploy dist files | |
run: | | |
cd ${{ needs.build.outputs.PR_DIST_DIR }} | |
export SURGE_DOMAIN='https://echarts-pr-${{ github.event.number }}.surge.sh' | |
npx surge --project ./ --domain $SURGE_DOMAIN --token ${{ secrets.SURGE_TOKEN }} | |
- name: Install action dependencies | |
run: | | |
mkdir .actions | |
cd .actions | |
git clone --depth=1 https://github.com/actions-cool/maintain-one-comment.git | |
echo "GH_SHA_SHORT=$(echo ${{ github.event.pull_request.head.sha }} | cut -c 1-7)" >> $GITHUB_ENV | |
- name: Create comment for PR preview | |
uses: ./.actions/maintain-one-comment | |
with: | |
body: | | |
<!-- ECHARTS_PR_PREVIEW --> | |
The changes brought by this PR can be previewed at: https://echarts.apache.org/examples/editor?version=PR-${{ github.event.number }}@${{ env.GH_SHA_SHORT }} | |
body-include: '<!-- ECHARTS_PR_PREVIEW -->' | |
number: ${{ github.event.number }} | |
teardown-preview: | |
runs-on: ubuntu-latest | |
if: ${{ github.repository_owner == 'apache' && github.event.action == 'closed' && github.pull_request.merged != true }} | |
continue-on-error: true | |
steps: | |
- name: Install action dependencies | |
run: git clone --depth=1 https://github.com/actions-cool/maintain-one-comment.git | |
- name: Delete PR preview comment | |
uses: ./maintain-one-comment | |
with: | |
body-include: '<!-- ECHARTS_PR_PREVIEW -->' | |
delete: true | |
number: ${{ github.event.number }} | |
- name: Teardown closed PR preview | |
run: | | |
export SURGE_DOMAIN='https://echarts-pr-${{ github.event.number }}.surge.sh' | |
npx surge teardown $SURGE_DOMAIN --token ${{ secrets.SURGE_TOKEN }} |