-
Notifications
You must be signed in to change notification settings - Fork 25
Add support for GCS #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add support for GCS #138
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,142 @@ | ||||||||
| #!/bin/bash | ||||||||
|
|
||||||||
| if [ -z "${BUILDKITE_PLUGIN_GCS_CACHE_BUCKET}" ]; then | ||||||||
| echo '+++ 🚨 Missing GCS bucket configuration' | ||||||||
| exit 1 | ||||||||
| fi | ||||||||
|
|
||||||||
| # Detect which GCS CLI to use | ||||||||
| detect_gcs_cli() { | ||||||||
| # Check for explicit preference | ||||||||
| if [ -n "${BUILDKITE_PLUGIN_GCS_CACHE_CLI}" ]; then | ||||||||
| echo "${BUILDKITE_PLUGIN_GCS_CACHE_CLI}" | ||||||||
| return | ||||||||
| fi | ||||||||
|
|
||||||||
| # Auto-detect: prefer gcloud storage if available | ||||||||
| if command -v gcloud &>/dev/null && gcloud storage --help &>/dev/null 2>&1; then | ||||||||
| echo "gcloud" | ||||||||
| elif command -v gsutil &>/dev/null; then | ||||||||
| echo "gsutil" | ||||||||
| else | ||||||||
| echo '+++ 🚨 Neither gcloud storage nor gsutil found' >&2 | ||||||||
| exit 1 | ||||||||
| fi | ||||||||
| } | ||||||||
|
|
||||||||
| # Lazy load CLI selection | ||||||||
| get_gcs_cli() { | ||||||||
| if [ -z "${GCS_CLI}" ]; then | ||||||||
| GCS_CLI=$(detect_gcs_cli) | ||||||||
| fi | ||||||||
| echo "${GCS_CLI}" | ||||||||
| } | ||||||||
|
|
||||||||
| build_key() { | ||||||||
| if [ -n "${BUILDKITE_PLUGIN_GCS_CACHE_PREFIX}" ]; then | ||||||||
| echo "${BUILDKITE_PLUGIN_GCS_CACHE_PREFIX}/${1}" | ||||||||
| else | ||||||||
| echo "$1" | ||||||||
| fi | ||||||||
| } | ||||||||
|
|
||||||||
| gcs_cmd() { | ||||||||
| local cmd_args=() | ||||||||
| local cli | ||||||||
| cli=$(get_gcs_cli) | ||||||||
|
|
||||||||
| if [ "${cli}" = "gcloud" ]; then | ||||||||
| cmd_args=(gcloud storage) | ||||||||
| if [ -n "${BUILDKITE_PLUGIN_GCS_CACHE_QUIET}" ]; then | ||||||||
| cmd_args+=(--verbosity=none) | ||||||||
| fi | ||||||||
| else | ||||||||
| cmd_args=(gsutil) | ||||||||
| if [ -n "${BUILDKITE_PLUGIN_GCS_CACHE_QUIET}" ]; then | ||||||||
| cmd_args+=(-q) | ||||||||
| fi | ||||||||
| fi | ||||||||
|
|
||||||||
| "${cmd_args[@]}" "$@" | ||||||||
| } | ||||||||
|
|
||||||||
| gcs_copy() { | ||||||||
| local from="$1" | ||||||||
| local to="$2" | ||||||||
| local use_rsync="${3:-true}" | ||||||||
| local cli | ||||||||
| cli=$(get_gcs_cli) | ||||||||
|
|
||||||||
| if [ "${use_rsync}" = 'true' ]; then | ||||||||
| # Use rsync for directories | ||||||||
| if [ "${cli}" = "gcloud" ]; then | ||||||||
| gcs_cmd rsync -r -d "${from}" "${to}" | ||||||||
| else | ||||||||
| gcs_cmd -m rsync -r -d "${from}" "${to}" | ||||||||
| fi | ||||||||
| else | ||||||||
| # Use cp for single files | ||||||||
| gcs_cmd cp "${from}" "${to}" | ||||||||
| fi | ||||||||
| } | ||||||||
|
|
||||||||
| gcs_exists() { | ||||||||
| local key="$1" | ||||||||
| local full_path="gs://${BUILDKITE_PLUGIN_GCS_CACHE_BUCKET}/$(build_key "${key}")" | ||||||||
|
|
||||||||
| # Check if the object exists using ls | ||||||||
| # For directories, ls will list contents | ||||||||
| # For files, it will return the file path | ||||||||
| if gcs_cmd ls "${full_path}*" &>/dev/null; then | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be possible for us to do this without the wildcard? As this may cause some false positives |
||||||||
| return 0 | ||||||||
| else | ||||||||
| return 1 | ||||||||
| fi | ||||||||
| } | ||||||||
|
|
||||||||
| restore_cache() { | ||||||||
| local from="$1" | ||||||||
| local to="$2" | ||||||||
| local use_rsync='false' | ||||||||
| local key="$(build_key "${from}")" | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixes a spellchecker issue
Suggested change
|
||||||||
| local full_path="gs://${BUILDKITE_PLUGIN_GCS_CACHE_BUCKET}/${key}" | ||||||||
|
|
||||||||
| # Check if it's a directory by trying to list it as a prefix | ||||||||
| if gcs_cmd ls "${full_path}/" &>/dev/null; then | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may be preferable here to use |
||||||||
| use_rsync='true' | ||||||||
| fi | ||||||||
|
|
||||||||
| gcs_copy "${full_path}" "${to}" "${use_rsync}" | ||||||||
| } | ||||||||
|
|
||||||||
| save_cache() { | ||||||||
| local to="$1" | ||||||||
| local from="$2" | ||||||||
| local use_rsync='true' | ||||||||
| local key="$(build_key "${to}")" | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixes a spellchecker issue
Suggested change
|
||||||||
| local full_path="gs://${BUILDKITE_PLUGIN_GCS_CACHE_BUCKET}/${key}" | ||||||||
|
|
||||||||
| if [ -f "${from}" ]; then | ||||||||
| use_rsync='false' | ||||||||
| fi | ||||||||
|
|
||||||||
| gcs_copy "${from}" "${full_path}" "${use_rsync}" | ||||||||
| } | ||||||||
|
|
||||||||
| exists_cache() { | ||||||||
| if [ -z "$1" ]; then exit 1; fi | ||||||||
| gcs_exists "$1" | ||||||||
| } | ||||||||
|
|
||||||||
| OPCODE="$1" | ||||||||
| shift | ||||||||
|
|
||||||||
| if [ "$OPCODE" = 'exists' ]; then | ||||||||
| exists_cache "$@" | ||||||||
| elif [ "$OPCODE" = 'get' ]; then | ||||||||
| restore_cache "$@" | ||||||||
| elif [ "$OPCODE" = 'save' ]; then | ||||||||
| save_cache "$@" | ||||||||
| else | ||||||||
| exit 255 | ||||||||
| fi | ||||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixes a spellchecker issue