Skip to content

Instantly share code, notes, and snippets.

@kalanakt
Last active May 27, 2024 06:43
Show Gist options
  • Select an option

  • Save kalanakt/8e48dd977ba34a0983bc7e6f1fa23062 to your computer and use it in GitHub Desktop.

Select an option

Save kalanakt/8e48dd977ba34a0983bc7e6f1fa23062 to your computer and use it in GitHub Desktop.
ship project to git and vercel in better way!

Ship Script Usage

Overview

The Ship script automates the process of updating the package version, staging changes, committing to Git, and deploying to Vercel.

Installation

  1. Save the script to a file named ship.sh in your project directory.
  2. Make the script executable:
    chmod +x ship.sh

Usage

Run the script with the following options:

  • To perform a patch version update, stage, commit, and push to Git, and deploy to Vercel with a specific message:

    ./ship.sh -m "Patch version update" --patch --git --vercel
  • To perform a minor version update, stage, commit, and push to Git with a specific message:

    ./ship.sh -m "Minor version update" --minor --git
  • To perform a major version update, deploy to Vercel, and specify a commit message:

    ./ship.sh -m "Major version update" --major --vercel
  • To perform all operations (patch version update, stage, commit, push to Git, and deploy to Vercel) with a specified message:

    ./ship.sh -m "All operations" --patch --git --vercel
  • To perform all operations with the default message:

    ./ship.sh --patch --git --vercel

Replace "Patch version update", "Minor version update", "Major version update", or "All operations" with your actual commit message.

#!/bin/bash
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
DATETIME=$(date +"%Y-%m-%d %T")
# Set default values
GIT=false
VERCEL=false
MESSAGE=""
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--patch|--minor|--major)
VERSION_TYPE="$1"
shift
;;
--git)
GIT=true
shift
;;
--vercel)
VERCEL=true
shift
;;
-m)
MESSAGE="$2"
shift 2
;;
*)
shift
;;
esac
done
# Get current version
CURRENT_VERSION=$(node -p "require('./package.json').version")
# Determine new version based on argument if provided
if [ -n "$VERSION_TYPE" ]; then
case "$VERSION_TYPE" in
--patch)
NEW_VERSION=$(npm version patch --force | sed 's/v//')
;;
--minor)
NEW_VERSION=$(npm version minor --force | sed 's/v//')
;;
--major)
NEW_VERSION=$(npm version major --force | sed 's/v//')
;;
esac
# Update version in package.json
if [ -n "$NEW_VERSION" ]; then
sed -i '' "s/${CURRENT_VERSION}/${NEW_VERSION}/" package.json
fi
fi
# Perform Git operations if enabled
if [ "$GIT" == true ]; then
echo -e "${GREEN}Adding all changes to the staging area...${NC}"
git add .
COMMIT_MESSAGE="changes in v ${NEW_VERSION}"
if [ -n "$MESSAGE" ]; then
COMMIT_MESSAGE="$MESSAGE : [$DATETIME]"
fi
echo -e "${GREEN}Committing changes with message: $COMMIT_MESSAGE${NC}"
git commit -m "$COMMIT_MESSAGE"
echo -e "${GREEN}Pushing changes to the remote repository...${NC}"
git push origin main
# Create a tag and push it to the remote repository
if [ -n "$NEW_VERSION" ]; then
echo -e "${GREEN}Creating GitHub tag for version ${NEW_VERSION}...${NC}"
git tag -a "v${NEW_VERSION}" -m "Version ${NEW_VERSION}"
git push origin "v${NEW_VERSION}"
fi
fi
# Perform Vercel deployment if enabled
if [ "$VERCEL" == true ]; then
echo -e "${GREEN}Deploying to production using Vercel...${NC}"
vercel --prod
fi
echo -e "${GREEN}Done!${NC}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment