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
11 changes: 11 additions & 0 deletions argocd-pr-env-deploy/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
# fetches the parent chart from. `--revision` is passed on every
# deploy, defaulting to `main`, so dropping the line resets the parent
# (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.
#
# Linked-PR image tags resolve in order:
# 1. `development-head-<head sha>` — pushed by build-push-ecr's PR
Expand Down Expand Up @@ -247,6 +250,14 @@ 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")"

# 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.
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
fi

# Lint: `Deploys:`-looking lines that don't match the strict form
# would otherwise be silently ignored and the env would quietly run
# defaults — the classic offender is a pasted PR URL. Warn per line.
Expand Down
31 changes: 31 additions & 0 deletions tests/test_argocd_pr_env_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,34 @@ def test_does_nothing_before_the_parent_exists(self, tmp_path):

def test_the_switch_off_runs_after_reading_the_parent_and_before_app_set():
assert RUN.index(PARENT_GET_CALL) < RUN.index(STAFF_OFF_START) < RUN.index(APP_SET_CALL)


CUSTOMER_SKIP_START = 'LABEL_HOST="$(jq'
CUSTOMER_SKIP_END = "\n\n# Lint: `Deploys:`-looking lines"


def run_customer_skip(labels):
"""Run the action from reading the PR's labels through the customer check."""
segment = RUN[RUN.index(CUSTOMER_SKIP_START):RUN.index(CUSTOMER_SKIP_END)]
pr_json = '{"labels": [' + ", ".join(f'{{"name": "{name}"}}' for name in labels) + "]}"
script = f'PR_JSON="$1"\n{segment}\necho "went on to argocd"'
return subprocess.run(["bash", "-c", script, "_", pr_json], capture_output=True, text=True)


class TestCustomerEnvs:
def test_a_customer_labeled_pr_stops_before_argocd(self):
result = run_customer_skip(["deploy", "customer:acme"])
assert result.returncode == 0
assert "::notice::This PR carries customer:acme. An admin deploys customer envs" in result.stdout
assert "went on to argocd" not in result.stdout

def test_an_ordinary_pr_goes_on(self):
result = run_customer_skip(["deploy"])
assert result.returncode == 0
assert "went on to argocd" in result.stdout


def test_the_customer_check_runs_before_any_argocd_call():
skip = RUN.index('if [ -n "$LABEL_HOST" ]; then')
assert skip < RUN.index(PARENT_GET_CALL) < RUN.index(APP_SET_CALL)
assert skip < RUN.index('"$ARGOCD_BIN"')
Loading