diff --git a/.github/workflows/lighthouse-ci.yml b/.github/workflows/lighthouse-ci.yml new file mode 100644 index 000000000..9b3ef5628 --- /dev/null +++ b/.github/workflows/lighthouse-ci.yml @@ -0,0 +1,98 @@ +name: CI - Lighthouse + +on: + pull_request: + branches: + - main + +jobs: + lhci: + runs-on: ubuntu-latest + env: + NODE_OPTIONS: --max-old-space-size=8192 + GATSBY_CONCURRENT_DOWNLOAD: 1 + + steps: + - name: Check for Lighthouse Trigger + id: trigger_check + run: | + if grep -q "Lighthouse Test" <<< "${{ github.event.pull_request.body }}"; then + echo "should_run=true" >> $GITHUB_OUTPUT + else + echo "should_run=false" >> $GITHUB_OUTPUT + fi + + + - name: Extract and build URLs from PR body paths + id: extract + if: steps.trigger_check.outputs.should_run == 'true' + shell: bash + env: + PR_BODY: ${{ github.event.pull_request.body || '' }} + run: | + echo "$PR_BODY" > pr_body.txt + + # A more robust way to read each path and build the ARGS string + ARGS="" + while read -r path; do + # The -n flag ensures we only process non-empty lines + if [ -n "$path" ]; then + clean_path=$(echo "$path" | sed 's/[),.;:]*$//') + full_url="http://localhost:9000$clean_path" + ARGS="$ARGS --collect.url='$full_url'" + fi + done < <(grep -Eo '^/[a-zA-Z0-9/_.-]*$' pr_body.txt || true) + + # This part remains the same + if [ -z "$ARGS" ]; then + echo "has_urls=false" >> $GITHUB_OUTPUT + echo "has_localhost=false" >> $GITHUB_OUTPUT + exit 0 + fi + + echo "ARGS=$ARGS" >> $GITHUB_ENV + echo "has_urls=true" >> $GITHUB_OUTPUT + echo "has_localhost=true" >> $GITHUB_OUTPUT + + - name: Checkout code + if: steps.trigger_check.outputs.should_run == 'true' && steps.extract.outputs.has_localhost == 'true' + uses: actions/checkout@v3 + with: + ref: ${{ github.event.pull_request.head.sha }} + + - name: Install dependencies & build + if: steps.trigger_check.outputs.should_run == 'true' && steps.extract.outputs.has_localhost == 'true' + run: | + rm -rf .cache public + npm ci + npm run build + + - name: Start static server + if: steps.trigger_check.outputs.should_run == 'true' && steps.extract.outputs.has_localhost == 'true' + shell: bash + run: | + npx http-server ./public -p 9000 --silent & + TIMEOUT=60 + COUNT=0 + until curl -sSf http://localhost:9000/ >/dev/null; do + sleep 1 + COUNT=$((COUNT+1)) + if [ $COUNT -ge $TIMEOUT ]; then + echo "Server did not start within timeout" + exit 1 + fi + done + echo "Local server ready" + + - name: Run Lighthouse CI for supplied URLs + if: steps.trigger_check.outputs.should_run == 'true' && steps.extract.outputs.has_urls == 'true' + env: + LHCI_GITHUB_APP_TOKEN: ${{ secrets.LHCI_GITHUB_APP_TOKEN }} + run: | + npm install -g @lhci/cli@0.15.x + if [ -z "${ARGS}" ]; then + echo "No URLs provided. Skipping LHCI." + exit 0 + fi + echo "Running LHCI autorun with URLs: $ARGS" + lhci autorun $ARGS \ No newline at end of file diff --git a/gatsby-config.js b/gatsby-config.js index c7b984b31..8016c486e 100644 --- a/gatsby-config.js +++ b/gatsby-config.js @@ -242,7 +242,7 @@ const plugins = [ { resolve: 'gatsby-plugin-canonical-urls', options: { - siteUrl: process.env.GATSBY_DEFAULT_SITE_URL, + siteUrl: process.env.GATSBY_DEFAULT_SITE_URL || 'https://cilium.io', }, }, { @@ -281,7 +281,7 @@ module.exports = { // pathPrefix: "", siteImage: '/images/social-preview.jpg', siteLanguage: 'en', - siteUrl: process.env.GATSBY_DEFAULT_SITE_URL, + siteUrl: process.env.GATSBY_DEFAULT_SITE_URL || 'https://cilium.io', /* author */ authorName: 'cilium', authorTwitterAccount: '@ciliumproject', diff --git a/lighthouserc.js b/lighthouserc.js new file mode 100644 index 000000000..991f147e1 --- /dev/null +++ b/lighthouserc.js @@ -0,0 +1,25 @@ +module.exports = { + ci: { + collect: { + staticDistDir: './public', + numberOfRuns: 1, + settings: { + emulatedFormFactor: 'Desktop', + throttlingMethod: 'simulate', + }, + }, + + assert: { + assertions: { + 'categories:performance': ['warn', { minScore: 0.7 }], + 'categories:accessibility': ['warn', { minScore: 0.9 }], + 'categories:best-practices': ['warn', { minScore: 0.9 }], + 'categories:seo': ['warn', { minScore: 0.9 }], + }, + }, + + upload: { + target: 'temporary-public-storage', + }, + }, +}; diff --git a/package.json b/package.json index a57c7e982..53db544e3 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,8 @@ "babel-loader": "^9.2.1", "classnames": "^2.5.1", "framer-motion": "^12.12.1", - "gatsby": "^5.14.3", + "gatsby": "^5.14.6", + "gatsby-adapter-netlify": "^1.2.1", "gatsby-plugin-algolia-search": "^0.5.7", "gatsby-plugin-canonical-urls": "^5.14.0", "gatsby-plugin-feed": "^5.14.0",