Update: 2025-05-30 08:45:28
This commit is contained in:
parent
187de2dc51
commit
1bf9f54c03
1 changed files with 101 additions and 49 deletions
|
@ -2,92 +2,144 @@
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
IFS=$'\n\t'
|
IFS=$'\n\t'
|
||||||
|
|
||||||
# ╭────────── Configuration ──────────╮
|
# ╭─────────────────────────────────────╮
|
||||||
|
# │ CONFIGURATION │
|
||||||
|
# ╰─────────────────────────────────────╯
|
||||||
|
BITBUCKET_USER="mrhavens"
|
||||||
|
BITBUCKET_WORKSPACE="thefoldwithin"
|
||||||
REMOTE_NAME="bitbucket"
|
REMOTE_NAME="bitbucket"
|
||||||
WORKSPACE="thefoldwithin"
|
|
||||||
REPO_NAME=$(basename "$(pwd)")
|
REPO_NAME=$(basename "$(pwd)")
|
||||||
USERNAME="mrhavens"
|
|
||||||
EMAIL="mark.r.havens@gmail.com"
|
EMAIL="mark.r.havens@gmail.com"
|
||||||
BB_API="https://api.bitbucket.org/2.0/repositories/$WORKSPACE/$REPO_NAME"
|
FULL_NAME="Mark Randall Havens"
|
||||||
CRED_FILE="$HOME/.bitbucket_app_password"
|
APP_PASS_FILE="$HOME/.bitbucket_app_password"
|
||||||
|
API_URL="https://api.bitbucket.org/2.0/repositories/$BITBUCKET_WORKSPACE/$REPO_NAME"
|
||||||
|
SSH_REMOTE="git@bitbucket.org:$BITBUCKET_WORKSPACE/$REPO_NAME.git"
|
||||||
|
|
||||||
# ╭────────── Logging Helpers ──────────╮
|
# ╭─────────────────────────────────────╮
|
||||||
|
# │ LOGGING UTILS │
|
||||||
|
# ╰─────────────────────────────────────╯
|
||||||
info() { echo -e "\e[1;34m[INFO]\e[0m $*"; }
|
info() { echo -e "\e[1;34m[INFO]\e[0m $*"; }
|
||||||
warn() { echo -e "\e[1;33m[WARN]\e[0m $*"; }
|
warn() { echo -e "\e[1;33m[WARN]\e[0m $*"; }
|
||||||
error() { echo -e "\e[1;31m[ERROR]\e[0m $*" >&2; exit 1; }
|
error() { echo -e "\e[1;31m[ERROR]\e[0m $*" >&2; exit 1; }
|
||||||
|
|
||||||
# ╭────────── Prerequisites ──────────╮
|
# ╭─────────────────────────────────────╮
|
||||||
|
# │ CHECK + INSTALL TOOLS │
|
||||||
|
# ╰─────────────────────────────────────╯
|
||||||
info "Checking prerequisites..."
|
info "Checking prerequisites..."
|
||||||
sudo apt update -qq
|
sudo apt update -qq
|
||||||
sudo apt install -y git curl openssh-client jq || error "Missing packages"
|
sudo apt install -y git curl jq openssh-client || error "Dependency install failed"
|
||||||
|
|
||||||
# ╭────────── Git Identity ──────────╮
|
# ╭─────────────────────────────────────╮
|
||||||
git config --global user.name "$USERNAME"
|
# │ GIT IDENTITY SETUP │
|
||||||
|
# ╰─────────────────────────────────────╯
|
||||||
|
git config --global user.name "$FULL_NAME"
|
||||||
git config --global user.email "$EMAIL"
|
git config --global user.email "$EMAIL"
|
||||||
info "Git identity: $USERNAME <$EMAIL>"
|
info "Git identity: $FULL_NAME <$EMAIL>"
|
||||||
|
|
||||||
# ╭────────── SSH Key Setup ──────────╮
|
# ╭─────────────────────────────────────╮
|
||||||
|
# │ SSH KEYGEN + AGENT │
|
||||||
|
# ╰─────────────────────────────────────╯
|
||||||
if [ ! -f ~/.ssh/id_rsa ]; then
|
if [ ! -f ~/.ssh/id_rsa ]; then
|
||||||
info "Generating SSH key..."
|
info "Generating new SSH key..."
|
||||||
ssh-keygen -t rsa -b 4096 -C "$EMAIL" -f ~/.ssh/id_rsa -N ""
|
ssh-keygen -t rsa -b 4096 -C "$EMAIL" -f ~/.ssh/id_rsa -N ""
|
||||||
fi
|
fi
|
||||||
|
|
||||||
eval "$(ssh-agent -s)"
|
eval "$(ssh-agent -s)"
|
||||||
ssh-add ~/.ssh/id_rsa
|
ssh-add ~/.ssh/id_rsa || error "Failed to add SSH key"
|
||||||
|
|
||||||
# ╭────────── Git Initialization ──────────╮
|
# Add Bitbucket to known_hosts to avoid prompt
|
||||||
|
ssh-keyscan -t rsa bitbucket.org >> ~/.ssh/known_hosts 2>/dev/null || true
|
||||||
|
|
||||||
|
# ╭─────────────────────────────────────╮
|
||||||
|
# │ SSH AUTH VERIFICATION │
|
||||||
|
# ╰─────────────────────────────────────╯
|
||||||
|
info "Verifying SSH access to Bitbucket..."
|
||||||
|
|
||||||
|
if ssh -T git@bitbucket.org 2>/dev/null; then
|
||||||
|
info "✓ SSH access to Bitbucket verified."
|
||||||
|
elif ssh -T git@bitbucket.org 2>&1 | grep -q "successfully authenticated"; then
|
||||||
|
info "✓ SSH authentication to Bitbucket accepted."
|
||||||
|
elif ssh -T git@bitbucket.org; [[ $? == 1 ]]; then
|
||||||
|
info "✓ SSH connected — Bitbucket returned expected code 1."
|
||||||
|
else
|
||||||
|
warn "❌ SSH key not authorized with Bitbucket."
|
||||||
|
echo "→ Visit: https://bitbucket.org/account/settings/ssh-keys/"
|
||||||
|
echo "→ Paste this key:"
|
||||||
|
echo
|
||||||
|
cat ~/.ssh/id_rsa.pub
|
||||||
|
echo
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# ╭─────────────────────────────────────╮
|
||||||
|
# │ BITBUCKET APP PASSWORD SETUP │
|
||||||
|
# ╰─────────────────────────────────────╯
|
||||||
|
if [ ! -f "$APP_PASS_FILE" ]; then
|
||||||
|
echo
|
||||||
|
echo "🔐 Create a Bitbucket App Password (repo read/write + SSH + webhooks)"
|
||||||
|
echo "→ https://bitbucket.org/account/settings/app-passwords/"
|
||||||
|
read -rsp "Enter Bitbucket App Password (input hidden): " APP_PASS
|
||||||
|
echo "$APP_PASS" > "$APP_PASS_FILE"
|
||||||
|
chmod 600 "$APP_PASS_FILE"
|
||||||
|
echo
|
||||||
|
info "App password saved at $APP_PASS_FILE"
|
||||||
|
fi
|
||||||
|
|
||||||
|
APP_PASS=$(<"$APP_PASS_FILE")
|
||||||
|
|
||||||
|
# ╭─────────────────────────────────────╮
|
||||||
|
# │ GIT INIT & COMMIT │
|
||||||
|
# ╰─────────────────────────────────────╯
|
||||||
if [ ! -d .git ]; then
|
if [ ! -d .git ]; then
|
||||||
info "Initializing git..."
|
info "Initializing Git repository..."
|
||||||
git init
|
git init
|
||||||
git add . || warn "Nothing to add"
|
git add . || warn "Nothing to add"
|
||||||
git commit -m "Initial commit" || warn "Nothing to commit"
|
git commit -m "Initial commit" || warn "Nothing to commit"
|
||||||
else
|
else
|
||||||
info "Git repository already exists."
|
info "Git repo already initialized."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# ╭────────── Bitbucket Credential ──────────╮
|
# ╭─────────────────────────────────────╮
|
||||||
if [ ! -f "$CRED_FILE" ]; then
|
# │ CREATE REMOTE IF NOT EXISTS │
|
||||||
echo
|
# ╰─────────────────────────────────────╯
|
||||||
echo "🔐 Please paste your Bitbucket App Password (scopes: Repositories + SSH)"
|
REPO_EXISTS=$(curl -s -u "$BITBUCKET_USER:$APP_PASS" "$API_URL" | jq -r '.name // empty')
|
||||||
echo "→ Create one at: https://bitbucket.org/account/settings/app-passwords/"
|
if [ -z "$REPO_EXISTS" ]; then
|
||||||
read -rsp "🔑 App Password: " BB_PASSWORD
|
|
||||||
echo "$BB_PASSWORD" > "$CRED_FILE"
|
|
||||||
chmod 600 "$CRED_FILE"
|
|
||||||
echo
|
|
||||||
info "Stored app password in $CRED_FILE"
|
|
||||||
fi
|
|
||||||
|
|
||||||
BB_PASSWORD=$(<"$CRED_FILE")
|
|
||||||
|
|
||||||
# ╭────────── Create Repo via API ──────────╮
|
|
||||||
if ! curl -s -u "$USERNAME:$BB_PASSWORD" "$BB_API" | grep -q '"slug":'; then
|
|
||||||
info "Creating Bitbucket repository '$REPO_NAME'..."
|
info "Creating Bitbucket repository '$REPO_NAME'..."
|
||||||
curl -s -X POST -u "$USERNAME:$BB_PASSWORD" "$BB_API" \
|
curl -s -u "$BITBUCKET_USER:$APP_PASS" -X POST "$API_URL" \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d "{\"scm\": \"git\", \"is_private\": false}" | jq .
|
-d "{\"scm\": \"git\", \"is_private\": false, \"project\": {\"key\": \"~$BITBUCKET_USER\"}}" \
|
||||||
|
| grep -q '"uuid"' || warn "Repo may already exist or creation failed"
|
||||||
else
|
else
|
||||||
warn "Repo may already exist or is already accessible"
|
info "Remote Bitbucket repo already exists."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# ╭────────── Set Git Remote ──────────╮
|
# ╭─────────────────────────────────────╮
|
||||||
REMOTE_URL="git@bitbucket.org:$WORKSPACE/$REPO_NAME.git"
|
# │ SET GIT REMOTE │
|
||||||
if git remote get-url "$REMOTE_NAME" &>/dev/null; then
|
# ╰─────────────────────────────────────╯
|
||||||
info "Remote '$REMOTE_NAME' already set to: $(git remote get-url "$REMOTE_NAME")"
|
if ! git remote get-url "$REMOTE_NAME" &>/dev/null; then
|
||||||
|
git remote add "$REMOTE_NAME" "$SSH_REMOTE"
|
||||||
|
info "Remote set: $SSH_REMOTE"
|
||||||
else
|
else
|
||||||
git remote add "$REMOTE_NAME" "$REMOTE_URL"
|
info "Remote already set: $(git remote get-url "$REMOTE_NAME")"
|
||||||
info "Remote added: $REMOTE_URL"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# ╭────────── Commit + Push Logic ──────────╮
|
# ╭─────────────────────────────────────╮
|
||||||
|
# │ COMMIT + PUSH LOGIC │
|
||||||
|
# ╰─────────────────────────────────────╯
|
||||||
|
BRANCH=$(git rev-parse --abbrev-ref HEAD)
|
||||||
|
|
||||||
if ! git diff --quiet || ! git diff --cached --quiet; then
|
if ! git diff --quiet || ! git diff --cached --quiet; then
|
||||||
git add .
|
git add . && git commit -m "Update: $(date '+%Y-%m-%d %H:%M:%S')" || warn "Nothing to commit"
|
||||||
git commit -m "Update: $(date '+%Y-%m-%d %H:%M:%S')" || warn "Nothing to commit"
|
|
||||||
else
|
else
|
||||||
info "No uncommitted changes."
|
info "No uncommitted changes."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
BRANCH=$(git rev-parse --abbrev-ref HEAD)
|
if ! git config --get branch."$BRANCH".remote &>/dev/null; then
|
||||||
info "Pushing to '$REMOTE_NAME/$BRANCH'..."
|
info "Pushing with upstream..."
|
||||||
git push -u "$REMOTE_NAME" "$BRANCH" || error "Push failed"
|
git push -u "$REMOTE_NAME" "$BRANCH" || error "Push failed"
|
||||||
|
else
|
||||||
|
info "Pushing to $REMOTE_NAME/$BRANCH..."
|
||||||
|
git push "$REMOTE_NAME" "$BRANCH" || error "Push failed"
|
||||||
|
fi
|
||||||
|
|
||||||
info "✅ Bitbucket push completed successfully."
|
info "✅ Bitbucket push complete."
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue