Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 106 additions & 95 deletions argocd-pr-env-deploy/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
# (the AppSet's ignoreApplicationDifferences no longer reverts it).
# - A PR labeled `customer:<hostBase>` deploys nothing. An admin deploys
# customer envs, and the Argo CD token this action holds can only read
# their parents.
# their parents. The label is still checked against the env that exists
# first, so a label added after `deploy` is refused, not skipped.
#
# Linked-PR image tags resolve in order:
# 1. `development-head-<head sha>` — pushed by build-push-ecr's PR
Expand Down Expand Up @@ -250,9 +251,105 @@ runs:
PR_BODY="$(jq -r '.body // ""' <<<"$PR_JSON" | tr -d '\r')"
LABEL_HOST="$(jq -r '[.labels[].name | select(startswith("customer:")) | ltrimstr("customer:")] | first // ""' <<<"$PR_JSON")"

# Post a comment on the anchor PR explaining a refused deploy.
post_blocking_comment() {
jq -n --arg b "$1" '{body: $b}' \
| curl -sfX POST -H "Authorization: Bearer $GH_TOKEN" \
-H "Accept: application/vnd.github+json" \
"https://api.github.com/repos/$OWN_REPO/issues/$PR_NUMBER/comments" \
--data @- >/dev/null \
|| echo "::warning::could not post the blocking comment to ${OWN_REPO}#${PR_NUMBER} (does the gh-token allow writing issue comments?)" >&2
}

# Install argocd CLI to a writable location if missing. /usr/local/bin
# is owned by root on self-hosted runners; $RUNNER_TEMP is per-job
# and always user-writable.
if command -v argocd >/dev/null 2>&1; then
ARGOCD_BIN=argocd
else
ARGOCD_BIN="${RUNNER_TEMP:-/tmp}/argocd"
if [ ! -x "$ARGOCD_BIN" ]; then
curl -sSLo "$ARGOCD_BIN" "https://github.com/argoproj/argo-cd/releases/download/${ARGOCD_VERSION}/argocd-linux-amd64"
chmod +x "$ARGOCD_BIN"
fi
fi

# Flags every argocd call needs, resolved once.
#
# --grpc-web is unconditional: it is harmless on a direct connection and
# required through the nginx ingress.
#
# --plaintext is the endpoint question. argocd-server runs with
# `server.insecure: true`, so it serves cleartext on Service port 80 and
# expects TLS to terminate upstream at the ingress. Talking to the
# Service directly therefore MUST be plaintext, and talking to the public
# hostname must NOT be. `auto` reads that off the endpoint rather than
# making callers keep two inputs consistent by hand; an explicit
# true/false still wins for anything this rule does not cover.
case "${ARGOCD_PLAINTEXT:-auto}" in
true) ARGOCD_PLAINTEXT_RESOLVED=true ;;
false) ARGOCD_PLAINTEXT_RESOLVED=false ;;
auto)
case "$ARGOCD_SERVER" in
*.svc.cluster.local|*.svc.cluster.local:*|*.svc|*.svc:*|localhost|localhost:*|127.0.0.1|127.0.0.1:*)
ARGOCD_PLAINTEXT_RESOLVED=true ;;
*)
ARGOCD_PLAINTEXT_RESOLVED=false ;;
esac
;;
*)
echo "::error::argocd-plaintext must be 'auto', 'true' or 'false' (got '${ARGOCD_PLAINTEXT}')."
exit 1
;;
esac
ARGOCD_FLAGS=(--grpc-web)
if [ "$ARGOCD_PLAINTEXT_RESOLVED" = true ]; then
ARGOCD_FLAGS+=(--plaintext)
fi
echo "ArgoCD endpoint: ${ARGOCD_SERVER} (plaintext=${ARGOCD_PLAINTEXT_RESOLVED})"

# Skip `argocd login` — it doesn't honour --auth-token (login only
# accepts username/password/sso), and the ARGOCD_SERVER +
# ARGOCD_AUTH_TOKEN env vars set above are picked up by every
# subsequent argocd command directly.

# Compare the PR's `customer:` label with the env that exists. An
# ordinary env reads its label once, when the ApplicationSet creates it,
# and an admin creates a customer env's parent. A parent that is absent
# or being deleted has nothing to compare: skip, and for an ordinary PR
# the `app set` retry below waits for the ApplicationSet to create it.
PARENT_JSON=""
get_errfile="$(mktemp)"
if ! PARENT_JSON="$("$ARGOCD_BIN" app get "$PARENT_APP" "${ARGOCD_FLAGS[@]}" -o json 2>"$get_errfile")"; then
PARENT_JSON=""
get_err="$(cat "$get_errfile")"
case "$get_err" in
*PermissionDenied*|*NotFound*|*"not found"*|*"permission denied"*|*"being deleted"*)
echo "Application ${PARENT_APP} not found or being deleted; skipping the customer label check."
;;
*)
echo "argocd app get ${PARENT_APP} failed unexpectedly:" >&2
echo "$get_err" >&2
rm -f "$get_errfile"
exit 1
;;
esac
fi
rm -f "$get_errfile"
if [ -n "$PARENT_JSON" ]; then
PARENT_HOST="$(jq -r '[.spec.source.helm.parameters[]? | select(.name=="hostBase") | .value] | first // ""' <<<"$PARENT_JSON")"
if [ "$LABEL_HOST" != "$PARENT_HOST" ]; then
comment="**PR-env deploy blocked.** This PR's \`customer:\` label does not match the environment that already exists (label: \`${LABEL_HOST}\`, env: \`${PARENT_HOST}\`). An ordinary env reads the label only when it is created, and an admin creates a customer env. Remove the \`deploy\` label and wait for the env to disappear; an admin then sets up the customer env."
post_blocking_comment "$comment"
echo "::error::Refusing to deploy: customer label '${LABEL_HOST}' does not match the existing env's hostBase '${PARENT_HOST}'."
exit 1
fi
fi

# An admin deploys a customer env, pinning its parent to a reviewed
# argocd-envs commit. This action's token can read that parent but not
# change it, so a customer-labeled PR sets nothing here.
# change it, so a customer-labeled PR whose label matches its env, or
# that has no env yet, sets nothing here. A mismatch was refused above.
if [ -n "$LABEL_HOST" ]; then
echo "::notice::This PR carries customer:${LABEL_HOST}. An admin deploys customer envs, so this job changes nothing in Argo CD."
exit 0
Expand Down Expand Up @@ -504,16 +601,6 @@ runs:
done
)

# Post a comment on the anchor PR explaining a refused deploy.
post_blocking_comment() {
jq -n --arg b "$1" '{body: $b}' \
| curl -sfX POST -H "Authorization: Bearer $GH_TOKEN" \
-H "Accept: application/vnd.github+json" \
"https://api.github.com/repos/$OWN_REPO/issues/$PR_NUMBER/comments" \
--data @- >/dev/null \
|| echo "::warning::could not post the blocking comment to ${OWN_REPO}#${PR_NUMBER} (does the gh-token allow writing issue comments?)" >&2
}

# Fail closed on any unresolvable Deploys link, and comment on the
# anchor PR so the cause is visible without digging through the log.
if [ "${#unresolved_links[@]}" -gt 0 ]; then
Expand All @@ -532,89 +619,6 @@ runs:
echo "Parent chart (argocd-envs) pinned to branch ${ENVS_REVISION} via ${ENVS_LINK}."
fi

# Install argocd CLI to a writable location if missing. /usr/local/bin
# is owned by root on self-hosted runners; $RUNNER_TEMP is per-job
# and always user-writable.
if command -v argocd >/dev/null 2>&1; then
ARGOCD_BIN=argocd
else
ARGOCD_BIN="${RUNNER_TEMP:-/tmp}/argocd"
if [ ! -x "$ARGOCD_BIN" ]; then
curl -sSLo "$ARGOCD_BIN" "https://github.com/argoproj/argo-cd/releases/download/${ARGOCD_VERSION}/argocd-linux-amd64"
chmod +x "$ARGOCD_BIN"
fi
fi

# Flags every argocd call needs, resolved once.
#
# --grpc-web is unconditional: it is harmless on a direct connection and
# required through the nginx ingress.
#
# --plaintext is the endpoint question. argocd-server runs with
# `server.insecure: true`, so it serves cleartext on Service port 80 and
# expects TLS to terminate upstream at the ingress. Talking to the
# Service directly therefore MUST be plaintext, and talking to the public
# hostname must NOT be. `auto` reads that off the endpoint rather than
# making callers keep two inputs consistent by hand; an explicit
# true/false still wins for anything this rule does not cover.
case "${ARGOCD_PLAINTEXT:-auto}" in
true) ARGOCD_PLAINTEXT_RESOLVED=true ;;
false) ARGOCD_PLAINTEXT_RESOLVED=false ;;
auto)
case "$ARGOCD_SERVER" in
*.svc.cluster.local|*.svc.cluster.local:*|*.svc|*.svc:*|localhost|localhost:*|127.0.0.1|127.0.0.1:*)
ARGOCD_PLAINTEXT_RESOLVED=true ;;
*)
ARGOCD_PLAINTEXT_RESOLVED=false ;;
esac
;;
*)
echo "::error::argocd-plaintext must be 'auto', 'true' or 'false' (got '${ARGOCD_PLAINTEXT}')."
exit 1
;;
esac
ARGOCD_FLAGS=(--grpc-web)
if [ "$ARGOCD_PLAINTEXT_RESOLVED" = true ]; then
ARGOCD_FLAGS+=(--plaintext)
fi
echo "ArgoCD endpoint: ${ARGOCD_SERVER} (plaintext=${ARGOCD_PLAINTEXT_RESOLVED})"

# Skip `argocd login` — it doesn't honour --auth-token (login only
# accepts username/password/sso), and the ARGOCD_SERVER +
# ARGOCD_AUTH_TOKEN env vars set above are picked up by every
# subsequent argocd command directly.

# The `customer:` label is read only when the env is created. A parent
# that is absent or being deleted has nothing to compare: skip, and the
# `app set` retry below waits for the ApplicationSet to create it.
PARENT_JSON=""
get_errfile="$(mktemp)"
if ! PARENT_JSON="$("$ARGOCD_BIN" app get "$PARENT_APP" "${ARGOCD_FLAGS[@]}" -o json 2>"$get_errfile")"; then
PARENT_JSON=""
get_err="$(cat "$get_errfile")"
case "$get_err" in
*PermissionDenied*|*NotFound*|*"not found"*|*"permission denied"*|*"being deleted"*)
echo "Application ${PARENT_APP} not found or being deleted; skipping the customer label check."
;;
*)
echo "argocd app get ${PARENT_APP} failed unexpectedly:" >&2
echo "$get_err" >&2
rm -f "$get_errfile"
exit 1
;;
esac
fi
rm -f "$get_errfile"
if [ -n "$PARENT_JSON" ]; then
PARENT_HOST="$(jq -r '[.spec.source.helm.parameters[]? | select(.name=="hostBase") | .value] | first // ""' <<<"$PARENT_JSON")"
if [ "$LABEL_HOST" != "$PARENT_HOST" ]; then
comment="**PR-env deploy blocked.** This PR's \`customer:\` label does not match the environment that already exists (label: \`${LABEL_HOST}\`, env: \`${PARENT_HOST}\`). The label is read only when the env is created. Remove the \`deploy\` label, wait for the env to disappear, then add \`customer:<hostBase>\` first and \`deploy\` second."
post_blocking_comment "$comment"
echo "::error::Refusing to deploy: customer label '${LABEL_HOST}' does not match the existing env's hostBase '${PARENT_HOST}'."
exit 1
fi
fi

# Turn the staff console off when nothing selects it any more. Only the
# two staff console parameters go: a parent can carry other parameters
# set by hand, which this must keep. A parent that does not exist yet
Expand Down Expand Up @@ -647,6 +651,13 @@ runs:
if set_err="$("$ARGOCD_BIN" app set "$PARENT_APP" "${ARGOCD_FLAGS[@]}" "${set_args[@]}" 2>&1)"; then
break
fi
# A parent the token could read but can't change won't become
# set-able by waiting: it belongs to a project ci-pr-envs doesn't hold.
if [ -n "$PARENT_JSON" ] && [[ "$set_err" == *PermissionDenied* || "$set_err" == *"permission denied"* ]]; then
echo "::error::ci-pr-envs may not change ${PARENT_APP} (project $(jq -r '.spec.project' <<<"$PARENT_JSON")); an admin deploys it." >&2
echo "$set_err" >&2
exit 1
fi
case "$set_err" in
*PermissionDenied*|*NotFound*|*"not found"*|*"permission denied"*|*"being deleted"*) ;;
*)
Expand Down
8 changes: 4 additions & 4 deletions scripts/prune_pr_environments.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
Environments are listed before PRs, so an environment created mid-run is left
for the next run. A repo whose PR list cannot be read is skipped entirely.

A closed PR labelled ``deploy`` and ``customer:<hostBase>`` had a customer env
whose data nothing deletes, so each one gets a single comment listing it (see
``REMINDER``). Every closed PR is scanned, so a PR missed while the job was
A closed PR labelled ``deploy`` and ``customer:<hostBase>`` anchors a customer
env, which an admin removes and whose data nothing deletes, so each one gets a
single comment saying so (see ``REMINDER``). Every closed PR is scanned, so a PR missed while the job was
failing is still reminded once it recovers.
"""

Expand All @@ -29,7 +29,7 @@
CUSTOMER_PREFIX = "customer:"
MARKER = "<!-- customer-env-close-reminder -->"
REMINDER = MARKER + """
This customer env is gone, but its data is not. Delete by hand when the engagement is over:
Closing this PR leaves its customer env running: an admin removes it. Its data outlives that. Delete by hand when the engagement is over:
- Files: the `customer-envs/{host_base}/` prefix in the bucket named by `filesBucket` in argocd-envs `parent/values.yaml`
- Cowork workspaces: the `pr-{slug}-{number}` directory on the cowork EFS
- Postgres: the retained PersistentVolume for `data-postgres-postgresql-0` in namespace `pr-{slug}-{number}` (`kubectl get pv | grep pr-{slug}-{number}`)
Expand Down
Loading
Loading