Commit 0633afc6 authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

feat: add hook scripts support

parent cacec6f7
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -70,3 +70,10 @@ environments) will automatically be propagated to your Cypress tests.

If you're not using a smart deployment job, you may still explicitly declare the `CYPRESS_BASE_URL` variable (but that 
will be unfortunately hardcoded to a single server).

### Hook scripts

The Cypress template supports _optional_ **hook scripts** from your project, located in the `$CYPRESS_PROJECT_DIR` directory to perform additional project-specific logic:

* `pre-cypress.sh` is executed **before** running Cypress,
* `post-cypress.sh` is executed **after** running Cypress (whichever the tests status).
+22 −1
Original line number Diff line number Diff line
@@ -230,6 +230,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
  }

  # maybe install NPM dependencies if package.json file found
  function maybe_install_deps() {
    if [[ -f "package-lock.json" ]]
@@ -286,10 +301,16 @@ cypress:
    - cd "$CYPRESS_PROJECT_DIR"
    - maybe_install_deps
  script:
    # maybe execute pre hook
    - maybe_exec_hook "./pre-cypress.sh"
    # maybe activate DEBUG traces
    - 'if [[ "$TRACE" ]]; then export DEBUG=cypress:*; fi'
    # run Cypress tests
    - cypress run --reporter junit --reporter-options "mochaFile=reports/cypress-[hash].xunit.xml" $CYPRESS_EXTRA_ARGS
    - cypress run --reporter junit --reporter-options "mochaFile=reports/cypress-[hash].xunit.xml" $CYPRESS_EXTRA_ARGS || rc=$?
    # maybe execute post hook
    - maybe_exec_hook "./post-cypress.sh"
    # exit with return code
    - exit $rc
  artifacts:
    name: "$CI_JOB_NAME artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
    when: always