This commit is contained in:
Mark Randall Havens 2025-05-30 06:28:28 -05:00
parent f243ed4283
commit 69b0906bc0
6 changed files with 392 additions and 0 deletions

134
gitfield-gitlab Executable file
View file

@ -0,0 +1,134 @@
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
# ────────────────
# Config
# ────────────────
GIT_REMOTE_NAME="origin"
REPO_NAME=$(basename "$(pwd)")
DEFAULT_NAME="Mark Randall Havens"
DEFAULT_EMAIL="mark.r.havens@gmail.com"
GITLAB_WEB="https://gitlab.com"
GITLAB_API="$GITLAB_WEB/api/v4"
GITLAB_SSH="git@gitlab.com"
# ────────────────
# Logging
# ────────────────
info() { echo -e "\e[1;34m[INFO]\e[0m $*"; }
warn() { echo -e "\e[1;33m[WARN]\e[0m $*"; }
error() { echo -e "\e[1;31m[ERROR]\e[0m $*" >&2; exit 1; }
# ────────────────
# Git Identity
# ────────────────
git config --global user.name "$DEFAULT_NAME"
git config --global user.email "$DEFAULT_EMAIL"
info "Git identity set to: $DEFAULT_NAME <$DEFAULT_EMAIL>"
# ────────────────
# Git Repo Init
# ────────────────
if [ ! -d .git ]; then
info "Initializing Git repo..."
git init
git add . || warn "Nothing to add"
git commit -m "Initial commit" || warn "Nothing to commit"
else
info "Git repo already initialized."
fi
# ────────────────
# Ensure First Commit
# ────────────────
if ! git rev-parse HEAD &>/dev/null; then
git add . && git commit -m "Initial commit" || warn "Nothing to commit"
fi
# ────────────────
# SSH Key Setup
# ────────────────
if [ ! -f ~/.ssh/id_rsa ]; then
info "Generating SSH key..."
ssh-keygen -t rsa -b 4096 -C "$DEFAULT_EMAIL" -f ~/.ssh/id_rsa -N "" || error "Failed to generate SSH key"
fi
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa || error "Failed to add SSH key"
# ────────────────
# GitLab Token Prompt
# ────────────────
echo
echo "🔐 Paste your GitLab Personal Access Token (scopes: api, read_user, write_repository, write_ssh_key)"
echo "→ Generate at: $GITLAB_WEB/-/profile/personal_access_tokens"
read -rp "🔑 Token: " TOKEN
# ────────────────
# Get GitLab Username (via API)
# ────────────────
USERNAME=$(curl -s --header "PRIVATE-TOKEN: $TOKEN" "$GITLAB_API/user" | grep -oP '(?<="username":")[^"]*') || {
error "Failed to retrieve username — invalid token?"
}
info "GitLab username detected: $USERNAME"
# ────────────────
# Register SSH Key (if missing)
# ────────────────
if ! ssh -T "$GITLAB_SSH" 2>&1 | grep -q "Welcome"; then
PUBKEY=$(<~/.ssh/id_rsa.pub)
TITLE="AutoKey-$(hostname)-$(date +%s)"
info "Uploading SSH key to GitLab..."
curl -s --fail -X POST "$GITLAB_API/user/keys" \
-H "PRIVATE-TOKEN: $TOKEN" \
-H "Content-Type: application/json" \
-d "{\"title\": \"$TITLE\", \"key\": \"$PUBKEY\"}" || warn "SSH key may already exist or failed to upload"
sleep 2
fi
# ────────────────
# Create Repository (via API)
# ────────────────
REPO_API_URL="$GITLAB_API/projects"
REPO_PAYLOAD="{\"name\": \"$REPO_NAME\", \"visibility\": \"public\"}"
info "Creating GitLab repository '$REPO_NAME'..."
if curl -s --fail -X POST "$REPO_API_URL" \
-H "PRIVATE-TOKEN: $TOKEN" \
-H "Content-Type: application/json" \
-d "$REPO_PAYLOAD" | grep -q '"ssh_url_to_repo":'; then
info "Repository created successfully."
else
warn "Repository may already exist or creation failed — continuing..."
fi
# ────────────────
# Add SSH Remote
# ────────────────
REMOTE_URL="$GITLAB_SSH:$USERNAME/$REPO_NAME.git"
if ! git remote get-url "$GIT_REMOTE_NAME" &>/dev/null; then
git remote add "$GIT_REMOTE_NAME" "$REMOTE_URL"
info "SSH remote set to: $REMOTE_URL"
else
info "Remote already exists: $(git remote get-url "$GIT_REMOTE_NAME")"
fi
# ────────────────
# Commit + Push
# ────────────────
if ! git diff --quiet || ! git diff --cached --quiet; then
info "Committing local changes..."
git add . && git commit -m "Update: $(date '+%Y-%m-%d %H:%M:%S')" || warn "Nothing to commit"
else
info "No local changes to commit."
fi
BRANCH=$(git rev-parse --abbrev-ref HEAD)
if ! git config --get branch."$BRANCH".remote &>/dev/null; then
info "Pushing with upstream..."
git push -u "$GIT_REMOTE_NAME" "$BRANCH" || error "Push failed"
else
info "Pushing to $GIT_REMOTE_NAME/$BRANCH..."
git push "$GIT_REMOTE_NAME" "$BRANCH" || error "Push failed"
fi

View file

@ -0,0 +1,63 @@
### 📘 `1_prerequisites_gitlab_ubuntu.md`
````markdown
## 📘 `1_prerequisites_gitlab_ubuntu.md`
### 📌 Purpose
Prepare your Ubuntu system to create and work with remote GitLab repositories using SSH and CLI tools.
---
### ✅ System Requirements
* **Install Git**
```bash
sudo apt update
sudo apt install git -y
````
* **Create a GitLab account**
👉 [https://gitlab.com/users/sign\_up](https://gitlab.com/users/sign_up)
* **Set your Git identity**
```bash
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"
```
* **Generate an SSH key (if not already present)**
```bash
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
```
* **Add your SSH key to GitLab**
```bash
cat ~/.ssh/id_rsa.pub
```
🔗 Copy the output and paste it at:
GitLab → Preferences → SSH Keys → *Add key*
* **Test the connection**
```bash
ssh -T git@gitlab.com
```
✅ You should see something like:
> Welcome to GitLab, @your-username!
---
````
---

View file

@ -0,0 +1,73 @@
### 📘 `2_create_remote_repo_gitlab_ubuntu.md`
```markdown
## 📘 `2_create_remote_repo_gitlab_ubuntu.md`
### 📌 Purpose
Create a new GitLab repository and push your local Ubuntu project to it using the CLI.
---
### 🪐 Step-by-Step
#### Step 1: Install GitLab CLI
```bash
curl -s https://raw.githubusercontent.com/profclems/glab/trunk/scripts/install.sh | sudo bash
````
#### Step 2: Authenticate GitLab CLI
```bash
glab auth login
```
Choose:
* GitLab.com or custom instance
* Paste your **Personal Access Token** when prompted
---
#### Step 3: Initialize your project
```bash
mkdir myproject
cd myproject
git init
echo "# My Project" > README.md
git add .
git commit -m "Initial commit"
```
---
#### Step 4: Create GitLab repository via CLI
```bash
glab repo create myproject --visibility public --confirm
```
This:
* Creates the GitLab repo
* Links it to your local repo
* Adds `origin` remote
---
#### Step 5: Push to GitLab
```bash
git push -u origin master
```
✅ From now on, `git push` will work as expected.
---
````
---

View file

@ -0,0 +1,53 @@
### 📘 `3_commit_existing_repo_gitlab_ubuntu.md`
```markdown
## 📘 `3_commit_existing_repo_gitlab_ubuntu.md`
### 📌 Purpose
Work with an existing GitLab repo: clone, edit, commit, and push using Ubuntu.
---
### 🛠️ Step-by-Step
#### Step 1: Clone the repository
```bash
git clone git@gitlab.com:your-username/your-repo.git
cd your-repo
````
---
#### Step 2: Edit files
```bash
nano myfile.txt
```
---
#### Step 3: Stage and commit
```bash
git add .
git commit -m "Your change description"
```
---
#### Step 4: Push your changes
```bash
git push origin master
```
If you use another branch (e.g., `main`, `dev`), substitute accordingly.
---
````
---

View file

@ -0,0 +1,69 @@
### 📘 `CLI-ONLY_workflow_gitlab_ubuntu.md`
```markdown
## 📘 `CLI-ONLY_workflow_gitlab_ubuntu.md`
### 📌 Purpose
Set up, initialize, and push a GitLab repo using only the terminal — no browser required.
---
### 🪐 Step-by-Step CLI Workflow
#### 1. Install everything you need
```bash
sudo apt update
sudo apt install git curl -y
curl -s https://raw.githubusercontent.com/profclems/glab/trunk/scripts/install.sh | sudo bash
````
#### 2. Configure your Git identity
```bash
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"
```
#### 3. Authenticate with GitLab
```bash
glab auth login
```
Use **SSH** and paste your **Personal Access Token** (create one at [https://gitlab.com/-/profile/personal\_access\_tokens](https://gitlab.com/-/profile/personal_access_tokens))
---
#### 4. Initialize your project
```bash
mkdir myproject
cd myproject
git init
touch README.md
git add .
git commit -m "Initial commit"
```
#### 5. Create GitLab repo via CLI
```bash
glab repo create myproject --visibility public --confirm
```
#### 6. Push your changes
```bash
git push -u origin master
```
---
✅ Done. You've created and linked a GitLab repository entirely from the CLI.
```
---

0
test Normal file
View file