Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(release): Set gitconfig before git write operations #32277

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/create_rc_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ jobs:
env:
MATRIX: ${{ matrix.value }}
run: |
git fetch
if ${{ env.IS_AGENT6_RELEASE == 'true' }}; then
inv -e release.create-rc -r "$MATRIX" --slack-webhook=${{ secrets.AGENT_RELEASE_SYNC_SLACK_WEBHOOK }} --patch-version
else
Expand Down
10 changes: 5 additions & 5 deletions tasks/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ def update(self):


@task(post=[tidy])
def update(ctx):
def update(_):
updater = CollectorVersionUpdater()
updater.update()
print("Update complete.")
Expand All @@ -505,12 +505,12 @@ def update(ctx):
@task()
def pull_request(ctx):
# Save current Git configuration
original_config = {'user.name': get_git_config('user.name'), 'user.email': get_git_config('user.email')}
original_config = {'user.name': get_git_config(ctx, 'user.name'), 'user.email': get_git_config(ctx, 'user.email')}

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

# Perform Git operations
ctx.run('git add .')
Expand All @@ -533,4 +533,4 @@ def pull_request(ctx):
print("No changes detected, skipping PR creation.")
finally:
# Revert to original Git configuration
revert_git_config(original_config)
revert_git_config(ctx, original_config)
20 changes: 11 additions & 9 deletions tasks/libs/common/git.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import os
import subprocess
import sys
import tempfile
from contextlib import contextmanager
Expand Down Expand Up @@ -295,18 +294,21 @@ def get_last_release_tag(ctx, repo, pattern):
return last_tag_commit, last_tag_name


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


def set_git_config(key, value):
subprocess.run(['git', 'config', key, value])
def set_git_config(ctx, key, value):
ctx.run(f'git config {key} {value}')


def revert_git_config(original_config):
def revert_git_config(ctx, original_config):
for key, value in original_config.items():
if value is None:
subprocess.run(['git', 'config', '--unset', key])
ctx.run(f'git config --unset {key}', hide=True)
else:
subprocess.run(['git', 'config', key, value])
ctx.run(f'git config {key} {value}', hide=True)
11 changes: 10 additions & 1 deletion tasks/libs/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

from tasks.libs.common.color import Color, color_message
from tasks.libs.common.constants import ALLOWED_REPO_ALL_BRANCHES, REPO_PATH
from tasks.libs.common.git import get_commit_sha, get_default_branch
from tasks.libs.common.git import get_commit_sha, get_default_branch, set_git_config
from tasks.libs.releasing.version import get_version
from tasks.libs.types.arch import Arch

Expand Down Expand Up @@ -506,6 +506,15 @@ def is_pr_context(branch, pr_id, test_name):
return True


def set_gitconfig_in_ci(ctx):
"""
Set username and email when runing git "write" commands in CI
"""
if running_in_ci():
set_git_config('user.name', 'github-actions[bot]')
set_git_config('user.email', 'github-actions[bot]@users.noreply.github.com')


@contextmanager
def gitlab_section(section_name, collapsed=False, echo=False):
"""
Expand Down
15 changes: 8 additions & 7 deletions tasks/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
)
from tasks.libs.common.gomodules import get_default_modules
from tasks.libs.common.user_interactions import yes_no_question
from tasks.libs.common.utils import set_gitconfig_in_ci
from tasks.libs.common.worktree import agent_context
from tasks.libs.pipeline.notifications import (
DEFAULT_JIRA_PROJECT,
Expand Down Expand Up @@ -254,6 +255,7 @@ def tag_modules(

if push:
tags_list = ' '.join(tags)
set_gitconfig_in_ci(ctx)
ctx.run(f"git push origin {tags_list}{force_option}")
print(f"Pushed tag {tags_list}")
print(f"Created module tags for version {agent_version}")
Expand Down Expand Up @@ -289,6 +291,7 @@ def tag_version(

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

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

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

with agent_context(ctx, release_branch):
github = GithubAPI(repository=GITHUB_REPO_NAME)
github_action = os.environ.get("GITHUB_ACTIONS")

if github_action:
set_git_config('user.name', 'github-actions[bot]')
set_git_config('user.email', 'github-actions[bot]@users.noreply.github.com')
upstream = f"https://x-access-token:{os.environ.get('GITHUB_TOKEN')}@github.com/{GITHUB_REPO_NAME}.git"

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

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

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

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