Skip to content

Commit 249b565

Browse files
Merge pull request #166 from stuggi/only_one_push
FR branching improvement - Split commit_and_push into commit() and commit_and_push()
2 parents bbdcd74 + 54b1707 commit 249b565

1 file changed

Lines changed: 44 additions & 25 deletions

File tree

.github/workflows/create-release-branch-v1.yml

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -142,34 +142,54 @@ jobs:
142142
cat <<'EOF' > workflow_functions.sh
143143
#!/bin/bash
144144
145-
# A function to handle committing and pushing changes.
146-
# Arguments: 1: Commit Message, 2: File(s) to add, 3: Branch to push
145+
# A function to handle committing changes (without pushing).
146+
# Arguments: 1: Commit Message, 2: File(s) to add
147147
# Returns: 0 on success, 1 on failure
148-
commit_and_push() {
148+
commit() {
149149
local commit_message="$1"
150150
local files_to_add="$2"
151-
local branch_to_push="$3"
152151
153152
if ! git diff --quiet -- $files_to_add; then
154-
echo "Showing changes for branch '${branch_to_push}':"
153+
echo "Showing changes:"
155154
git diff -- $files_to_add
156155
if [ "$DRY_RUN" = "true" ]; then
157-
echo "DRY RUN: Commit '${commit_message}' not created for branch '${branch_to_push}'."
156+
echo "DRY RUN: Commit '${commit_message}' not created."
158157
return 0
159158
else
160159
git config --global user.name '${{ steps.app-token.outputs.app-slug }}[bot]' || return 1
161160
git config --global user.email '${{ steps.get-user-id.outputs.user-id }}+${{ steps.app-token.outputs.app-slug }}[bot]@users.noreply.github.com' || return 1
162161
git add $files_to_add || return 1
163162
git commit -m "$commit_message" || return 1
164-
echo "Pushing changes to ${branch_to_push}..."
165-
retry_command 3 5 "Pushing to ${branch_to_push}" git push origin ${branch_to_push} || return 1
166163
fi
167164
else
168165
echo "No changes detected in '${files_to_add}'. Nothing to commit."
169166
fi
170167
return 0
171168
}
172169
170+
# A function to handle committing and pushing changes.
171+
# Arguments: 1: Commit Message, 2: File(s) to add, 3: Branch to push
172+
# Returns: 0 on success, 1 on failure
173+
commit_and_push() {
174+
local commit_message="$1"
175+
local files_to_add="$2"
176+
local branch_to_push="$3"
177+
178+
# Use commit function to create the commit
179+
if ! commit "$commit_message" "$files_to_add"; then
180+
return 1
181+
fi
182+
183+
# Push if there were changes and not in dry run mode
184+
if ! git diff --quiet HEAD~1 -- $files_to_add 2>/dev/null || ! git diff --quiet -- $files_to_add; then
185+
if [ "$DRY_RUN" != "true" ]; then
186+
echo "Pushing changes to ${branch_to_push}..."
187+
retry_command 3 5 "Pushing to ${branch_to_push}" git push origin ${branch_to_push} || return 1
188+
fi
189+
fi
190+
return 0
191+
}
192+
173193
# Updates a value in a YAML file, automatically detecting the type of the existing node.
174194
# Arguments: 1: File Path, 2: YQ Target Path, 3: New Value
175195
update_yaml_value() {
@@ -389,6 +409,8 @@ jobs:
389409
skip_makefile_changes=$(yq e ".repos[] | select(.name == \"${repo_name}\") | .skip_makefile_changes // false" "${REPO_LIST_FILE}")
390410

391411
BRANCH_PUSHED=false
412+
413+
# Update Makefile if needed
392414
if [ -f "Makefile" ] && [ "$skip_makefile_changes" != "true" ]; then
393415
echo "Makefile found. Checking for variables to update on new branch..."
394416
if grep -qE "^${BRANCH_VARIABLE}\s*(\?=|=)" Makefile; then
@@ -404,13 +426,11 @@ jobs:
404426
fi
405427
fi
406428

407-
# Check if there are changes to commit and push
429+
# Commit Makefile changes if any
408430
if ! git diff --quiet -- Makefile; then
409-
# FAIL if commit and push fails
410-
if ! commit_and_push "[${BRANCH_NAME}] Update Makefile for ${BRANCH_NAME}" "Makefile" "${BRANCH_NAME}"; then
411-
handle_repo_error "${repo_name}" "Failed to commit and push Makefile changes" "$TEMP_DIR"
431+
if ! commit "[${BRANCH_NAME}] Update Makefile for ${BRANCH_NAME}" "Makefile"; then
432+
handle_repo_error "${repo_name}" "Failed to commit Makefile changes" "$TEMP_DIR"
412433
fi
413-
BRANCH_PUSHED=true
414434
else
415435
echo "No changes detected in Makefile."
416436
fi
@@ -438,14 +458,12 @@ jobs:
438458
handle_repo_error "${repo_name}" "Failed to update openstack-must-gather in ${DEFAULT_IMAGES_FILE}" "$TEMP_DIR"
439459
fi
440460

441-
# Check if there are changes to commit and push
461+
# Commit default_images.yaml changes if any
442462
if ! git diff --quiet -- "${DEFAULT_IMAGES_FILE}"; then
443463
echo "Changes detected in ${DEFAULT_IMAGES_FILE}"
444-
# FAIL if commit and push fails
445-
if ! commit_and_push "[${BRANCH_NAME}] Update default images for ${BRANCH_NAME}" "${DEFAULT_IMAGES_FILE}" "${BRANCH_NAME}"; then
446-
handle_repo_error "${repo_name}" "Failed to commit and push ${DEFAULT_IMAGES_FILE} changes" "$TEMP_DIR"
464+
if ! commit "[${BRANCH_NAME}] Update default images for ${BRANCH_NAME}" "${DEFAULT_IMAGES_FILE}"; then
465+
handle_repo_error "${repo_name}" "Failed to commit ${DEFAULT_IMAGES_FILE} changes" "$TEMP_DIR"
447466
fi
448-
BRANCH_PUSHED=true
449467
else
450468
echo "No changes detected in ${DEFAULT_IMAGES_FILE}."
451469
fi
@@ -454,14 +472,15 @@ jobs:
454472
fi
455473
fi
456474

457-
# Push the branch if it wasn't already pushed via commit_and_push
458-
if [ "$BRANCH_PUSHED" = "false" ]; then
459-
echo "Pushing branch ${BRANCH_NAME} without Makefile changes."
460-
if [ "$DRY_RUN" != "true" ]; then
461-
if ! retry_command 3 5 "Pushing new branch ${BRANCH_NAME}" git push origin ${BRANCH_NAME}; then
462-
handle_repo_error "${repo_name}" "Failed to push new branch after retries" "$TEMP_DIR"
463-
fi
475+
# Push the branch (with commits if any were made, or as empty branch)
476+
if [ "$DRY_RUN" != "true" ]; then
477+
echo "Pushing branch ${BRANCH_NAME}..."
478+
if ! retry_command 3 5 "Pushing to ${BRANCH_NAME}" git push origin ${BRANCH_NAME}; then
479+
handle_repo_error "${repo_name}" "Failed to push branch ${BRANCH_NAME}" "$TEMP_DIR"
464480
fi
481+
BRANCH_PUSHED=true
482+
else
483+
echo "DRY RUN: Would push branch ${BRANCH_NAME}"
465484
fi
466485

467486
# Track successful repository

0 commit comments

Comments
 (0)