Update: 2025-05-30 09:23:54

This commit is contained in:
Mark Randall Havens 2025-05-30 09:23:54 -05:00
parent f8a98f20ac
commit d7afcfb997

View file

@ -14,13 +14,14 @@ FULL_NAME="Mark Randall Havens"
APP_PASS_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" API_URL="https://api.bitbucket.org/2.0/repositories/$BITBUCKET_WORKSPACE/$REPO_NAME"
SSH_REMOTE="git@bitbucket.org:$BITBUCKET_WORKSPACE/$REPO_NAME.git" SSH_REMOTE="git@bitbucket.org:$BITBUCKET_WORKSPACE/$REPO_NAME.git"
WEB_LINK="https://bitbucket.org/$BITBUCKET_WORKSPACE/$REPO_NAME"
# ╭─────────────────────────────────────╮ # ╭─────────────────────────────────────╮
# │ LOGGING UTILS │ # │ LOGGING UTILS │
# ╰─────────────────────────────────────╯ # ╰─────────────────────────────────────╯
info() { echo -e "\e[1;34m[INFO]\e[0m $*"; } info() { echo -e "\n\e[1;34m[INFO]\e[0m $*"; }
warn() { echo -e "\e[1;33m[WARN]\e[0m $*"; } warn() { echo -e "\n\e[1;33m[WARN]\e[0m $*"; }
error() { echo -e "\e[1;31m[ERROR]\e[0m $*" >&2; exit 1; } error() { echo -e "\n\e[1;31m[ERROR]\e[0m $*" >&2; exit 1; }
# ╭─────────────────────────────────────╮ # ╭─────────────────────────────────────╮
# │ CHECK + INSTALL TOOLS │ # │ CHECK + INSTALL TOOLS │
@ -46,15 +47,12 @@ fi
eval "$(ssh-agent -s)" eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa || error "Failed to add SSH key" ssh-add ~/.ssh/id_rsa || error "Failed to add SSH key"
# Add Bitbucket to known_hosts to avoid prompt
ssh-keyscan -t rsa bitbucket.org >> ~/.ssh/known_hosts 2>/dev/null || true ssh-keyscan -t rsa bitbucket.org >> ~/.ssh/known_hosts 2>/dev/null || true
# ╭─────────────────────────────────────╮ # ╭─────────────────────────────────────╮
# │ SSH AUTH VERIFICATION │ # │ SSH AUTH VERIFICATION │
# ╰─────────────────────────────────────╯ # ╰─────────────────────────────────────╯
info "Verifying SSH access to Bitbucket..." info "Verifying SSH access to Bitbucket..."
if ssh -T git@bitbucket.org 2>&1 | grep -q "authenticated"; then if ssh -T git@bitbucket.org 2>&1 | grep -q "authenticated"; then
info "✓ SSH access to Bitbucket verified." info "✓ SSH access to Bitbucket verified."
else else
@ -72,7 +70,7 @@ fi
# ╰─────────────────────────────────────╯ # ╰─────────────────────────────────────╯
if [ ! -f "$APP_PASS_FILE" ]; then if [ ! -f "$APP_PASS_FILE" ]; then
echo echo
echo "🔐 Create a Bitbucket App Password (repo read/write + SSH + webhooks)" echo "🔐 Create a Bitbucket App Password (repo:admin + write + webhook)"
echo "→ https://bitbucket.org/account/settings/app-passwords/" echo "→ https://bitbucket.org/account/settings/app-passwords/"
read -rsp "Enter Bitbucket App Password (input hidden): " APP_PASS read -rsp "Enter Bitbucket App Password (input hidden): " APP_PASS
echo "$APP_PASS" > "$APP_PASS_FILE" echo "$APP_PASS" > "$APP_PASS_FILE"
@ -92,14 +90,13 @@ if [ ! -d .git ]; then
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 repo already initialized." info "Git repo already initialized."
fi fi
# ╭─────────────────────────────────────╮ # ╭─────────────────────────────────────╮
# │ CREATE REMOTE IF NOT EXISTS │ # │ CREATE REMOTE IF NOT EXISTS │
# ╰─────────────────────────────────────╯ # ╰─────────────────────────────────────╯
REPO_EXISTS=$(curl -s -u "$BITBUCKET_USER:$APP_PASS" "$API_URL" | jq -r '.name // empty') REPO_EXISTS=$(curl -s -u "$BITBUCKET_USER:$APP_PASS" "$API_URL" | jq -r '.name // empty')
if [ -z "$REPO_EXISTS" ]; then if [ -z "$REPO_EXISTS" ]; then
info "Creating Bitbucket repository '$REPO_NAME'..." info "Creating Bitbucket repository '$REPO_NAME'..."
CREATE_RESPONSE=$(curl -s -w "%{http_code}" -o /tmp/create_resp.txt -u "$BITBUCKET_USER:$APP_PASS" -X POST "$API_URL" \ CREATE_RESPONSE=$(curl -s -w "%{http_code}" -o /tmp/create_resp.txt -u "$BITBUCKET_USER:$APP_PASS" -X POST "$API_URL" \
@ -119,7 +116,6 @@ fi
# ╰─────────────────────────────────────╯ # ╰─────────────────────────────────────╯
EXPECTED_REMOTE="$SSH_REMOTE" EXPECTED_REMOTE="$SSH_REMOTE"
CURRENT_REMOTE=$(git remote get-url "$REMOTE_NAME" 2>/dev/null || echo "") CURRENT_REMOTE=$(git remote get-url "$REMOTE_NAME" 2>/dev/null || echo "")
if [[ "$CURRENT_REMOTE" != "$EXPECTED_REMOTE" ]]; then if [[ "$CURRENT_REMOTE" != "$EXPECTED_REMOTE" ]]; then
if [ -n "$CURRENT_REMOTE" ]; then if [ -n "$CURRENT_REMOTE" ]; then
warn "Removing incorrect remote: $CURRENT_REMOTE" warn "Removing incorrect remote: $CURRENT_REMOTE"
@ -135,7 +131,6 @@ fi
# │ COMMIT + PUSH LOGIC │ # │ COMMIT + PUSH LOGIC │
# ╰─────────────────────────────────────╯ # ╰─────────────────────────────────────╯
BRANCH=$(git rev-parse --abbrev-ref HEAD) 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 commit -m "Update: $(date '+%Y-%m-%d %H:%M:%S')" || warn "Nothing to commit" git add . && git commit -m "Update: $(date '+%Y-%m-%d %H:%M:%S')" || warn "Nothing to commit"
else else
@ -150,4 +145,8 @@ else
git push "$REMOTE_NAME" "$BRANCH" || error "Push failed" git push "$REMOTE_NAME" "$BRANCH" || error "Push failed"
fi fi
# ╭─────────────────────────────────────╮
# │ FINAL LINK OUTPUT │
# ╰─────────────────────────────────────╯
info "✅ Bitbucket push complete." info "✅ Bitbucket push complete."
echo -e "\n🔗 View in browser: $WEB_LINK\n"