From bd62be93d9f8eac96464b1d425c05025af4832b5 Mon Sep 17 00:00:00 2001 From: Sarthak kundra <52133994+sarthakkundra@users.noreply.github.com> Date: Thu, 26 Nov 2020 22:12:05 +0530 Subject: [PATCH] feat(v2): Added Lighthouse CI to PR checks (#3761) * added lighthouse CI removed wait for Netlify action corrected Netlify deploy urls corrected sitename corrected sitename * removed bootstrap and blog-only urls * upload results to public storage * added lighthousesrc.json * corrected configPath * corrected robots-txt assertion * corrected syntax for lighthousesrc * tried different audit * corrected all audits for lighthouse * added is-crawlable audit to skip * added lighthouse stats commit to action * Fixed Lighthouse yml syntax * Fixed lighthouse yml syntax(2) * trying comment * trying yaml syntax * test logging * trying to see a log * logging.... * corrected audit step id * Corrected token * changed comment number from issue to PR * trying different message * corrected message * added offline-start-url to skip audits * added start-url to skip fields * added offline-starts-url --- .github/workflows/lighthouseCI.yml | 65 ++++++++++++++++++++++++++++ .github/workflows/lighthousesrc.json | 9 ++++ 2 files changed, 74 insertions(+) create mode 100644 .github/workflows/lighthouseCI.yml create mode 100644 .github/workflows/lighthousesrc.json diff --git a/.github/workflows/lighthouseCI.yml b/.github/workflows/lighthouseCI.yml new file mode 100644 index 0000000000..5a906a4105 --- /dev/null +++ b/.github/workflows/lighthouseCI.yml @@ -0,0 +1,65 @@ +name: Lighthouse CI +on: pull_request +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Use Node.js 12.x + uses: actions/setup-node@v1 + with: + node-version: 12.x + - name: Install and Build + run: | + yarn install + yarn build + - name: Wait for the Netlify Preview + uses: jakepartusch/wait-for-netlify-action@v1 + id: netlify + with: + site_name: 'docusaurus-2' + - name: Audit URLs using Lighthouse + id: lighthouse_audit + uses: treosh/lighthouse-ci-action@v3 + with: + urls: | + https://deploy-preview-$PR_NUMBER--docusaurus-2.netlify.app/classic/ + configPath: ./.github/workflows/lighthousesrc.json + uploadArtifacts: true + temporaryPublicStorage: true + env: + PR_NUMBER: ${{ github.event.pull_request.number}} + - name: Format lighthouse score + id: format_lighthouse_score + uses: actions/github-script@v3 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const result = ${{ steps.lighthouse_audit.outputs.manifest }}[0].summary + const links = ${{ steps.lighthouse_audit.outputs.links }} + const formatResult = (res) => Math.round((res * 100)) + Object.keys(result).forEach(key => result[key] = formatResult(result[key])) + const score = res => res >= 90 ? '🟢' : res >= 50 ? '🟠' : '🔴' + const comment = [ + `⚡️ [Lighthouse report](${Object.values(links)[0]}) for the changes in this PR:`, + '| Category | Score |', + '| --- | --- |', + `| ${score(result.performance)} Performance | ${result.performance} |`, + `| ${score(result.accessibility)} Accessibility | ${result.accessibility} |`, + `| ${score(result['best-practices'])} Best practices | ${result['best-practices']} |`, + `| ${score(result.seo)} SEO | ${result.seo} |`, + `| ${score(result.pwa)} PWA | ${result.pwa} |`, + ' ', + `*Lighthouse ran on [${Object.keys(links)[0]}](${Object.keys(links)[0]})*` + ].join('\n') + core.setOutput("comment", comment); + + - name: Add Lighthouse stats as comment + id: comment_to_pr + uses: marocchino/sticky-pull-request-comment@v2.0.0 + with: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + number: ${{ github.event.pull_request.number }} + header: lighthouse + message: ${{ steps.format_lighthouse_score.outputs.comment }} diff --git a/.github/workflows/lighthousesrc.json b/.github/workflows/lighthousesrc.json new file mode 100644 index 0000000000..1510b0dd71 --- /dev/null +++ b/.github/workflows/lighthousesrc.json @@ -0,0 +1,9 @@ +{ + "ci": { + "collect": { + "settings": { + "skipAudits": ["robots-txt", "canonical", "tap-targets", "is-crawlable", "works-offline", "offline-start-url"] + } + } + } +}