Skip to content

Commit a7dd125

Browse files
committed
feat(release): Set gitconfig before git write operations
1 parent 9f73ec4 commit a7dd125

File tree

5 files changed

+35
-22
lines changed

5 files changed

+35
-22
lines changed

.github/workflows/create_rc_pr.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ jobs:
106106
env:
107107
MATRIX: ${{ matrix.value }}
108108
run: |
109+
git fetch
109110
if ${{ env.IS_AGENT6_RELEASE == 'true' }}; then
110111
inv -e release.create-rc -r "$MATRIX" --slack-webhook=${{ secrets.AGENT_RELEASE_SYNC_SLACK_WEBHOOK }} --patch-version
111112
else

tasks/collector.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ def update(self):
496496

497497

498498
@task(post=[tidy])
499-
def update(ctx):
499+
def update(_):
500500
updater = CollectorVersionUpdater()
501501
updater.update()
502502
print("Update complete.")
@@ -505,12 +505,12 @@ def update(ctx):
505505
@task()
506506
def pull_request(ctx):
507507
# Save current Git configuration
508-
original_config = {'user.name': get_git_config('user.name'), 'user.email': get_git_config('user.email')}
508+
original_config = {'user.name': get_git_config(ctx, 'user.name'), 'user.email': get_git_config(ctx, 'user.email')}
509509

510510
try:
511511
# Set new Git configuration
512-
set_git_config('user.name', 'github-actions[bot]')
513-
set_git_config('user.email', 'github-actions[bot]@users.noreply.github.com')
512+
set_git_config(ctx, 'user.name', 'github-actions[bot]')
513+
set_git_config(ctx, 'user.email', 'github-actions[bot]@users.noreply.github.com')
514514

515515
# Perform Git operations
516516
ctx.run('git add .')
@@ -533,4 +533,4 @@ def pull_request(ctx):
533533
print("No changes detected, skipping PR creation.")
534534
finally:
535535
# Revert to original Git configuration
536-
revert_git_config(original_config)
536+
revert_git_config(ctx, original_config)

tasks/libs/common/git.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import os
4-
import subprocess
54
import sys
65
import tempfile
76
from contextlib import contextmanager
@@ -295,18 +294,21 @@ def get_last_release_tag(ctx, repo, pattern):
295294
return last_tag_commit, last_tag_name
296295

297296

298-
def get_git_config(key):
299-
result = subprocess.run(['git', 'config', '--get', key], capture_output=True, text=True)
300-
return result.stdout.strip() if result.returncode == 0 else None
297+
def get_git_config(ctx, key):
298+
try:
299+
result = ctx.run(f'git config --get {key}')
300+
except Exit:
301+
return None
302+
return result.stdout.strip() if result.return_code == 0 else None
301303

302304

303-
def set_git_config(key, value):
304-
subprocess.run(['git', 'config', key, value])
305+
def set_git_config(ctx, key, value):
306+
ctx.run(f'git config {key} {value}')
305307

306308

307-
def revert_git_config(original_config):
309+
def revert_git_config(ctx, original_config):
308310
for key, value in original_config.items():
309311
if value is None:
310-
subprocess.run(['git', 'config', '--unset', key])
312+
ctx.run(f'git config --unset {key}', hide=True)
311313
else:
312-
subprocess.run(['git', 'config', key, value])
314+
ctx.run(f'git config {key} {value}', hide=True)

tasks/libs/common/utils.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
from tasks.libs.common.color import Color, color_message
2525
from tasks.libs.common.constants import ALLOWED_REPO_ALL_BRANCHES, REPO_PATH
26-
from tasks.libs.common.git import get_commit_sha, get_default_branch
26+
from tasks.libs.common.git import get_commit_sha, get_default_branch, set_git_config
2727
from tasks.libs.releasing.version import get_version
2828
from tasks.libs.types.arch import Arch
2929

@@ -506,6 +506,15 @@ def is_pr_context(branch, pr_id, test_name):
506506
return True
507507

508508

509+
def set_gitconfig_in_ci(ctx):
510+
"""
511+
Set username and email when runing git "write" commands in CI
512+
"""
513+
if running_in_ci():
514+
set_git_config('user.name', 'github-actions[bot]')
515+
set_git_config('user.email', 'github-actions[bot]@users.noreply.github.com')
516+
517+
509518
@contextmanager
510519
def gitlab_section(section_name, collapsed=False, echo=False):
511520
"""

tasks/release.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
)
4040
from tasks.libs.common.gomodules import get_default_modules
4141
from tasks.libs.common.user_interactions import yes_no_question
42+
from tasks.libs.common.utils import set_gitconfig_in_ci
4243
from tasks.libs.common.worktree import agent_context
4344
from tasks.libs.pipeline.notifications import (
4445
DEFAULT_JIRA_PROJECT,
@@ -254,6 +255,7 @@ def tag_modules(
254255

255256
if push:
256257
tags_list = ' '.join(tags)
258+
set_gitconfig_in_ci(ctx)
257259
ctx.run(f"git push origin {tags_list}{force_option}")
258260
print(f"Pushed tag {tags_list}")
259261
print(f"Created module tags for version {agent_version}")
@@ -289,6 +291,7 @@ def tag_version(
289291

290292
if push:
291293
tags_list = ' '.join(tags)
294+
set_gitconfig_in_ci(ctx)
292295
ctx.run(f"git push origin {tags_list}{force_option}")
293296
print(f"Pushed tag {tags_list}")
294297
print(f"Created tags for version {agent_version}")
@@ -349,6 +352,7 @@ def finish(ctx, release_branch, upstream="origin"):
349352

350353
commit_message = f"'Final updates for release.json and Go modules for {new_version} release'"
351354

355+
set_gitconfig_in_ci(ctx)
352356
ok = try_git_command(ctx, f"git commit -m {commit_message}")
353357
if not ok:
354358
raise Exit(
@@ -437,12 +441,6 @@ def create_rc(ctx, release_branch, patch_version=False, upstream="origin", slack
437441

438442
with agent_context(ctx, release_branch):
439443
github = GithubAPI(repository=GITHUB_REPO_NAME)
440-
github_action = os.environ.get("GITHUB_ACTIONS")
441-
442-
if github_action:
443-
set_git_config('user.name', 'github-actions[bot]')
444-
set_git_config('user.email', 'github-actions[bot]@users.noreply.github.com')
445-
upstream = f"https://x-access-token:{os.environ.get('GITHUB_TOKEN')}@github.com/{GITHUB_REPO_NAME}.git"
446444

447445
# Get the version of the highest major: useful for some logging & to get
448446
# the version to use for Go submodules updates
@@ -496,10 +494,10 @@ def create_rc(ctx, release_branch, patch_version=False, upstream="origin", slack
496494
ctx.run("git add release.json")
497495
ctx.run("git ls-files . | grep 'go.mod$' | xargs git add")
498496

497+
set_gitconfig_in_ci(ctx)
499498
ok = try_git_command(
500499
ctx,
501500
f"git commit --no-verify -m 'Update release.json and Go modules for {new_highest_version}'",
502-
github_action,
503501
)
504502
if not ok:
505503
raise Exit(
@@ -680,6 +678,7 @@ def _main():
680678
# Step 2 - Push newly created release branch to the remote repository
681679

682680
print(color_message("Pushing new branch to the upstream repository", "bold"))
681+
set_gitconfig_in_ci(ctx)
683682
res = ctx.run(f"git push --set-upstream {upstream} {release_branch}", warn=True)
684683
if res.exited is None or res.exited > 0:
685684
raise Exit(
@@ -871,6 +870,7 @@ def cleanup(ctx, release_branch):
871870
ctx.run("git add release.json")
872871

873872
commit_message = f"Update last_stable to {version}"
873+
set_gitconfig_in_ci(ctx)
874874
ok = try_git_command(ctx, f"git commit -m '{commit_message}'")
875875
if not ok:
876876
raise Exit(
@@ -1184,6 +1184,7 @@ def check_for_changes(ctx, release_branch, warning_mode=False):
11841184
with clone(ctx, repo_name, repo['branch'], options="--filter=blob:none --no-checkout"):
11851185
# We can add the new commit now to be used by release candidate creation
11861186
print(f"Creating new tag {next_version} on {repo_name}", file=sys.stderr)
1187+
set_gitconfig_in_ci(ctx)
11871188
ctx.run(f"git tag {next_version}")
11881189
ctx.run(f"git push origin tag {next_version}")
11891190
# This repo has changes, the next check is not needed

0 commit comments

Comments
 (0)