Skip to content

Instantly share code, notes, and snippets.

@haffla
Created January 5, 2026 11:57
Show Gist options
  • Select an option

  • Save haffla/798aebe1bba8800604fa9ca3caaa7f47 to your computer and use it in GitHub Desktop.

Select an option

Save haffla/798aebe1bba8800604fa9ca3caaa7f47 to your computer and use it in GitHub Desktop.
Update the Ruby version in a project.
#!/bin/bash
################################################################################
# Ruby Version Update Script
################################################################################
#
# Description:
# This script automates the process of updating Ruby to a new version across
# the project. It updates configuration files, runs bundle install, creates
# a git branch, commits changes, and opens a pull request.
#
# Usage:
# ./update-ruby.sh <ruby-version>
#
# Example:
# ./update-ruby.sh 3.2.1
#
# Prerequisites and Assumptions:
# - .ruby-version file must exist in the project root
# - Dockerfile must exist in the project root with an ARG RUBY_VERSION line
# - gh (GitHub CLI) command must be installed and authenticated
# - Git repository must be initialized
# - Bundler must be installed
# - User must have permissions to create branches and push to the repository
#
# What the script does:
# 1. Updates the .ruby-version file with the new version
# 2. Updates the RUBY_VERSION argument in the Dockerfile
# 3. Runs bundle install to update dependencies
# 4. Runs bundle update --bundler to update bundler version
# 5. Creates a new git branch named "upgrade-ruby-to-<version>"
# 6. Commits the changes (Dockerfile, .ruby-version, Gemfile.lock)
# 7. Pushes the branch to GitHub
# 8. Creates a pull request using gh CLI
#
################################################################################
# if [ -n "$(git status --porcelain)" ]; then
# echo "Please commit or stash your changes before running this script."
# exit 1
# fi
# Ensure the script is executed with a version argument
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <ruby-version>"
exit 1
fi
# The new Ruby version passed as an argument
NEW_VERSION="$1"
BRANCH_NAME="upgrade-ruby-to-$NEW_VERSION"
# Update the .ruby-version file
if [ -f ".ruby-version" ]; then
echo "Updating .ruby-version to $NEW_VERSION"
echo "$NEW_VERSION" >.ruby-version
else
echo ".ruby-version file not found!"
exit 1
fi
# Update the Dockerfile
if [ -f "Dockerfile" ]; then
echo "Updating Ruby version in Dockerfile to $NEW_VERSION"
sed -i.bak -E "s/ARG RUBY_VERSION=[0-9]+\.[0-9]+\.[0-9]+/ARG RUBY_VERSION=$NEW_VERSION/" Dockerfile
if [ $? -eq 0 ]; then
echo "Dockerfile updated successfully."
rm Dockerfile.bak # Remove the backup file created by sed
else
echo "Failed to update Dockerfile."
exit 1
fi
else
echo "Dockerfile not found!"
exit 1
fi
# Run bundle install
echo "Running bundle install"
bundle install
if [ $? -ne 0 ]; then
echo "Bundle install failed."
exit 1
fi
# Update bundler
echo "Running bundle update --bundler"
bundle update --bundler
if [ $? -ne 0 ]; then
echo "Bundle update --bundler failed."
exit 1
fi
# Create a new branch for the changes
echo "Creating new branch $BRANCH_NAME"
git checkout -b "$BRANCH_NAME"
if [ $? -ne 0 ]; then
echo "Failed to create and switch to branch $BRANCH_NAME."
exit 1
fi
# Commit the changes
echo "Committing changes"
git add Dockerfile .ruby-version Gemfile.lock
git commit -m "Update Ruby to version $NEW_VERSION"
if [ $? -ne 0 ]; then
echo "Git commit failed."
exit 1
fi
# Push the branch to GitHub
echo "Pushing branch $BRANCH_NAME to GitHub"
git push origin "$BRANCH_NAME"
if [ $? -ne 0 ]; then
echo "Failed to push branch $BRANCH_NAME to GitHub."
exit 1
fi
# Create a pull request with the GitHub CLI
PR_TITLE="Update Ruby to $NEW_VERSION"
echo "Creating GitHub pull request with title: $PR_TITLE"
gh pr create --title "$PR_TITLE" --fill
if [ $? -ne 0 ]; then
echo "Failed to create pull request."
exit 1
fi
echo "Ruby upgrade to version $NEW_VERSION completed and pull request created successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment