github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/util/deploy.sh (about)

     1  #!/bin/bash
     2  
     3  # Helper script for using a standardized version flag when deploying.
     4  
     5  REPO_DIR="$(git rev-parse --show-toplevel)"
     6  source "${REPO_DIR}/util/logging.sh"
     7  WPTD_PATH=${WPTD_PATH:-"${REPO_DIR}"}
     8  
     9  usage() {
    10    USAGE="Usage: deploy.sh [-p] [-r] [-q] [-b] [-h] [app path]
    11      -p : Promote (i.e. pass --promote flag to deploy)
    12      -r : Release (use the git-hash as the version)
    13      -q : Quiet (no user prompts, debugging off)
    14      -b : Branch name - defaults to current Git branch
    15      -h : Show (this) help information
    16      app path: wpt.fyi relative path for the app, e.g. \"webapp\""
    17    echo "${USAGE}"
    18  }
    19  
    20  # Take the last argument.
    21  APP_PATH=${@: -1}
    22  # Trim the trailing slash (if any).
    23  APP_PATH=${APP_PATH%/}
    24  
    25  while getopts ':b:prhq' flag; do
    26    case "${flag}" in
    27      r) RELEASE='true' ;;
    28      b) BRANCH_NAME="${OPTARG}" ;;
    29      p) PROMOTE='true' ;;
    30      q) QUIET='true' ;;
    31      :) echo "Option -$OPTARG requires an argument." && exit 1;;
    32      h|*) usage && exit 1;;
    33    esac
    34  done
    35  
    36  if [[ "${APP_PATH}" == ""  ]]; then fatal "app path not specified."; fi
    37  case "${APP_PATH}" in
    38    "webapp/web" | \
    39    "webapp/web/app.staging.yaml" | \
    40    "results-processor" | \
    41    "results-processor/app.staging.yaml" | \
    42    "api/query/cache/service" | \
    43    "api/query/cache/service/app.staging.yaml")
    44    ;;
    45  *)
    46    fatal "Unrecognized app path \"${APP_PATH}\"."
    47    ;;
    48  esac
    49  
    50  # Ensure dependencies are installed.
    51  if [[ -z "${QUIET}" ]]; then info "Installing dependencies..."; fi
    52  cd "${WPTD_PATH}"
    53  if [[ "${APP_PATH}" == "webapp" ]]; then
    54    make deployment_state || fatal "Error installing deps"
    55  fi
    56  
    57  format_branch_name() {
    58    local BRANCH="$1"
    59    # Normalize to lower-case, and replace all non-alphanumeric characters
    60    # with '-'.
    61    BRANCH="$(echo -n $BRANCH | tr [:upper:] [:lower:] | tr -c [:alnum:] -)"
    62  
    63    # Limit version names to 22 characters, to leave enough space for the HTTPS
    64    # domain name (which could have a very long suffix like
    65    # "-dot-searchcache-wptdashboard-staging"). Domain name parts can be no
    66    # longer than 63 characters in total.
    67    BRANCH="$(echo -n $BRANCH | cut -c 1-22)"
    68  
    69    # GCP requires that the branch name start and end in a letter or digit.
    70    while [[ "$BRANCH" == -* ]]; do
    71      BRANCH="${BRANCH:1}"
    72    done
    73    while [[ "$BRANCH" == *- ]]; do
    74      BRANCH="${BRANCH::-1}"
    75    done
    76  
    77    echo $BRANCH
    78  }
    79  
    80  # Create a name for this version
    81  VERSION_BRANCH_NAME="$(format_branch_name "${BRANCH_NAME:-"$(git rev-parse --abbrev-ref HEAD)"}")"
    82  USER="$(git remote -v get-url origin | sed -E 's#(https?:\/\/|git@)github.com(\/|:)##' | sed 's#/.*$##')-"
    83  if [[ "${USER}" == "web-platform-tests-" ]]; then USER=""; fi
    84  
    85  VERSION="${USER}${VERSION_BRANCH_NAME}"
    86  if [[ -n ${RELEASE} ]]
    87  then
    88    # Use SHA for releases.
    89    # Add a prefix to prevent gcloud from interpreting the version name as a number.
    90    VERSION="rev-$(git rev-parse --short HEAD)"
    91  fi
    92  
    93  PROMOTE_FLAG="--no-promote"
    94  if [[ -n ${PROMOTE} ]]
    95  then
    96    PROMOTE_FLAG="--promote"
    97    if [[ -z "${QUIET}" ]]; then debug "Producing production configuration..."; fi
    98    if [[ "${USER}" != "" ]]
    99    then
   100      if [[ -z "${QUIET}" ]]
   101      then
   102        confirm "Are you sure you want to be deploying a non-web-platform-tests repo (${USER})?"
   103        if [ "${?}" != "0" ]; then fatal "User cancelled the deploy"; fi
   104      fi
   105    fi
   106  fi
   107  
   108  if [[ -n "${QUIET}" ]]
   109  then
   110      QUIET_FLAG="-q"
   111  else
   112      QUIET_FLAG=""
   113  fi
   114  COMMAND="gcloud app deploy ${PROMOTE_FLAG} ${QUIET_FLAG} --version=${VERSION} ${APP_PATH}"
   115  
   116  if [[ -z "${QUIET}" ]]; then info "Executing deploy command:\n${COMMAND}"; fi
   117  
   118  set -e
   119  
   120  ${COMMAND} || fatal "Deploy returned non-zero exit code $?"
   121  
   122  exit 0