Skip to content

Commit a4bf023

Browse files
committed
Refactor deploy-to-vercel-action script to avoid too many paths error, add container .env file to gitignore
1 parent b01e71a commit a4bf023

3 files changed

Lines changed: 74 additions & 57 deletions

File tree

‎.github/actions/deploy-to-vercel-action/src/context.py‎

Lines changed: 69 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -51,47 +51,79 @@ def load_event_payload() -> dict[str, Any]:
5151
return {}
5252

5353

54-
def build_context() -> Context:
55-
github_repository = get_env("GITHUB_REPOSITORY")
54+
def _parse_repository_slug(github_repository: str) -> tuple[str, str]:
5655
if "/" not in github_repository:
5756
raise ValueError("GITHUB_REPOSITORY is not in 'owner/repo' format")
5857
user, repo = github_repository.split("/", 1)
58+
return user, repo
59+
60+
61+
def _require_input(name: str) -> str:
62+
value = get_input(name)
63+
if not value:
64+
raise ValueError(f"Missing required input: {name}")
65+
return value
66+
67+
68+
def _require_one_of_inputs(primary: str, fallback: str, *, display_name: str) -> str:
69+
value = get_input(primary) or get_input(fallback)
70+
if not value:
71+
raise ValueError(f"Missing required input: {display_name}")
72+
return value
73+
74+
75+
def _extract_pr_context(
76+
*,
77+
payload: dict[str, Any],
78+
github_repository: str,
79+
default_user: str,
80+
) -> tuple[str, str, str, str, int | None, bool]:
81+
pr = (payload or {}).get("pull_request") or {}
82+
83+
pr_number = int((payload or {}).get("number") or 0) or None
84+
85+
head = (pr.get("head") or {})
86+
sha = str(head.get("sha") or "").strip() or "XXXXXXX"
87+
branch = str(head.get("ref") or "").strip()
88+
ref = branch
89+
actor = str(((pr.get("user") or {}).get("login") or "")).strip() or default_user
90+
91+
head_repo_full_name = str(((head.get("repo") or {}).get("full_name") or "")).strip()
92+
is_fork = bool(head_repo_full_name and head_repo_full_name != github_repository)
93+
94+
return branch, sha, ref, actor, pr_number, is_fork
95+
96+
97+
def _extract_push_context(*, default_user: str) -> tuple[str, str, str, str, int | None, bool]:
98+
actor = get_env("GITHUB_ACTOR") or default_user
99+
ref = get_env("GITHUB_REF")
100+
sha = get_env("GITHUB_SHA") or "XXXXXXX"
101+
102+
# refs/heads/<branch>
103+
branch = ref[11:] if ref.startswith("refs/heads/") else ref
104+
105+
return branch, sha, ref, actor, None, False
106+
107+
108+
def build_context() -> Context:
109+
github_repository = get_env("GITHUB_REPOSITORY")
110+
user, repo = _parse_repository_slug(github_repository)
59111

60112
event_name = get_env("GITHUB_EVENT_NAME")
61113
is_pr = event_name in {"pull_request", "pull_request_target"}
62114

63-
github_token = get_input("GITHUB_TOKEN") or get_input("GH_PAT")
64-
vercel_token = get_input("VERCEL_TOKEN")
65-
vercel_org_id = get_input("VERCEL_ORG_ID")
66-
vercel_project_id = get_input("VERCEL_PROJECT_ID")
67-
68-
if not github_token:
69-
raise ValueError("Missing required input: GITHUB_TOKEN")
70-
if not vercel_token:
71-
raise ValueError("Missing required input: VERCEL_TOKEN")
72-
if not vercel_org_id:
73-
raise ValueError("Missing required input: VERCEL_ORG_ID")
74-
if not vercel_project_id:
75-
raise ValueError("Missing required input: VERCEL_PROJECT_ID")
115+
github_token = _require_one_of_inputs("GITHUB_TOKEN", "GH_PAT", display_name="GITHUB_TOKEN")
116+
vercel_token = _require_input("VERCEL_TOKEN")
117+
vercel_org_id = _require_input("VERCEL_ORG_ID")
118+
vercel_project_id = _require_input("VERCEL_PROJECT_ID")
76119

77120
production = parse_bool(get_input("PRODUCTION"), default=not is_pr)
78121

79-
public_google_maps_api_key = get_input("PUBLIC_GOOGLE_MAPS_API_KEY")
80-
public_google_map_id = get_input("PUBLIC_GOOGLE_MAP_ID")
81-
public_sentry_dsn = get_input("PUBLIC_SENTRY_DSN")
82-
public_upstash_search_rest_url = get_input("PUBLIC_UPSTASH_SEARCH_REST_URL")
83-
public_upstash_search_readonly_token = get_input("PUBLIC_UPSTASH_SEARCH_READONLY_TOKEN")
84-
85-
if not public_google_maps_api_key:
86-
raise ValueError("Missing required input: PUBLIC_GOOGLE_MAPS_API_KEY")
87-
if not public_google_map_id:
88-
raise ValueError("Missing required input: PUBLIC_GOOGLE_MAP_ID")
89-
if not public_sentry_dsn:
90-
raise ValueError("Missing required input: PUBLIC_SENTRY_DSN")
91-
if not public_upstash_search_rest_url:
92-
raise ValueError("Missing required input: PUBLIC_UPSTASH_SEARCH_REST_URL")
93-
if not public_upstash_search_readonly_token:
94-
raise ValueError("Missing required input: PUBLIC_UPSTASH_SEARCH_READONLY_TOKEN")
122+
public_google_maps_api_key = _require_input("PUBLIC_GOOGLE_MAPS_API_KEY")
123+
public_google_map_id = _require_input("PUBLIC_GOOGLE_MAP_ID")
124+
public_sentry_dsn = _require_input("PUBLIC_SENTRY_DSN")
125+
public_upstash_search_rest_url = _require_input("PUBLIC_UPSTASH_SEARCH_REST_URL")
126+
public_upstash_search_readonly_token = _require_input("PUBLIC_UPSTASH_SEARCH_READONLY_TOKEN")
95127

96128
pr_labels = parse_disableable_list(get_input("PR_LABELS"), default=["deployed"])
97129
alias_domains = parse_disableable_list(get_input("ALIAS_DOMAINS"), default=None)
@@ -104,34 +136,14 @@ def build_context() -> Context:
104136
log_url = f"https://github.com/{user}/{repo}/actions/runs/{run_id}" if run_id else f"https://github.com/{user}/{repo}"
105137

106138
payload = load_event_payload()
107-
108-
pr_number: int | None = None
109-
branch = ""
110-
sha = ""
111-
ref = ""
112-
actor = ""
113-
is_fork = False
114-
115139
if is_pr:
116-
pr = (payload or {}).get("pull_request") or {}
117-
pr_number = int((payload or {}).get("number") or 0) or None
118-
head = (pr.get("head") or {})
119-
sha = str(head.get("sha") or "").strip()
120-
branch = str(head.get("ref") or "").strip()
121-
ref = branch
122-
actor = str(((pr.get("user") or {}).get("login") or "")).strip() or user
123-
124-
head_repo_full_name = str(((head.get("repo") or {}).get("full_name") or "")).strip()
125-
is_fork = bool(head_repo_full_name and head_repo_full_name != github_repository)
140+
branch, sha, ref, actor, pr_number, is_fork = _extract_pr_context(
141+
payload=payload,
142+
github_repository=github_repository,
143+
default_user=user,
144+
)
126145
else:
127-
actor = get_env("GITHUB_ACTOR") or user
128-
ref = get_env("GITHUB_REF")
129-
sha = get_env("GITHUB_SHA")
130-
# refs/heads/<branch>
131-
branch = ref[11:] if ref.startswith("refs/heads/") else ref
132-
133-
if not sha:
134-
sha = "XXXXXXX"
146+
branch, sha, ref, actor, pr_number, is_fork = _extract_push_context(default_user=user)
135147

136148
return Context(
137149
github_token=github_token,

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ npm-debug.log*
2626
# dotenv environment variable files
2727
.env*
2828
!.env.example
29+
!test/containers/.env
2930

3031
# Optional npm cache directory
3132
.npm

‎test/containers/.env‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
COMPOSE_PROJECT_NAME=wb-e2e
2+
WIREMOCK_HOST=127.0.0.1
3+
CONVERTKIT_HTTP_PORT=9010
4+
RESEND_HTTP_PORT=9011

0 commit comments

Comments
 (0)