Skip to content

Instantly share code, notes, and snippets.

@alfredlucero
Created August 2, 2020 02:57
Show Gist options
  • Save alfredlucero/09727b0c66b5424194ccea1e698f961f to your computer and use it in GitHub Desktop.
Save alfredlucero/09727b0c66b5424194ccea1e698f961f to your computer and use it in GitHub Desktop.
Cypress Tips/Tricks - Buildkite Parse Selected Cypress Specs and Trigger Cypress Pipeline to Run
#!/bin/bash
set -e
# Get where the script is currently running from
DIRNAME=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# Get spec selection values; its value will be the Cypress spec's relative path i.e. cypress/integration/SenderAuthentication/**/*
RUNALL=$(buildkite-agent meta-data get "runAll")
RUNALERTS=$(buildkite-agent meta-data get "runAlerts")
RUNMAILSETTINGS=$(buildkite-agent meta-data get "runMailSettings")
RUNSENDERAUTHENTICATION=$(buildkite-agent meta-data get "runSenderAuthentication")
# ...More Cypress Yes/No answers...
SPECVALUES=(
"${RUNALL}"
"${RUNALERTS}"
"${RUNMAILSETTINGS}"
"${RUNSENDERAUTHENTICATION}"
# ...More RUN*...
)
# Filter all selected specs that do not equal "no"
SELECTEDSPECS=()
for i in "${SPECVALUES[@]}"; do [[ $i != "no" ]] && SELECTEDSPECS+=("$i"); done
# Skip if no specs selected
if [[ ${#SELECTEDSPECS[@]} = 0 ]]; then
echo "Skipping Cypress specs for the ${CYPRESS_TEST_ENV} environment!"
exit 0
fi
# Form comma-separated specs string i.e. cypress/integration/SenderAuthentication/**/*,cypress/integration/MailSettings/*
FORMATTEDSELECTEDSPECS=$(
IFS=$','
echo "${SELECTEDSPECS[*]}"
)
# Trigger Cypress pipeline to run
# The exported variables such as CYPRESS_SPECS, CYPRESS_TEST_ENV, PARALLELISM, ASYNC will go to triggerCypress.yml
# and eventually set up the environment variables in pipeline.cypress.yml
export CYPRESS_SPECS="$FORMATTEDSELECTEDSPECS"
export CYPRESS_TEST_ENV="$CYPRESS_TEST_ENV"
export PARALLELISM=$PARALLELISM
if [[ "$CYPRESS_TEST_ENV" = "testing" ]]; then
# Do not block after testing/feature branch deploys based on triggered testing Cypress tests
export ASYNC=true
else
# Block deploys to production based on triggered staging Cypress tests
export ASYNC=false
fi
buildkite-agent pipeline upload "$DIRNAME"/triggerCypress.yml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment