Skip to content

Instantly share code, notes, and snippets.

@sarmadgulzar
Last active July 22, 2024 01:23
Show Gist options
  • Select an option

  • Save sarmadgulzar/f9792b2c82780f9a43af23dea34ebc56 to your computer and use it in GitHub Desktop.

Select an option

Save sarmadgulzar/f9792b2c82780f9a43af23dea34ebc56 to your computer and use it in GitHub Desktop.
Increment GitHub release with ease
#!/bin/bash
# Function to increment version
increment_version() {
local version=$1
local increment_type=${2:-patch}
IFS='.' read -ra version_parts <<< "$version"
case $increment_type in
major)
((version_parts[0]++))
version_parts[1]=0
version_parts[2]=0
;;
minor)
((version_parts[1]++))
version_parts[2]=0
;;
patch|*)
((version_parts[2]++))
;;
esac
echo "${version_parts[0]}.${version_parts[1]}.${version_parts[2]}"
}
# Get the increment type from argument or use default
increment_type=${1:-patch}
# Validate increment type
if [[ ! "$increment_type" =~ ^(major|minor|patch)$ ]]; then
echo "Invalid increment type. Please use 'major', 'minor', or 'patch'."
exit 1
fi
# Get the latest release version
latest_version=$(gh release list -L 1 | awk -F'\t' '{print $3}')
# Increment the version
new_version=$(increment_version "$latest_version" "$increment_type")
# Create the new release
gh release create "$new_version" -t "$new_version"
@sarmadgulzar
Copy link
Author

Version Increment and Release Script

This Bash script automates the process of incrementing version numbers and creating new releases using GitHub CLI. It supports patch, minor, and major version increments.

Prerequisites

  • Bash shell
  • GitHub CLI (gh) installed and authenticated

Installation

  1. Download the script to your local machine.
  2. Make the script executable:
    chmod +x increment_release.sh
    

Usage

Run the script with an optional argument to specify the increment type:

./increment_release.sh [increment_type]

Where [increment_type] can be:

  • patch (default): Increments the patch version (e.g., 1.0.0 to 1.0.1)
  • minor: Increments the minor version (e.g., 1.0.0 to 1.1.0)
  • major: Increments the major version (e.g., 1.0.0 to 2.0.0)

Examples

  1. Create a patch release (default):

    ./increment_release.sh
    
  2. Create a minor release:

    ./increment_release.sh minor
    
  3. Create a major release:

    ./increment_release.sh major
    

How It Works

  1. The script retrieves the latest release version from GitHub.
  2. It increments the version number based on the specified increment type.
  3. A new GitHub release is created with the new version number.

Notes

  • Ensure you have the necessary permissions to create releases in your GitHub repository.
  • The script assumes your releases follow semantic versioning (MAJOR.MINOR.PATCH).
  • Always review the new version before confirming the release creation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment