Last active
May 26, 2026 07:44
-
-
Save isuke01/83fd05f1016d396b26a4b96bdd947d21 to your computer and use it in GitHub Desktop.
Automation for git release, will merge stage to main, ask for tag, create it and deploy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Check version | |
| if [ -z "$1" ]; then | |
| git fetch --tags >/dev/null 2>&1 | |
| LATEST_TAG="$(git tag --sort=-v:refname | head -n1)" | |
| if [[ -n "$LATEST_TAG" ]]; then | |
| echo "π Current latest tag: $LATEST_TAG" | |
| else | |
| echo "β οΈ No tags found in this repository." | |
| fi | |
| read -p "Version to release (eg. 1.2.3): " VERSION | |
| if [[ -z "$VERSION" ]]; then | |
| echo "β No version provided. Aborting." | |
| exit 1 | |
| fi | |
| else | |
| VERSION=$1 | |
| fi | |
| # Switch to stage and pull changes | |
| git checkout stage || exit 1 | |
| git pull origin stage || exit 1 | |
| # Check if main has commits that are not on stage | |
| if git rev-list --left-right stage...main | grep "^>"; then | |
| echo "β Warning: main is ahead stage!" | |
| read -p "Are you sure to proceed? (y/n) " CONFIRM | |
| if [ "$CONFIRM" != "y" ]; then | |
| echo "Canceled." | |
| exit 1 | |
| fi | |
| fi | |
| # Merge stage into main without auto commit | |
| git checkout main || exit 1 | |
| git pull origin main || exit 1 | |
| # Show diff commits between main and stage | |
| echo "" | |
| echo "ββββββββββββββββββββββββββββββββββββββ" | |
| echo "π Commits to release (stage β main):" | |
| echo "" | |
| git log --oneline main..stage | |
| echo "" | |
| echo "ββββββββββββββββββββββββββββββββββββββ" | |
| echo "" | |
| read -p "Proceed with release $VERSION? (y/n) " RELEASE_CONFIRM | |
| if [ "$RELEASE_CONFIRM" != "y" ]; then | |
| echo "Canceled. Switching back to stage." | |
| git checkout stage | |
| exit 1 | |
| fi | |
| # Merge stage into main without auto commit | |
| git merge stage --no-commit || exit 1 | |
| # Create a release tag | |
| git tag -a "$VERSION" -m "Release $VERSION" || exit 1 | |
| # Push changes | |
| git push origin main --follow-tags || exit 1 | |
| echo "β Release $VERSION has been deployed!" | |
| # 1. Add script to bin globally: | |
| # sudo mv release.sh /usr/local/bin/release | |
| # sudo chmod +x /usr/local/bin/release | |
| # Usage: release 1.2.3 | |
| # 2. Second option: Alis in .bashrc or .zshrc | |
| # BASH: | |
| # echo 'alias release="bash /path/to/release.sh"' >> ~/.bashrc | |
| # source ~/.bashrc | |
| # ZSH: | |
| # echo 'alias release="bash /path/to/release.sh"' >> ~/.zshrc | |
| # source ~/.zshrc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment