Skip to content

Instantly share code, notes, and snippets.

@ashutoshsahoo
Last active February 3, 2025 08:14
Show Gist options
  • Save ashutoshsahoo/836ffa6bcdf2baa7efa9953161221ab2 to your computer and use it in GitHub Desktop.
Save ashutoshsahoo/836ffa6bcdf2baa7efa9953161221ab2 to your computer and use it in GitHub Desktop.
Create Release Branch for a list of bitbucket URLs

Create Release Branch from Develop for Multiple Bitbucket Repositories

Overview

This script automates the process of creating a release branch from the develop branch for multiple Bitbucket repositories. It ensures that a new branch is only created if it does not already exist.

Features

  • Reads a list of Bitbucket repository URLs from a file.
  • Clones each repository (if not already cloned).
  • Fetches the latest changes from the remote repository.
  • Checks if the release branch already exists.
  • Creates and pushes the new branch only if it does not exist.

Shell Script: create_release_branch.sh

#!/bin/bash

# Configuration
RELEASE_BRANCH="release-$(date +%Y%m%d)"  # Change naming convention if needed
REPO_LIST="bitbucket_repos.txt"  # File containing the list of Bitbucket repo URLs
BASE_BRANCH="develop"

# Ensure the repo list file exists
if [[ ! -f "$REPO_LIST" ]]; then
    echo "Error: Repository list file '$REPO_LIST' not found."
    exit 1
fi

# Process each repository
while read -r REPO_URL; do
    if [[ -z "$REPO_URL" || "$REPO_URL" =~ ^# ]]; then
        continue
    fi

    # Extract repo name from URL
    REPO_NAME=$(basename -s .git "$REPO_URL")

    echo "Processing repository: $REPO_NAME"

    # Clone the repository if not already present
    if [[ ! -d "$REPO_NAME" ]]; then
        echo "Cloning repository..."
        git clone "$REPO_URL" "$REPO_NAME"
    fi

    # Change into repo directory
    cd "$REPO_NAME" || exit

    # Fetch latest updates
    git fetch --all

    # Check if the branch already exists remotely
    if git ls-remote --heads origin "$RELEASE_BRANCH" | grep -q "$RELEASE_BRANCH"; then
        echo "Branch '$RELEASE_BRANCH' already exists. Skipping..."
    else
        # Create the release branch from develop and push it
        echo "Creating and pushing release branch '$RELEASE_BRANCH'..."
        git checkout "$BASE_BRANCH"
        git pull origin "$BASE_BRANCH"
        git checkout -b "$RELEASE_BRANCH"
        git push origin "$RELEASE_BRANCH"
        echo "Branch '$RELEASE_BRANCH' created successfully."
    fi

    # Move back to parent directory
    cd ..

done < "$REPO_LIST"

echo "Script execution completed."

How to Use

Prepare the Repository List

  • Create a file named bitbucket_repos.txt and list Bitbucket repository URLs, one per line. Put a new line at the end.
# This is a list of Bitbucket repositories
https://bitbucket.org/team/repo1.git
# https://bitbucket.org/team/repo2.git (Disabled for now)
https://bitbucket.org/team/repo3.git

  • Make the Script Executable Run the following command to grant execution permissions:
chmod +x create_release_branch.sh
  • Run the Script Execute the script with:
./create_release_branch.sh

What This Script Does

  • Reads repository URLs from bitbucket_repos.txt.
  • Clones each repo if not already cloned.
  • Fetches the latest branches from the remote repository.
  • Checks if the release branch (e.g., release-20250203) already exists.
  • If the branch exists, it skips creating a new one.
  • If it doesn’t exist, it creates and pushes it.

Notes

  • The release branch name follows the pattern release-YYYYMMDD. Modify the RELEASE_BRANCH variable if needed.
  • If the repository is already cloned, it fetches the latest changes instead of cloning again.

How to Run the Script

  • Open a terminal.
  • Navigate to the directory where the script is saved.
  • Ensure you have the required permissions by running:
chmod +x create_release_branch.sh
  • Run the script:
./create_release_branch.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment