forked from kubernetes-sigs/agent-sandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease
More file actions
executable file
·223 lines (189 loc) · 7.61 KB
/
Copy pathrelease
File metadata and controls
executable file
·223 lines (189 loc) · 7.61 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python3
# Copyright 2025 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import glob
import json
import os
import re
import subprocess
import sys
def run_command(cmd):
"""Helper to run shell commands."""
try:
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError:
print(f"Error running command: {' '.join(cmd)}")
sys.exit(1)
def get_release_info(tag):
"""Returns (exists, is_draft) for the GitHub release."""
cmd = [
"gh", "release", "view", tag,
"--repo", "kubernetes-sigs/agent-sandbox",
"--json", "isDraft",
]
try:
result = subprocess.run(
cmd,
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
except FileNotFoundError:
print("Error: GitHub CLI 'gh' was not found in PATH.")
sys.exit(1)
if result.returncode == 0:
try:
release_info = json.loads(result.stdout or "{}")
except json.JSONDecodeError:
print(
"Error parsing GitHub release info for "
f"{tag}: {' '.join(cmd)} returned invalid JSON."
)
stdout = (result.stdout or "").strip()
if stdout:
print(stdout)
sys.exit(1)
return True, bool(release_info.get("isDraft"))
stderr = (result.stderr or "").strip()
stdout = (result.stdout or "").strip()
combined_output = "\n".join(part for part in [stderr, stdout] if part)
normalized_output = combined_output.lower()
if "release not found" in normalized_output or "http 404" in normalized_output:
return False, False
print(
"Error checking GitHub release info for "
f"{tag}: {' '.join(cmd)} failed with exit code {result.returncode}."
)
if combined_output:
print(combined_output)
sys.exit(1)
def main():
parser = argparse.ArgumentParser(description="Generate release manifests.")
parser.add_argument("--tag", required=True, help="The git tag for the release.")
parser.add_argument("--publish", action="store_true", help="Publish the release to GitHub.")
parser.add_argument(
"--model",
default=os.getenv("GEMINI_MODEL", "gemini-2.5-flash"),
help="Gemini model to use for generating release notes.",
)
args = parser.parse_args()
tag = args.tag
# Create the release_assets directory if it doesn't exist.
if not os.path.exists("release_assets"):
os.makedirs("release_assets")
all_yaml_files = glob.glob("k8s/**/*.yaml", recursive=True)
image_url = f"registry.k8s.io/agent-sandbox/agent-sandbox-controller:{args.tag}"
# --- Generate manifest.yaml ---
print("INFO: Preparing core manifest.yaml")
manifest_files = [f for f in all_yaml_files if "extensions" not in os.path.basename(f)]
with open("release_assets/manifest.yaml", "w") as outfile:
for fname in sorted(manifest_files):
with open(fname) as infile:
outfile.write(infile.read())
outfile.write("---\n")
# Replace image placeholder in manifest.yaml.
print(f"INFO: Replacing image placeholder in core manifest.yaml with: {image_url}")
with open("release_assets/manifest.yaml", "r") as file:
filedata = file.read()
filedata = re.sub(
r"ko://sigs.k8s.io/agent-sandbox/cmd/agent-sandbox-controller.*",
image_url,
filedata,
)
with open("release_assets/manifest.yaml", "w") as file:
file.write(filedata)
print("INFO: Successfully created release_assets/manifest.yaml")
# --- Generate extensions.yaml ---
print("INFO: Preparing extensions.yaml")
extension_files = [f for f in all_yaml_files if "extensions" in os.path.basename(f)]
with open("release_assets/extensions.yaml", "w") as outfile:
for fname in sorted(extension_files):
with open(fname) as infile:
outfile.write(infile.read())
outfile.write("---\n")
# Replace image placeholder in extensions.yaml.
print(f"INFO: Replacing image placeholder in extensions.yaml with: {image_url}")
with open("release_assets/extensions.yaml", "r") as file:
filedata = file.read()
filedata = re.sub(
r"ko://sigs.k8s.io/agent-sandbox/cmd/agent-sandbox-controller.*",
image_url,
filedata,
)
with open("release_assets/extensions.yaml", "w") as file:
file.write(filedata)
print("INFO: Successfully created release_assets/extensions.yaml")
if args.publish:
print(f"INFO: Publishing draft release {tag} to GitHub...")
manifest_path = "release_assets/manifest.yaml"
extensions_path = "release_assets/extensions.yaml"
if not os.path.exists(manifest_path) or not os.path.exists(extensions_path):
print("❌ Release assets missing. Cannot publish.")
sys.exit(1)
release_exists, is_draft = get_release_info(tag)
if release_exists:
if not is_draft:
print(
f"❌ Release {tag} already exists and is published. "
"Refusing to update assets."
)
sys.exit(1)
print(f"INFO: Draft release {tag} already exists; updating assets...")
run_command([
"gh", "release", "upload", tag,
manifest_path,
extensions_path,
"--clobber",
"--repo", "kubernetes-sigs/agent-sandbox",
])
else:
# Generate release notes using the external standalone script.
notes_file = "release_assets/release_notes.md"
print("INFO: Generating release notes using dev/tools/generate-release-notes...")
try:
subprocess.run(
[
"./dev/tools/generate-release-notes",
"--tag", tag,
"--output", notes_file,
"--model", args.model,
],
check=True,
)
print(f"INFO: Release notes generated at {notes_file}")
except subprocess.CalledProcessError as e:
print(f"⚠️ Error generating release notes with script: {e}")
print("⚠️ Falling back to default GitHub notes on release creation.")
notes_file = None
cmd = [
"gh", "release", "create", tag,
manifest_path,
extensions_path,
]
if notes_file:
cmd.extend(["--notes-file", notes_file])
cmd.extend([
"--generate-notes",
"--title", tag,
"--verify-tag",
"--draft",
"--repo", "kubernetes-sigs/agent-sandbox",
])
run_command(cmd)
print(f"🎉 Draft release {tag} published successfully!")
print("👉 Go to https://github.com/kubernetes-sigs/agent-sandbox/releases to review and publish.")
if __name__ == "__main__":
main()