CircleCI-Public / gcp-cli-orb

Install and configure the Google Cloud CLI (gcloud)
https://circleci.com/orbs/registry/orb/circleci/gcp-cli
MIT License
9 stars 32 forks source link

feat: try to retry downloading and untarring of CLI zip file. #71

Closed kelvintaywl closed 1 year ago

kelvintaywl commented 1 year ago

Checklist

Motivation, issues

This tries to address the occasional error seen by customers (likely) due to corrupted zip files downloaded. Example: #70

Description

I tested on a sample project here and it's so far so good: https://app.circleci.com/pipelines/github/kelvintaywl-cci/cut-me-some-slack/6/workflows/f90bbc73-273c-4588-a228-d8aef6e1eb88/jobs/7/parallel-runs/24?filterBy=ALL

(done so by importing my modified version inline: https://github.com/kelvintaywl-cci/cut-me-some-slack/blob/64de1ba4c3e41cf551be3bfc5bbfaa1a226aba3e/.circleci/config.yml#L6-L358)

The 25 runs passed, though it is not saying the retry occurred. However, if anything, i think it shows there is no regression with the changes here 🙏

EricRibeiro commented 1 year ago

Thanks for the help, @kelvintaywl!

While this would work, there is space for improvement. Have a look at my suggestion below:

download_and_extract() {
  local output_file="$1"
  local url_path_fixture="$2"
  local version="$3"
  local install_directory="$4"

  curl --location --silent --fail --retry 3 --output "$output_file" "https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-$url_path_fixture-$version-linux-x86_64.tar.gz"
  tar -xzf "$output_file" -C "$install_directory"

  return $?
}

retry_download() {
  local download_tries=0
  local max_download_tries=2

  while [ $download_tries -le $max_download_tries ]; do
    download_and_extract "$1" "$2" "$3" "$4"
    if [ $? -eq 0 ]; then
      printf '%s\n' ". $4/google-cloud-sdk/path.bash.inc" >> "$5"
      break
    else
      download_tries=$((download_tries + 1))
      printf "Download failed, retrying... (attempt: %d)\n" "$download_tries"
    fi
  done

  if [ $download_tries -gt $max_download_tries ]; then
    printf "Failed to download and extract the tar file after %d attempts.\n" "$max_download_tries"
    return 1
  fi
}

retry_download "$install_dir/google-cloud-sdk.tar.gz" "$url_path_fixture" "$arg_version" "$install_dir" "$BASH_ENV" || exit 1

Having the requests inside a loop will make it easier to increase the number of retries should it be required in the future. It will also allow us to inform the user about the error and the steps to mitigate it - in this case, the number of retries. Additionally, we fail the job if the SDK isn't downloaded, as it is a requirement to get a successful run.

Please let me know what you think.

EricRibeiro commented 1 year ago

Closing due to #75.