|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# ----------------------------------------------------------------------------- |
| 4 | +# Doppler Deploy Common Helper |
| 5 | +# |
| 6 | +# Centralises all Doppler + Helmfile deployment logic so each environment |
| 7 | +# wrapper only needs to set `CONFIG` (e.g. stg, prd) and invoke this script. |
| 8 | +# |
| 9 | +# Workflow: |
| 10 | +# 1. `CONFIG` must be defined by the caller. If not, we exit with error. |
| 11 | +# 2. Attempt a normal deploy via: |
| 12 | +# doppler run --config "$CONFIG" -- helmfile "$@" |
| 13 | +# On success the script exits 0. |
| 14 | +# 3. If that fails, check if we are running from an external drive |
| 15 | +# (`/Volumes/*` on macOS). Keychain auth often breaks there. |
| 16 | +# • If on external drive: |
| 17 | +# a. Look for service-token file named: |
| 18 | +# ~/.doppler-tokens/<git-root-directory>-$CONFIG |
| 19 | +# b. If token exists → export DOPPLER_TOKEN and retry doppler. |
| 20 | +# c. If token is missing → instruct user how to create a Service Token |
| 21 | +# in the Doppler dashboard, suggest naming it |
| 22 | +# `<project>-<config>`, and where to save it; then exit 1. |
| 23 | +# d. You can create a Service Token in the Doppler web UI under: |
| 24 | +# Projects → <your project> → <config> → Access tab → "Generate Service Token". |
| 25 | +# 4. If not on an external drive and doppler still failed, prompt the user to |
| 26 | +# run `doppler login` and `doppler setup`, then exit 1. |
| 27 | +# |
| 28 | +# All arguments received are passed unchanged to Helmfile (`"$@"`). |
| 29 | +# The script is `set -euo pipefail` safe – it aborts on any error or unset var. |
| 30 | +# ----------------------------------------------------------------------------- |
| 31 | + |
| 32 | +set -euo pipefail |
| 33 | + |
| 34 | +if [[ -z "${CONFIG:-}" ]]; then |
| 35 | + echo "CONFIG environment variable not set. Please set CONFIG before calling this script." >&2 |
| 36 | + exit 1 |
| 37 | +fi |
| 38 | + |
| 39 | +# First, try Doppler normally |
| 40 | +if doppler settings 2>/dev/null; then |
| 41 | + doppler run --config "$CONFIG" -- helmfile "$@" |
| 42 | + exit 0 |
| 43 | +fi |
| 44 | + |
| 45 | +# If that failed, resolve current path and check if we're on an external drive |
| 46 | +CURRENT_DIR="$(command -v realpath >/dev/null 2>&1 && realpath "$PWD" || pwd -P)" |
| 47 | +if [[ "$CURRENT_DIR" == /Volumes/* ]]; then |
| 48 | + echo "⚠️ Detected external drive (resolved path: $CURRENT_DIR). Attempting alternative authentication..." |
| 49 | + |
| 50 | + # Check for existing service token specific to this project & config |
| 51 | + TOKEN_FILE="$HOME/.doppler-tokens/$(basename $(git rev-parse --show-toplevel 2>/dev/null || pwd))-$CONFIG" |
| 52 | + |
| 53 | + if [ -f "$TOKEN_FILE" ]; then |
| 54 | + export DOPPLER_TOKEN="$(cat "$TOKEN_FILE")" |
| 55 | + doppler run --config "$CONFIG" -- helmfile "$@" |
| 56 | + exit 0 |
| 57 | + fi |
| 58 | + |
| 59 | + echo "❌ No service token found for external drive usage." |
| 60 | + echo "" |
| 61 | + |
| 62 | + # Determine closest doppler.yaml to fill in project/config placeholders |
| 63 | + DOPPLER_YAML="" |
| 64 | + SEARCH_DIR="$CURRENT_DIR" |
| 65 | + while [[ "$SEARCH_DIR" != "/" ]]; do |
| 66 | + if [[ -f "$SEARCH_DIR/doppler.yaml" ]]; then |
| 67 | + DOPPLER_YAML="$SEARCH_DIR/doppler.yaml" |
| 68 | + break |
| 69 | + fi |
| 70 | + SEARCH_DIR="$(dirname "$SEARCH_DIR")" |
| 71 | + done |
| 72 | + |
| 73 | + if [[ -n "$DOPPLER_YAML" ]]; then |
| 74 | + DOPPLER_PROJECT="$(grep -E '^\s*project:' "$DOPPLER_YAML" | head -n1 | awk '{print $2}')" |
| 75 | + DOPPLER_CONFIG="$(grep -E '^\s*config:' "$DOPPLER_YAML" | head -n1 | awk '{print $2}')" |
| 76 | + fi |
| 77 | + |
| 78 | + # Guidance for the user (also echoed to terminal) |
| 79 | +echo "To fix this, create a Service Token for project '${DOPPLER_PROJECT:-<project>}' and config '${DOPPLER_CONFIG:-<config>}' in the Doppler dashboard (https://doppler.com)." |
| 80 | + echo "Name the token something like '${DOPPLER_PROJECT:-<project>}-${DOPPLER_CONFIG:-<config>}' so it's clear which environment it belongs to." |
| 81 | + echo "Copy the token and save it into: $TOKEN_FILE" |
| 82 | + echo "" |
| 83 | + echo "Example:" |
| 84 | + echo " echo 'dp.st.your_generated_token' > $TOKEN_FILE" |
| 85 | + echo "" |
| 86 | + echo "After saving the token, rerun this script." |
| 87 | + exit 1 |
| 88 | +else |
| 89 | + # Not on external drive, but doppler still failed earlier |
| 90 | + echo "❌ Doppler command failed. Please run 'doppler login' and 'doppler setup'" |
| 91 | + exit 1 |
| 92 | +fi |
0 commit comments