|
| 1 | +# Simulated deploys for the ForeCode test estate. Source it before deploy commands: |
| 2 | +# |
| 3 | +# source .github/simulate.sh |
| 4 | +# helm upgrade --install web ./chart ... |
| 5 | +# |
| 6 | +# The workflow keeps real deploy commands, so ForeCode detects them exactly as it would in a customer's pipeline, |
| 7 | +# but while SIMULATE is "true" (the default) the deploy tools only print what they would do. Nothing is deployed and |
| 8 | +# no cloud credentials are needed. |
| 9 | +# |
| 10 | +# SIMULATE_FAIL_EVERY=N makes every Nth run (by GITHUB_RUN_NUMBER) fail its deploy, to give a pipeline a known change |
| 11 | +# failure rate. 0 (the default) never fails. |
| 12 | + |
| 13 | +SIMULATE="${SIMULATE:-true}" |
| 14 | +SIMULATE_FAIL_EVERY="${SIMULATE_FAIL_EVERY:-0}" |
| 15 | + |
| 16 | +_simulated() { |
| 17 | + echo "[simulated] $*" |
| 18 | +} |
| 19 | + |
| 20 | +# Deploy tools can fail on purpose (SIMULATE_FAIL_EVERY); building and publishing artifacts never do. |
| 21 | +_simulated_deploy() { |
| 22 | + _simulated "$@" |
| 23 | + if [ "$SIMULATE_FAIL_EVERY" -gt 0 ] && [ $(( ${GITHUB_RUN_NUMBER:-1} % SIMULATE_FAIL_EVERY )) -eq 0 ]; then |
| 24 | + echo "::error::simulated deploy failure (run ${GITHUB_RUN_NUMBER:-1}, every ${SIMULATE_FAIL_EVERY}th run fails)" |
| 25 | + return 1 |
| 26 | + fi |
| 27 | +} |
| 28 | + |
| 29 | +if [ "$SIMULATE" = "true" ]; then |
| 30 | + helm() { _simulated_deploy helm "$@"; } |
| 31 | + kubectl() { _simulated_deploy kubectl "$@"; } |
| 32 | + aws() { _simulated_deploy aws "$@"; } |
| 33 | + sam() { _simulated_deploy sam "$@"; } |
| 34 | + ssh() { _simulated_deploy ssh "$@"; } |
| 35 | + scp() { _simulated scp "$@"; } |
| 36 | + docker() { |
| 37 | + case "$1" in |
| 38 | + push|login) _simulated docker "$@" ;; |
| 39 | + *) command docker "$@" ;; |
| 40 | + esac |
| 41 | + } |
| 42 | + npm() { |
| 43 | + case "$1" in |
| 44 | + publish) _simulated npm "$@" ;; |
| 45 | + *) command npm "$@" ;; |
| 46 | + esac |
| 47 | + } |
| 48 | + terraform() { |
| 49 | + case "$1" in |
| 50 | + plan) _simulated terraform "$@" ;; |
| 51 | + apply|destroy|import) _simulated_deploy terraform "$@" ;; |
| 52 | + init) command terraform init -backend=false "${@:2}" ;; |
| 53 | + *) command terraform "$@" ;; |
| 54 | + esac |
| 55 | + } |
| 56 | + echo "Deploys are simulated (SIMULATE=true): deploy tools print instead of acting." |
| 57 | +fi |
0 commit comments