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

Add (optional) branch sync script for use by the Release Docs Team #2711

Merged
merged 2 commits into from
Jan 24, 2025
Merged
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
35 changes: 35 additions & 0 deletions release-team/role-handbooks/docs/branch-sync-script/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Kubernetes Release Docs Team - Branch Sync Script

Bash script to use to perform weekly branch syncs for kubernetes release team

## Description

Each week the release docs team needs to merge the `main` branch into `dev-[future release]` branch in order to avoid merge conflicts on release day. This script provides a user friendly execution of the branch sync commands from the [release docs handbook](https://github.com/kubernetes/sig-release/blob/b51237ddb556bb6d24e43d7424e55cb9e66694d5/release-team/role-handbooks/docs/Release-Timeline.md#maintain-the-current-and-upcoming-dev-branch)).

## Getting Started

### Installing

Download the script or clone this repo and add executable permissions to the script to allow it to run
```
chmod +x branch-sync.sh
```

## Usage
```
Usage: ./branch-sync.sh FUTURE_RELEASE [-p | --push] [-u | --user]
Options:
-p, --push Push the branch sync automatically
-u, --user Manually set github user
-h, --help Display this help message
```

### Example Start of Script

```
➜ ./branch-sync.sh 1.31 -u youruser
USER: youruser
FUTURE_RELEASE: 1.31
AUTO_PUSH: false
Do you want to proceed with these values? (y/n):
```
141 changes: 141 additions & 0 deletions release-team/role-handbooks/docs/branch-sync-script/branch-sync.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#!/bin/bash

# Initialize Variables
YOUR_USER=""
AUTO_PUSH=false
FUTURE_RELEASE=""

# Help Function
help() {
echo "Usage: $0 FUTURE_RELEASE [-p | --push] [-u | --user]"
echo "Options:"
echo " -p, --push Push the branch sync automatically without prompting"
echo " -u, --user Manually set github user"
echo " -h, --help Display this help message"
}

# Function to print commands in bold and colored text
print_command() {
echo -e "\033[1;34mExecuting: $1\033[0m"
}

# Function to execute a command and handle errors
execute() {
print_command "$1"
eval $1
if [ $? -ne 0 ]; then
echo -e "\033[1;31mError: Command failed - $1\033[0m"
exit 1
fi
}

# Parse command-line options
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-h|--help)
help
exit 0
;;
-p|--push)
AUTO_PUSH=true
shift
;;
-u|--user)
YOUR_USER="$2"
shift
shift
;;
*)
FUTURE_RELEASE="$key"
shift
;;
esac
done

# Ensure a future release argument is provided
if [ -z "$FUTURE_RELEASE" ]; then
help
exit 1
fi

# Ensure a github user is provided
if [ -z "$YOUR_USER" ]; then
echo "Error: Please provide your github user either through the -u option or directly adding it in the branch-sync.sh file"
help
exit 1
fi

# Display configuration and prompt user before execution
echo -e "USER: $YOUR_USER"
echo -e "FUTURE_RELEASE: $FUTURE_RELEASE"
echo -e "AUTO_PUSH: $AUTO_PUSH"

echo -e "\033[1;35mDo you want to proceed with these values? (y/n):\033[0m"
read response
if [[ "$response" =~ ^[Yy]$ ]]; then

# Print recieved arguments
print_command "FUTURE_RELEASE: $FUTURE_RELEASE"
print_command "AUTO_PUSH: $AUTO_PUSH"

# Clone the repository
execute "git clone [email protected]:$YOUR_USER/website.git"

# Change directory to the cloned repository
execute "cd website"

# Add the upstream remote and set the push URL to no_push
execute "git remote add upstream https://github.com/kubernetes/website.git"
execute "git remote set-url --push upstream no_push"

# Fetch the upstream main and future release branches
execute "git fetch upstream main"
execute "git fetch upstream dev-$FUTURE_RELEASE"

# Checkout the future release branch tracking upstream
execute "git checkout --track upstream/dev-$FUTURE_RELEASE"

# Ensure the branch is up-to-date
execute "git pull --ff-only"

# Merge the main branch from upstream, stopping on conflicts
print_command "git merge upstream/main -m "Merge main into dev-$FUTURE_RELEASE to keep in sync""
git merge upstream/main -m "Merge main into dev-$FUTURE_RELEASE to keep in sync"
if [ $? -ne 0 ]; then
echo -e "\033[1;31mError: Potentially a merge conflict has been detected.\033[0m"
echo -e "\033[1;32mIf there is a merge conflict, resolve the conflict\033[0m"
echo -e "\033[1;32m- More on resolving merge conflicts: https://help.github.com/articles/resolving-a-merge-conflict-using-the-command-line/\033[0m"
echo -e "\033[1;32m- e.g. git add ... and git commit -m "Merge main into dev-[future release] to keep in sync"\033[0m"
exit 1
fi

# Create a new branch for the merged changes
execute "git checkout -b merged-main-dev-$FUTURE_RELEASE"

# Push the new branch to the origin repository
if [ "$AUTO_PUSH" = true ]; then
execute "git push origin merged-main-dev-$FUTURE_RELEASE"
echo -e "\033[1;32mBranch Sync Pushed!\033[0m"
echo -e "\033[1;33mTo create the PR, go to your fork in a browser: https://www.github.com/{YOUR_USER}/website\033[0m"
echo -e "\033[1;33mSubmit a PR against upstream 'dev-$FUTURE_RELEASE' from your fork's branch 'merged-main-dev-$FUTURE_RELEASE' by changing the 'base' from 'main' to 'dev-[future-release]' when creating the PR.\033[0m"
echo -e "\033[1;33me.g. [Merge main into future release](https://github.com/kubernetes/website/pull/16225).\033[0m"
else
# Prompt to confirm before pushing to origin
echo -e "\033[1;35mDo you want to push the changes to origin? (y/n):\033[0m"
read response
if [[ "$response" =~ ^[Yy]$ ]]; then
execute "git push origin merged-main-dev-$FUTURE_RELEASE"
echo -e "\033[1;32mBranch Sync Pushed!\033[0m"
echo -e "\033[1;33mTo create the PR, go to your fork in a browser: https://www.github.com/{YOUR_USER}/website\033[0m"
echo -e "\033[1;33mSubmit a PR against upstream 'dev-$FUTURE_RELEASE' from your fork's branch 'merged-main-dev-$FUTURE_RELEASE' by changing the 'base' from 'main' to 'dev-[future-release]' when creating the PR.\033[0m"
echo -e "\033[1;33me.g. [Merge main into future release](https://github.com/kubernetes/website/pull/16225).\033[0m"
else
echo -e "\033[1;33mChanges were not pushed to origin.\n\033[0m"
echo -e "You can manually push using the following command:"
echo -e "\033[0;32mgit push origin merged-main-dev-$FUTURE_RELEASE\033[0m"
fi
fi
else
echo -e "\033[1;31mTerminating...\033[0m"
fi