Skip to content
Open
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
47 changes: 47 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
on: [pull_request]

jobs:
run_psi:
runs-on: ubuntu-latest
name: Running Page Speed Insights
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Running Page Speed Insights
uses: ./ # Uses an action in the root directory
id: psi
with:
url: "https://jake.partus.ch"
threshold: 85
strategy: mobile
activity_type: pull_request
- name: Outputting the performance results of a given URL
uses: mshick/add-pr-comment@v1
with:
score: |
**PSI Score: **
`${{ steps.psi.outputs.score }}`

**[CoreWebVitals] FCP Weight: **
`${{ steps.psi.outputs.fcp }}`

**[CoreWebVitals] TTI Weight: **
`${{ steps.psi.outputs.tti }}`

**[CoreWebVitals] SI Weight: **
`${{ steps.psi.outputs.si }}`

**[CoreWebVitals] TBT Weight: **
`${{ steps.psi.outputs.tbt }}`

**[CoreWebVitals] LCP Weight: **
`${{ steps.psi.outputs.lcp }}`

**[CoreWebVitals] CLS Weight: **
`${{ steps.psi.outputs.cls }}`

**[CoreWebVitals] FMP Weight: **
`${{ steps.psi.outputs.fmp }}`
repo-token: ${{ secrets.GITHUB_TOKEN }}
repo-token-user-login: 'github-actions[bot]'
allow-repeats: false
19 changes: 19 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ inputs:
description: "Strategy to use when analyzing the page (mobile/desktop)."
key:
description: "A PageSpeed Insights API key"
activity_type:
description: "The activity type to trigger workflows that run on this event"
outputs:
score:
description: "Performance score of a given URL"
fcp:
description: "FCP weight of a given URL"
tti:
description: "TTI weight of a given URL"
si:
description: "SI weight of a given URL"
tbt:
description: "TBT weight of a given URL"
lcp:
description: "LCP weight of a given URL"
cls:
description: "CLS weight of a given URL"
fmp:
description: "FMP weight of a given URL"
runs:
using: "node12"
main: "index.js"
32 changes: 29 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const core = require("@actions/core");
const psi = require("psi");

const coreWebVitalsTypes = ["FCP", "TTI", "SI", "TBT", "LCP", "CLS", "FMP"];

const run = async () => {
try {
const url = core.getInput("url");
Expand All @@ -13,18 +15,42 @@ const run = async () => {

const threshold = Number(core.getInput("threshold")) || 70;
const strategy = core.getInput("strategy") || "mobile";
// Output a formatted report to the terminal
console.log(`Running Page Speed Insights for ${url}`);
await psi.output(url, {

const params = {
...(key ? {key} : undefined),
...(key ? undefined : {nokey: "true"}),
strategy,
format: "cli",
threshold
});
};

// Output a formatted report to the terminal
await psi.output(url, params);

if (activity_type === "pull_request") {
// Output a simple report to the pull request comment section
const output = await psi(url, params);
const performance = output.data.lighthouseResult.categories.performance;
core.setOutput("score", JSON.stringify(performance.score));
const coreWebVitals = getCoreWebVitals(performance.auditRefs);
setCoreWebVitals(coreWebVitals);
}
} catch (error) {
core.setFailed(error.message);
}
};

const getCoreWebVitals = (refs) => {
return refs.reduce((acc, ref, _) => {
if (coreWebVitalsTypes.includes(ref.acronym))
acc[ref.acronym] = ref.weight;
}, {});
}

const setCoreWebVitals = (coreWebVitals) => {
for (const type of coreWebVitalsTypes)
core.setOutput(type.toLowerCase(), JSON.stringify(coreWebVitals[type]));
}

run();