|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Validate required configuration |
| 4 | +if [ -z "${BUILDKITE_PLUGIN_AZURE_CACHE_CONTAINER}" ]; then |
| 5 | + echo '+++ 🚨 Missing Azure container configuration' |
| 6 | + exit 1 |
| 7 | +fi |
| 8 | + |
| 9 | +if [ -z "${BUILDKITE_PLUGIN_AZURE_CACHE_ACCOUNT}" ]; then |
| 10 | + echo '+++ 🚨 Missing Azure storage account configuration' |
| 11 | + exit 1 |
| 12 | +fi |
| 13 | + |
| 14 | +build_key() { |
| 15 | + if [ -n "${BUILDKITE_PLUGIN_AZURE_CACHE_PREFIX}" ]; then |
| 16 | + echo "${BUILDKITE_PLUGIN_AZURE_CACHE_PREFIX}/${1}" |
| 17 | + else |
| 18 | + echo "$1" |
| 19 | + fi |
| 20 | +} |
| 21 | + |
| 22 | +az_cmd() { |
| 23 | + local subcommand="$1" |
| 24 | + shift |
| 25 | + |
| 26 | + local cmd_args=(az storage blob "${subcommand}") |
| 27 | + cmd_args+=(--account-name "${BUILDKITE_PLUGIN_AZURE_CACHE_ACCOUNT}") |
| 28 | + |
| 29 | + if [[ "${BUILDKITE_PLUGIN_AZURE_CACHE_QUIET:-false}" =~ ^(true|on|1)$ ]]; then |
| 30 | + # Only upload and download support --no-progress |
| 31 | + if [ "${subcommand}" = "upload" ] || [ "${subcommand}" = "download" ]; then |
| 32 | + cmd_args+=(--no-progress) |
| 33 | + fi |
| 34 | + cmd_args+=(--only-show-errors) |
| 35 | + fi |
| 36 | + |
| 37 | + if [ -n "${BUILDKITE_PLUGIN_AZURE_CACHE_AUTH_MODE}" ]; then |
| 38 | + cmd_args+=(--auth-mode "${BUILDKITE_PLUGIN_AZURE_CACHE_AUTH_MODE}") |
| 39 | + |
| 40 | + # When using key auth mode, explicitly pass the account key |
| 41 | + if [ "${BUILDKITE_PLUGIN_AZURE_CACHE_AUTH_MODE}" = "key" ] && [ -n "${AZURE_STORAGE_KEY}" ]; then |
| 42 | + cmd_args+=(--account-key "${AZURE_STORAGE_KEY}") |
| 43 | + fi |
| 44 | + fi |
| 45 | + |
| 46 | + # When quiet mode is enabled, suppress stdout (progress bars, JSON output) |
| 47 | + # but preserve stderr (error messages) |
| 48 | + if [[ "${BUILDKITE_PLUGIN_AZURE_CACHE_QUIET:-false}" =~ ^(true|on|1)$ ]]; then |
| 49 | + "${cmd_args[@]}" "$@" >/dev/null |
| 50 | + else |
| 51 | + "${cmd_args[@]}" "$@" |
| 52 | + fi |
| 53 | +} |
| 54 | + |
| 55 | +azure_exists() { |
| 56 | + local key="$1" |
| 57 | + local blob_name |
| 58 | + blob_name="$(build_key "${key}")" |
| 59 | + |
| 60 | + # Try to check if exact blob exists |
| 61 | + if az_cmd show \ |
| 62 | + --container-name "${BUILDKITE_PLUGIN_AZURE_CACHE_CONTAINER}" \ |
| 63 | + --name "${blob_name}" &>/dev/null; then |
| 64 | + return 0 |
| 65 | + fi |
| 66 | + |
| 67 | + # Check if it's a directory-like prefix (with trailing slash) |
| 68 | + local list_args=(az storage blob list) |
| 69 | + list_args+=(--container-name "${BUILDKITE_PLUGIN_AZURE_CACHE_CONTAINER}") |
| 70 | + list_args+=(--account-name "${BUILDKITE_PLUGIN_AZURE_CACHE_ACCOUNT}") |
| 71 | + list_args+=(--prefix "${blob_name}/") |
| 72 | + list_args+=(--num-results 1) |
| 73 | + list_args+=(--query '[0].name') |
| 74 | + list_args+=(--output tsv) |
| 75 | + |
| 76 | + # Note: list command does not support --no-progress |
| 77 | + if [[ "${BUILDKITE_PLUGIN_AZURE_CACHE_QUIET:-false}" =~ ^(true|on|1)$ ]]; then |
| 78 | + list_args+=(--only-show-errors) |
| 79 | + fi |
| 80 | + |
| 81 | + if [ -n "${BUILDKITE_PLUGIN_AZURE_CACHE_AUTH_MODE}" ]; then |
| 82 | + list_args+=(--auth-mode "${BUILDKITE_PLUGIN_AZURE_CACHE_AUTH_MODE}") |
| 83 | + |
| 84 | + # When using key auth mode, explicitly pass the account key |
| 85 | + if [ "${BUILDKITE_PLUGIN_AZURE_CACHE_AUTH_MODE}" = "key" ] && [ -n "${AZURE_STORAGE_KEY}" ]; then |
| 86 | + list_args+=(--account-key "${AZURE_STORAGE_KEY}") |
| 87 | + fi |
| 88 | + fi |
| 89 | + |
| 90 | + local result |
| 91 | + result=$("${list_args[@]}" 2>/dev/null) |
| 92 | + |
| 93 | + [ -n "${result}" ] |
| 94 | +} |
| 95 | + |
| 96 | +restore_cache() { |
| 97 | + local from="$1" |
| 98 | + local to="$2" |
| 99 | + local blob_name |
| 100 | + blob_name="$(build_key "${from}")" |
| 101 | + |
| 102 | + # Check if it's a single blob or a directory |
| 103 | + if az_cmd show \ |
| 104 | + --container-name "${BUILDKITE_PLUGIN_AZURE_CACHE_CONTAINER}" \ |
| 105 | + --name "${blob_name}" &>/dev/null; then |
| 106 | + # Single file - use download |
| 107 | + az_cmd download \ |
| 108 | + --container-name "${BUILDKITE_PLUGIN_AZURE_CACHE_CONTAINER}" \ |
| 109 | + --name "${blob_name}" \ |
| 110 | + --file "${to}" |
| 111 | + else |
| 112 | + # Directory - use download-batch |
| 113 | + local batch_args=(az storage blob download-batch) |
| 114 | + batch_args+=(--account-name "${BUILDKITE_PLUGIN_AZURE_CACHE_ACCOUNT}") |
| 115 | + batch_args+=(--source "${BUILDKITE_PLUGIN_AZURE_CACHE_CONTAINER}") |
| 116 | + batch_args+=(--destination "${to}") |
| 117 | + batch_args+=(--pattern "${blob_name}/*") |
| 118 | + |
| 119 | + if [[ "${BUILDKITE_PLUGIN_AZURE_CACHE_QUIET:-false}" =~ ^(true|on|1)$ ]]; then |
| 120 | + batch_args+=(--no-progress) |
| 121 | + batch_args+=(--only-show-errors) |
| 122 | + fi |
| 123 | + |
| 124 | + if [ -n "${BUILDKITE_PLUGIN_AZURE_CACHE_AUTH_MODE}" ]; then |
| 125 | + batch_args+=(--auth-mode "${BUILDKITE_PLUGIN_AZURE_CACHE_AUTH_MODE}") |
| 126 | + |
| 127 | + # When using key auth mode, explicitly pass the account key |
| 128 | + if [ "${BUILDKITE_PLUGIN_AZURE_CACHE_AUTH_MODE}" = "key" ] && [ -n "${AZURE_STORAGE_KEY}" ]; then |
| 129 | + batch_args+=(--account-key "${AZURE_STORAGE_KEY}") |
| 130 | + fi |
| 131 | + fi |
| 132 | + |
| 133 | + # When quiet mode is enabled, suppress stdout but preserve stderr |
| 134 | + if [[ "${BUILDKITE_PLUGIN_AZURE_CACHE_QUIET:-false}" =~ ^(true|on|1)$ ]]; then |
| 135 | + "${batch_args[@]}" >/dev/null |
| 136 | + else |
| 137 | + "${batch_args[@]}" |
| 138 | + fi |
| 139 | + fi |
| 140 | +} |
| 141 | + |
| 142 | +save_cache() { |
| 143 | + local to="$1" |
| 144 | + local from="$2" |
| 145 | + local blob_name |
| 146 | + blob_name="$(build_key "${to}")" |
| 147 | + |
| 148 | + if [ ! -e "${from}" ]; then |
| 149 | + echo "+++ 🚨 Cache source path does not exist: ${from}" >&2 |
| 150 | + return 1 |
| 151 | + fi |
| 152 | + |
| 153 | + if [ -f "${from}" ]; then |
| 154 | + # Single file - use upload |
| 155 | + az_cmd upload \ |
| 156 | + --container-name "${BUILDKITE_PLUGIN_AZURE_CACHE_CONTAINER}" \ |
| 157 | + --name "${blob_name}" \ |
| 158 | + --file "${from}" \ |
| 159 | + --overwrite |
| 160 | + else |
| 161 | + # Directory - use upload-batch |
| 162 | + local batch_args=(az storage blob upload-batch) |
| 163 | + batch_args+=(--account-name "${BUILDKITE_PLUGIN_AZURE_CACHE_ACCOUNT}") |
| 164 | + batch_args+=(--destination "${BUILDKITE_PLUGIN_AZURE_CACHE_CONTAINER}") |
| 165 | + batch_args+=(--source "${from}") |
| 166 | + batch_args+=(--destination-path "${blob_name}") |
| 167 | + batch_args+=(--overwrite) |
| 168 | + |
| 169 | + if [[ "${BUILDKITE_PLUGIN_AZURE_CACHE_QUIET:-false}" =~ ^(true|on|1)$ ]]; then |
| 170 | + batch_args+=(--no-progress) |
| 171 | + batch_args+=(--only-show-errors) |
| 172 | + fi |
| 173 | + |
| 174 | + if [ -n "${BUILDKITE_PLUGIN_AZURE_CACHE_AUTH_MODE}" ]; then |
| 175 | + batch_args+=(--auth-mode "${BUILDKITE_PLUGIN_AZURE_CACHE_AUTH_MODE}") |
| 176 | + |
| 177 | + # When using key auth mode, explicitly pass the account key |
| 178 | + if [ "${BUILDKITE_PLUGIN_AZURE_CACHE_AUTH_MODE}" = "key" ] && [ -n "${AZURE_STORAGE_KEY}" ]; then |
| 179 | + batch_args+=(--account-key "${AZURE_STORAGE_KEY}") |
| 180 | + fi |
| 181 | + fi |
| 182 | + |
| 183 | + # When quiet mode is enabled, suppress stdout but preserve stderr |
| 184 | + if [[ "${BUILDKITE_PLUGIN_AZURE_CACHE_QUIET:-false}" =~ ^(true|on|1)$ ]]; then |
| 185 | + "${batch_args[@]}" >/dev/null |
| 186 | + else |
| 187 | + "${batch_args[@]}" |
| 188 | + fi |
| 189 | + fi |
| 190 | +} |
| 191 | + |
| 192 | +exists_cache() { |
| 193 | + if [ -z "$1" ]; then exit 1; fi |
| 194 | + azure_exists "$1" |
| 195 | +} |
| 196 | + |
| 197 | +OPCODE="$1" |
| 198 | +shift |
| 199 | + |
| 200 | +if [ "$OPCODE" = 'exists' ]; then |
| 201 | + exists_cache "$@" |
| 202 | +elif [ "$OPCODE" = 'get' ]; then |
| 203 | + restore_cache "$@" |
| 204 | +elif [ "$OPCODE" = 'save' ]; then |
| 205 | + save_cache "$@" |
| 206 | +else |
| 207 | + exit 255 |
| 208 | +fi |
0 commit comments