-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathentrypoint.py
More file actions
195 lines (159 loc) · 6.59 KB
/
entrypoint.py
File metadata and controls
195 lines (159 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python3
import glob
import os
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
from subprocess import CalledProcessError
def log(msg: str) -> None:
print(f"[cos-sync] {msg}")
def error(msg: str) -> None:
print(f"::error::{msg}", file=sys.stderr)
sys.exit(1)
def get_input(name: str, required: bool = True, default: str | None = None) -> str:
env_name = f"INPUT_{name.upper()}"
val = os.environ.get(env_name, "")
if not val and default is not None:
val = default
if required and not val:
error(f"Missing required input: {name}")
return val
def parse_bool(name: str, val: str) -> bool:
normalized = val.strip().lower()
if normalized in ("true", "1", "yes", "y"):
return True
if normalized in ("false", "0", "no", "n", ""):
return False
error(f"Invalid boolean value for {name}: {val}")
return False
def normalize_prefix(prefix: str) -> str:
return prefix if prefix.endswith("/") else prefix + "/"
def split_patterns(raw: str) -> list[str]:
parts = []
for token in raw.split(","):
for line in token.splitlines():
cleaned = line.strip()
if cleaned:
parts.append(cleaned)
return parts
def resolve_paths(patterns: list[str]) -> list[Path]:
paths: list[Path] = []
for pat in patterns:
matches = [Path(p) for p in glob.glob(pat, recursive=True)]
if not matches:
error(f"No files matched pattern: {pat}")
paths.extend(matches)
return paths
def stage_paths(paths: list[Path], staging_root: Path) -> None:
cwd = Path.cwd()
for src in paths:
abs_src = src.resolve()
try:
rel = abs_src.relative_to(cwd)
except ValueError:
error(f"Path must be within workspace: {abs_src}")
dest = staging_root / rel
if src.is_dir():
if dest.exists() and dest.is_file():
error(f"Collision while staging directory (file already staged here): {dest}")
dest.parent.mkdir(parents=True, exist_ok=True)
log(f"Staging directory: {rel}")
shutil.copytree(abs_src, dest, dirs_exist_ok=True)
elif src.is_file():
if dest.exists():
if dest.is_file():
log(f"Skipping already staged file from overlapping glob: {rel}")
continue
error(f"Collision while staging file (directory already staged here): {dest}")
dest.parent.mkdir(parents=True, exist_ok=True)
log(f"Staging file: {rel}")
shutil.copy2(abs_src, dest)
else:
error(f"Path is neither file nor directory: {src}")
def run_cmd(cmd: list[str], cwd: Path | None = None, env: dict[str, str] | None = None) -> None:
log(f"Running: {' '.join(cmd)}" + (f" (cwd={cwd})" if cwd else ""))
subprocess.run(cmd, check=True, cwd=str(cwd) if cwd else None, env=env)
def configure_coscmd(secret_id: str, secret_key: str, bucket: str, region: str, use_global: bool = False) -> None:
cmd = ["coscmd", "config", "-a", secret_id, "-s", secret_key, "-b", bucket]
if use_global:
cmd += ["-e", "cos.accelerate.myqcloud.com"]
cmd += ["--retry", "5"]
cmd += ["--timeout", "60"]
else:
cmd += ["-r", region]
cmd += ["--retry", "1"] # 失败了赶紧fallback到全球加速
cmd += ["--timeout", "10"]
cmd += ["-m", "1"]
run_cmd(cmd)
def main() -> None:
workspace_root = Path.cwd().resolve()
secret_id = get_input("secret_id")
secret_key = get_input("secret_key")
region = get_input("region")
bucket = get_input("bucket")
prefix = normalize_prefix(get_input("prefix"))
artifacts_raw = get_input("artifacts")
flush_url = get_input("flush_url", required=False, default="")
delete_remote = parse_bool("delete_remote", get_input("delete_remote", required=False, default="false"))
global_mode = parse_bool("global", get_input("global", required=False, default="false"))
working_dir_raw = get_input("working_directory", required=False, default="").strip()
if working_dir_raw:
working_dir = (workspace_root / working_dir_raw).resolve()
try:
working_dir.relative_to(workspace_root)
except ValueError:
error(f"working_directory must be inside the workspace: {working_dir}")
if not working_dir.is_dir():
error(f"working_directory does not exist or is not a directory: {working_dir}")
os.chdir(working_dir)
log(f"Using working_directory: {working_dir}")
patterns = split_patterns(artifacts_raw)
if not patterns:
error("No artifact patterns provided after normalization")
paths = resolve_paths(patterns)
if global_mode:
log("Configuring coscmd (global accelerate endpoint)")
configure_coscmd(secret_id, secret_key, bucket, region, use_global=True)
else:
log("Configuring coscmd (regional endpoint)")
configure_coscmd(secret_id, secret_key, bucket, region, use_global=False)
with tempfile.TemporaryDirectory() as tmpdir:
staging_root = Path(tmpdir)
log(f"Staging uploads in: {staging_root}")
stage_paths(paths, staging_root)
flags = ["-rs", "--yes"]
if delete_remote:
flags.append("--delete")
try:
run_cmd(
["coscmd", "upload", *flags, ".", prefix],
cwd=staging_root,
)
except CalledProcessError as exc:
if global_mode:
error(f"Upload failed with global accelerate endpoint: {exc}")
log("Upload failed with regional endpoint, retrying with global accelerate endpoint")
configure_coscmd(secret_id, secret_key, bucket, region, use_global=True)
try:
run_cmd(
["coscmd", "upload", *flags, ".", prefix],
cwd=staging_root,
)
except CalledProcessError as retry_exc:
error(f"Upload failed after retry with accelerate endpoint: {retry_exc}")
if flush_url:
log(f"Purge CDN cache: {flush_url}")
env = os.environ.copy()
env["TENCENTCLOUD_SECRET_ID"] = secret_id
env["TENCENTCLOUD_SECRET_KEY"] = secret_key
env["TENCENTCLOUD_REGION"] = region
run_cmd(
["tccli", "cdn", "PurgePathCache", "--cli-unfold-argument", "--Paths", flush_url, "--FlushType", "flush"],
env=env,
)
else:
log("flush_url not provided; skipping CDN purge")
if __name__ == "__main__":
main()