Commit 0fe138ee authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

feat: add hook scripts support

parent acdddfe2
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -76,6 +76,13 @@ If you're not using your own deployment job, you may:
2. use any programmatic solution of your choice with a JavaScript-based [configuration file](https://github.com/GoogleChrome/lighthouse-ci/blob/main/docs/configuration.md#configuration-file).
3. explicitly set the `collect.url` option in the `LHCI_RUN_OPTS` variable (hardcoded to a single server),

### Hook scripts

The Lighthouse template supports _optional_ **hook scripts** from your project, located in the root directory to perform additional project-specific logic:

* `pre-lighthouse.sh` is executed **before** running Lighthouse,
* `post-lighthouse.sh` is executed **after** running Lighthouse (whichever the tests status).

## Examples

### Analysing multiple urls
+23 −1
Original line number Diff line number Diff line
@@ -232,6 +232,21 @@ stages:
    log_info "... done"
  }

  function maybe_exec_hook() {
    if [[ -f "$1" ]]
    then
      log_info "\\e[33;1m$1\\e[0m hook found: execute"
      if [[ ! -x "$1" ]] && ! chmod +x "$1"
      then
        log_warn "... could not make \\e[33;1m${1}\\e[0m executable: please do it (chmod +x)"
        # fallback technique
        sh "$1"
      else
        "$1"
      fi
    fi
  }

  function awkenvsubst() {
    # escapes '&' char in variables for gsub
    awk '{while(match($0,"[$%]{[^}]*}")) {var=substr($0,RSTART+2,RLENGTH-3);val=ENVIRON[var];gsub("&","\\\\&",val);gsub("[$%]{"var"}",val)}}1'
@@ -259,9 +274,16 @@ lighthouse:
    - install_ca_certs "${CUSTOM_CA_CERTS:-$DEFAULT_CA_CERTS}"
    - npm install -g @lhci/cli@$LHCI_VERSION
  script:
    # maybe execute pre hook
    - maybe_exec_hook "./pre-lighthouse.sh"
    # run tests
    # late variables expansion (to expand propagated $environment_url)
    - lhci_run_args=$(echo "$LHCI_RUN_OPTS" | awkenvsubst)
    - lhci autorun $lhci_run_args
    - lhci autorun $lhci_run_args || rc=$?
    # maybe execute post hook
    - maybe_exec_hook "./post-lighthouse.sh"
    # exit with return code
    - exit $rc
  artifacts:
    name: "$CI_JOB_NAME artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
    when: always