Skip to content

Commit 5b9c023

Browse files
authored
Merge pull request #5 from SCE-Development/docker-git-output-embed
display stdout and stderr output from git and docker in embed
2 parents f37ccdd + 22ee7e1 commit 5b9c023

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

server.py

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,16 @@ class RepoToWatch:
5050
branch: str
5151
path: str
5252

53+
5354
@dataclasses.dataclass
5455
class RepoUpdateResult:
5556
git_exit_code: int = 0
5657
docker_exit_code: int = 0
5758
development: bool = False
59+
git_stdout: str = ""
60+
git_stderr: str = ""
61+
docker_stdout: str = ""
62+
docker_stderr: str = ""
5863

5964

6065
def load_config(development: bool):
@@ -72,33 +77,44 @@ def load_config(development: bool):
7277

7378
def get_args():
7479
parser = argparse.ArgumentParser()
75-
parser.add_argument("--development", action='store_true')
80+
parser.add_argument("--development", action="store_true")
7681
return parser.parse_args()
7782

83+
7884
args = get_args()
7985

8086
config = load_config(args.development)
8187

82-
def push_update_success_as_discord_embed(repo_config: RepoToWatch, result: RepoUpdateResult):
88+
89+
def push_update_success_as_discord_embed(
90+
repo_config: RepoToWatch, result: RepoUpdateResult
91+
):
8392
repo_name = repo_config.name
8493
# default green
8594
color = 0x57F287
8695
if result.development:
87-
prefix = '[development mode]'
88-
repo_name = prefix + ' ' + repo_name
96+
prefix = "[development mode]"
97+
repo_name = prefix + " " + repo_name
8998
# do a gray color if we are sending "not real" embeds
9099
color = 0x99AAB5
91100

92101
embed_json = {
93102
"embeds": [
94103
{
95104
"title": f"{repo_name} was successfully updated",
96-
"url": "https://github.com/SCE-Development/" + repo_config.name, # link to CICD project repo
97-
"description": "\n".join([
98-
f"• git pull exited with code **{result.git_exit_code}**",
99-
f"• docker-compose up exited with code **{result.docker_exit_code}**"
100-
]),
101-
"color": color
105+
"url": "https://github.com/SCE-Development/"
106+
+ repo_config.name, # link to CICD project repo
107+
"description": "\n".join(
108+
[
109+
f"• git pull exited with code **{result.git_exit_code}**",
110+
f"• git stdout: **```{result.git_stdout or 'No output'}```**",
111+
f"• git stderr: **```{result.git_stderr or 'No output'}```**",
112+
f"• docker-compose up exited with code **{result.docker_exit_code}**",
113+
f"• docker-compose up stdout: **```{result.docker_stdout or 'No output'}```**",
114+
f"• docker-compose up stderr: **```{result.docker_stderr or 'No output'}```**",
115+
]
116+
),
117+
"color": color,
102118
}
103119
]
104120
}
@@ -116,6 +132,7 @@ def push_update_success_as_discord_embed(repo_config: RepoToWatch, result: RepoU
116132
except Exception:
117133
logger.exception("push_update_success_as_discord_embed had a bad time")
118134

135+
119136
def update_repo(repo_config: RepoToWatch) -> RepoUpdateResult:
120137
MetricsHandler.last_push_timestamp.labels(repo=repo_config.name).set(time.time())
121138
logger.info(
@@ -130,17 +147,27 @@ def update_repo(repo_config: RepoToWatch) -> RepoUpdateResult:
130147
return push_update_success_as_discord_embed(repo_config, result)
131148
try:
132149
git_result = subprocess.run(
133-
["git", "pull", "origin", repo_config.branch], cwd=repo_config.path
150+
["git", "pull", "origin", repo_config.branch],
151+
cwd=repo_config.path,
152+
capture_output=True,
153+
text=True,
134154
)
135155
logger.info(f"Git pull stdout: {git_result.stdout}")
136156
logger.info(f"Git pull stderr: {git_result.stderr}")
157+
result.git_stdout = git_result.stdout
158+
result.git_stderr = git_result.stderr
137159
result.git_exit_code = git_result.returncode
138160

139161
docker_result = subprocess.run(
140-
["docker-compose", "up", "--build", "-d"], cwd=repo_config.path
162+
["docker-compose", "up", "--build", "-d"],
163+
cwd=repo_config.path,
164+
capture_output=True,
165+
text=True,
141166
)
142167
logger.info(f"Docker compose stdout: {docker_result.stdout}")
143168
logger.info(f"Docker compose stdout: {docker_result.stderr}")
169+
result.docker_stdout = docker_result.stdout
170+
result.docker_stderr = docker_result.stderr
144171
result.git_exit_code = git_result.returncode
145172
push_update_success_as_discord_embed(repo_config, result)
146173
except Exception:
@@ -169,7 +196,7 @@ async def github_webhook(request: Request):
169196
if args.development and key not in config:
170197
# if we are in development mode, pretend that
171198
# we wanted to watch this repo no matter what
172-
config[key] = RepoToWatch(name=repo_name, branch=branch, path='/dev/null')
199+
config[key] = RepoToWatch(name=repo_name, branch=branch, path="/dev/null")
173200

174201
if key not in config:
175202
logging.warning(f"not acting on repo and branch name of {key}")

0 commit comments

Comments
 (0)