-
Notifications
You must be signed in to change notification settings - Fork 0
[LINUX] setup local repository
fourslickz edited this page May 27, 2025
·
1 revision
#!/bin/bash
# ===== Parameter Check =====
if [ $# -ne 2 ]; then
echo "Usage: $0 <REPO_NAME> <BRANCH_NAME>"
exit 1
fi
# ===== Input Parameters =====
REPO_NAME=$1
BRANCH_NAME=$2
# ===== Configurable Paths =====
REPO_SLUG=$(echo "$REPO_NAME" | tr '[:upper:]' '[:lower:]') # lowercase version
LOCAL_REPO_DIR="$HOME/cicd/$BRANCH_NAME"
GITHUB_URL="git@github.com:IT-RIN-AYOSATU/$REPO_NAME.git"
BARE_REPO_DIR="/home/pramuka/git/$BRANCH_NAME/${REPO_SLUG}.git"
DEPLOY_DIR="/var/www/html/$BRANCH_NAME/${REPO_SLUG}"
# ===== Clone from GitHub =====
mkdir -p "$LOCAL_REPO_DIR"
cd "$LOCAL_REPO_DIR" || exit 1
if [ ! -d "$REPO_NAME" ]; then
git clone -b "$BRANCH_NAME" "$GITHUB_URL"
fi
cd "$REPO_NAME" || exit 1
# ===== Add Remote for Bare Repo =====
git remote add bare-repo-"$BRANCH_NAME" "ssh://pramuka@localhost:2244$BARE_REPO_DIR"
# ===== Create Bare Repo =====
mkdir -p "$BARE_REPO_DIR"
cd "$BARE_REPO_DIR" || exit 1
git init --bare
# ===== Create Post-Receive Hook =====
tee hooks/post-receive > /dev/null <<EOF
#!/bin/sh
GIT_WORK_TREE=$DEPLOY_DIR git checkout $BRANCH_NAME -f
EOF
chmod 755 hooks/post-receive
# ===== Create Deployment Directory =====
mkdir -p "$DEPLOY_DIR"
# ==== Initialize ====
cd "$LOCAL_REPO_DIR"
cd "$REPO_NAME"
git push bare-repo-"$BRANCH_NAME" +"$BRANCH_NAME"
# ==== finish ====
echo "Setup for $REPO_NAME on branch $BRANCH_NAME completed successfully."
echo "Push to 'bare-repo-$BRANCH_NAME' remote to deploy."