Skip to main content

GitOps CLI and web server for Bitbucket operations

Project description

ngen-gitops

GitOps CLI and web server for Bitbucket operations with general git commands supporting multi-remote workflows. Automate branch creation, YAML image updates, pull request creation, merging, and general git operations via command-line interface or REST API.

PyPI version Python License: MIT

Features

GitOps Operations (Bitbucket)

  • ๐ŸŒฟ Branch Management: Create branches from source branches
  • ๐Ÿ–ผ๏ธ Image Updates: Update container images in Kubernetes YAML files
  • ๐Ÿ”„ Pull Requests: Create and merge pull requests automatically
  • ๐ŸŒ Web API: REST API server with FastAPI for integration

Git Commands (Multi-Remote)

  • ๐Ÿš€ Clone: Clone repositories from Bitbucket, GitHub, or GitLab
  • ๐Ÿ“ฅ Pull/Fetch: Pull or fetch changes from remote
  • ๐Ÿ“ค Push: Push changes to remote
  • ๐Ÿ’พ Commit: Commit changes with automatic add option
  • ๐Ÿ“Š Status: Check repository status
  • ๐Ÿ”€ Flexible Remotes: Default to Bitbucket, but works with GitHub and GitLab

General

  • ๐Ÿ” Secure: Uses app passwords for authentication
  • ๐Ÿ“ฆ Easy Configuration: Simple JSON config file
  • ๐Ÿš€ Dual Command: Use ngen-gitops or gitops command
  • ๐Ÿ’ป PyPI Package: Install with pip

Installation

pip install ngen-gitops

Both ngen-gitops and gitops commands will be available after installation.

Quick Start

1. Configuration

On first run, ngen-gitops creates a config file at ~/.ngen-gitops/.env:

# ngen-gitops Configuration

# Bitbucket Credentials
BITBUCKET_USER=
BITBUCKET_APP_PASSWORD=
BITBUCKET_ORG=loyaltoid

# Server Settings
SERVER_HOST=0.0.0.0
SERVER_PORT=8080

# Git Settings
GIT_DEFAULT_REMOTE=bitbucket.org
GIT_DEFAULT_ORG=loyaltoid

# Notifications
TEAMS_WEBHOOK=

Update the config with your credentials:

  1. Go to Bitbucket Settings โ†’ App passwords
  2. Create a new app password with repository read/write permissions
  3. Go to Bitbucket Settings โ†’ App passwords
  4. Create a new app password with repository read/write permissions
  5. Update ~/.ngen-gitops/.env with your username and app password

Or use environment variables:

export BITBUCKET_USER="your-username"
export BITBUCKET_APP_PASSWORD="your-app-password"
export BITBUCKET_ORG="your-organization"

Optional: Configure Teams Webhook for notifications:

export TEAMS_WEBHOOK="https://your-org.webhook.office.com/webhookb2/..."

Or add it to ~/.ngen-gitops/.env:

TEAMS_WEBHOOK="https://your-org.webhook.office.com/webhookb2/..."

When configured, you'll receive Microsoft Teams notifications for:

  • โœ… Branch creation
  • โœ… Image updates
  • โœ… Pull request creation
  • โœ… Pull request merging

2. Verify Configuration

gitops config

Output:

๐Ÿ“‹ ngen-gitops Configuration
   Config file: /home/user/.ngen-gitops/.env

Bitbucket:
   Username: your-username
   App Password: ***SET***
   Organization: loyaltoid

Server:
   Host: 0.0.0.0
   Port: 8080

Git:
   Default Remote: bitbucket.org
   Default Org: loyaltoid

Notifications:
   Teams Webhook: ***SET***

โœ… Credentials configured

Usage

Git Commands (Multi-Remote Support)

The gitops command provides general git operations with smart remote detection and support for Bitbucket, GitHub, and GitLab.

Clone Repository

Clone a repository with single-branch (default) or full clone:

# Clone single branch from Bitbucket (default)
gitops clone myrepo main

# Clone from GitHub
gitops clone myrepo main --remote github.com

# Clone from GitLab
gitops clone myrepo develop --remote gitlab.com

# Clone all branches (full clone)
gitops clone myrepo main --full

# Clone with custom org
gitops clone myrepo main --org myorg

# Clone with full repo path
gitops clone myorg/myrepo feature/test

# Clone to specific directory
gitops clone myrepo main --destination my-project

Example Output:

๐Ÿ”„ Cloning loyaltoid/myrepo from bitbucket.org...
   Branch/Tag: main
   Mode: Single branch
Cloning into 'myrepo'...
โœ… Successfully cloned to myrepo

โœ… Repository cloned successfully to: myrepo

Command Options:

  • repo: Repository name (e.g., myrepo or org/myrepo)
  • branch: Branch or tag to clone (optional)
  • --org: Organization name (defaults to config)
  • --remote: Remote type - bitbucket.org, github.com, gitlab.com (defaults to config)
  • --destination, -d: Destination directory (optional)
  • --full: Clone all branches instead of single branch (default: single-branch only)

Pull Changes

# Pull current branch
gitops pull

# Pull specific branch
gitops pull develop

# Pull in specific directory
gitops pull --cwd /path/to/repo

Push Changes

# Push current branch
gitops push

# Push specific branch
gitops push main

# Force push (use with caution)
gitops push --force

# Push in specific directory
gitops push --cwd /path/to/repo

Commit Changes

# Commit with message
gitops commit -m "Update configuration"

# Commit all changes (add + commit)
gitops commit -m "Update files" --all

# Commit in specific directory
gitops commit -m "Fix bug" --cwd /path/to/repo

Check Status

# Show status
gitops status

# Show status in specific directory
gitops status --cwd /path/to/repo

Fetch Changes

# Fetch from remote
gitops fetch

# Fetch in specific directory
gitops fetch --cwd /path/to/repo

GitOps Commands (Bitbucket Operations)

Create Branch

Create a new branch from a source branch:

gitops create-branch <repo> <src_branch> <dest_branch>

Example:

gitops create-branch my-app main feature/new-feature

Output:

๐Ÿ” Creating branch 'feature/new-feature' from 'main' in repository 'my-app'...
โœ… Source branch 'main' validated (commit: abc1234)
โœ… Branch 'feature/new-feature' created successfully from 'main'

โœ… Branch 'feature/new-feature' created successfully
   Branch URL: https://bitbucket.org/org/my-app/branch/feature/new-feature

Update Image in YAML

Update container image in Kubernetes YAML file:

gitops set-image-yaml <repo> <branch> <yaml_path> <image> [--dry-run]

Example:

gitops set-image-yaml my-app develop k8s/deployment.yaml myregistry/myapp:v1.2.3

Output:

๐Ÿ” Cloning repository my-app (branch: develop)...
โœ… Repository cloned
   Current image(s): myregistry/myapp:v1.2.0
   New image: myregistry/myapp:v1.2.3
โœ… Updated image in YAML file
โœ… Changes committed
โœ… Changes pushed to develop

โœ… Image updated to myregistry/myapp:v1.2.3 and pushed to develop
   [develop abc1234] chore: update image to myregistry/myapp:v1.2.3

Dry-run mode (don't push changes):

gitops set-image-yaml my-app develop k8s/deployment.yaml myapp:v1.0.0 --dry-run

Create Pull Request

Create a pull request from source to destination branch:

gitops pull-request <repo> <src_branch> <dest_branch> [--delete-after-merge]

Example:

gitops pull-request my-app feature/new-feature develop --delete-after-merge

Output:

๐Ÿ” Creating pull request from 'feature/new-feature' to 'develop' in repository 'my-app'...
   โš ๏ธ  Source branch 'feature/new-feature' will be deleted after merge
โœ… Source branch 'feature/new-feature' validated
โœ… Destination branch 'develop' validated
โœ… Pull request created successfully
   PR #42
   URL: https://bitbucket.org/org/my-app/pull-requests/42

โœ… Pull request #42 created successfully
   Pull Request URL: https://bitbucket.org/org/my-app/pull-requests/42

Merge Pull Request

Merge an existing pull request:

gitops merge <pr_url> [--delete-after-merge]

Example:

gitops merge https://bitbucket.org/org/my-app/pull-requests/42

Output:

๐Ÿ” Merging pull request #42 in repository 'my-app'...
โœ… Pull request validated
   Source branch: feature/new-feature
   Destination branch: develop
   State: OPEN
โœ… Pull request #42 merged successfully
   Merge commit: abc1234

โœ… Pull request #42 merged successfully
   Merge commit: abc1234

Kubernetes PR Workflow

Run a complete GitOps workflow: Create Branch -> Update Image -> Create PR -> Merge (optional).

gitops k8s-pr <cluster> <namespace> <deploy> <image> [--approve-merge] [--repo REPO]

Arguments:

  • cluster: Source branch (e.g., cluster name like k8s-cluster-1)
  • namespace: Kubernetes namespace
  • deploy: Deployment name
  • image: New image tag
  • --approve-merge: Automatically merge the PR if successful
  • --repo: Repository name (default: gitops-k8s)

Example:

gitops k8s-pr main my-ns my-app myregistry/app:v2 --approve-merge

Workflow Steps:

  1. Creates branch my-ns/my-app_deployment.yaml from main
  2. Updates image in my-ns/my-app_deployment.yaml file
  3. Creates PR from new branch to main
  4. Merges PR (if --approve-merge is set)

Start Web Server

Start the REST API server:

gitops server [--host HOST] [--port PORT]

Example:

gitops server --port 8080

Output:

๐Ÿš€ Starting ngen-gitops server...
   Host: 0.0.0.0
   Port: 8080
   Endpoints:
     - POST /v1/gitops/create-branch
     - POST /v1/gitops/set-image-yaml
     - POST /v1/gitops/pull-request
     - POST /v1/gitops/merge

INFO:     Started server process [12345]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)

JSON Output

GitOps commands support --json flag for machine-readable output:

gitops create-branch my-app main develop --json

Output:

{
  "success": true,
  "repository": "my-app",
  "source_branch": "main",
  "destination_branch": "develop",
  "message": "Branch 'develop' created successfully",
  "branch_url": "https://bitbucket.org/org/my-app/branch/develop"
}

REST API

Starting the Server

gitops server --port 8080

API Endpoints

1. Create Branch

Endpoint: POST /v1/gitops/create-branch

Request:

{
  "repo": "my-app",
  "src_branch": "main",
  "dest_branch": "feature/new-feature"
}

Response:

{
  "success": true,
  "repository": "my-app",
  "source_branch": "main",
  "destination_branch": "feature/new-feature",
  "message": "Branch 'feature/new-feature' created successfully",
  "branch_url": "https://bitbucket.org/org/my-app/branch/feature/new-feature"
}

cURL Example:

curl -X POST http://localhost:8080/v1/gitops/create-branch \
  -H "Content-Type: application/json" \
  -d '{
    "repo": "my-app",
    "src_branch": "main",
    "dest_branch": "feature/new-feature"
  }'

2. Update Image in YAML

Endpoint: POST /v1/gitops/set-image-yaml

Request:

{
  "repo": "my-app",
  "refs": "develop",
  "yaml_path": "k8s/deployment.yaml",
  "image": "myregistry/myapp:v1.2.3",
  "dry_run": false
}

Response:

{
  "success": true,
  "repository": "my-app",
  "branch": "develop",
  "yaml_path": "k8s/deployment.yaml",
  "image": "myregistry/myapp:v1.2.3",
  "message": "Image updated to myregistry/myapp:v1.2.3 and pushed to develop",
  "commit": "[develop abc1234] chore: update image to myregistry/myapp:v1.2.3"
}

cURL Example:

curl -X POST http://localhost:8080/v1/gitops/set-image-yaml \
  -H "Content-Type: application/json" \
  -d '{
    "repo": "my-app",
    "refs": "develop",
    "yaml_path": "k8s/deployment.yaml",
    "image": "myregistry/myapp:v1.2.3",
    "dry_run": false
  }'

3. Create Pull Request

Endpoint: POST /v1/gitops/pull-request

Request:

{
  "repo": "my-app",
  "src_branch": "feature/new-feature",
  "dest_branch": "develop",
  "delete_after_merge": false
}

Response:

{
  "success": true,
  "repository": "my-app",
  "source": "feature/new-feature",
  "destination": "develop",
  "delete_after_merge": false,
  "pr_id": 42,
  "pr_url": "https://bitbucket.org/org/my-app/pull-requests/42",
  "message": "Pull request #42 created successfully"
}

cURL Example:

curl -X POST http://localhost:8080/v1/gitops/pull-request \
  -H "Content-Type: application/json" \
  -d '{
    "repo": "my-app",
    "src_branch": "feature/new-feature",
    "dest_branch": "develop",
    "delete_after_merge": false
  }'

4. Merge Pull Request

Endpoint: POST /v1/gitops/merge

Request:

{
  "pr_url": "https://bitbucket.org/org/my-app/pull-requests/42",
  "delete_after_merge": false
}

Response:

{
  "success": true,
  "pr_url": "https://bitbucket.org/org/my-app/pull-requests/42",
  "repository": "my-app",
  "pr_id": "42",
  "source": "feature/new-feature",
  "destination": "develop",
  "message": "Pull request #42 merged successfully",
  "merge_commit": "abc1234",
  "delete_after_merge": false
}

cURL Example:

curl -X POST http://localhost:8080/v1/gitops/merge \
  -H "Content-Type: application/json" \
  -d '{
    "pr_url": "https://bitbucket.org/org/my-app/pull-requests/42",
    "delete_after_merge": false
  }'

5. Kubernetes PR Workflow

Endpoint: POST /v1/gitops/k8s-pr

Request:

{
  "cluster": "main",
  "namespace": "my-ns",
  "deploy": "my-app",
  "image": "myregistry/app:v2",
  "approve_merge": true,
  "repo": "gitops-k8s"
}

Response:

{
  "success": true,
  "steps": [
    {"name": "create_branch", "result": {...}},
    {"name": "set_image", "result": {...}},
    {"name": "create_pr", "result": {...}},
    {"name": "merge_pr", "result": {...}}
  ],
  "pr_url": "https://bitbucket.org/...",
  "message": "Workflow completed successfully (merged)"
}

API Documentation

When the server is running, visit:

  • Swagger UI: http://localhost:8080/docs
  • ReDoc: http://localhost:8080/redoc

Use Cases

Multi-Remote Git Workflow

Clone from different remotes:

# Clone from Bitbucket (default)
gitops clone my-app main

# Clone from GitHub
gitops clone my-company/app main --remote github.com

# Clone from GitLab
gitops clone group/project develop --remote gitlab.com

Work with cloned repo:

# Make changes
cd my-app
echo "update" >> README.md

# Commit and push
gitops commit -m "Update README" --all
gitops push

CI/CD Integration

Update image after Docker build:

# Build Docker image
docker build -t myregistry/myapp:${VERSION} .
docker push myregistry/myapp:${VERSION}

# Update Kubernetes deployment
gitops set-image-yaml my-app develop k8s/deployment.yaml myregistry/myapp:${VERSION}

Automated PR workflow:

# Create feature branch
gitops create-branch my-app develop feature/auto-update-${VERSION}

# Update image in feature branch
gitops set-image-yaml my-app feature/auto-update-${VERSION} k8s/deployment.yaml myregistry/myapp:${VERSION}

# Create pull request
PR_URL=$(gitops pull-request my-app feature/auto-update-${VERSION} develop --delete-after-merge --json | jq -r '.pr_url')

# Auto-merge (optional)
gitops merge $PR_URL

REST API Integration

Python example:

import requests

# Create branch
response = requests.post('http://localhost:8080/v1/gitops/create-branch', json={
    'repo': 'my-app',
    'src_branch': 'main',
    'dest_branch': 'feature/new-feature'
})
print(response.json())

# Update image
response = requests.post('http://localhost:8080/v1/gitops/set-image-yaml', json={
    'repo': 'my-app',
    'refs': 'develop',
    'yaml_path': 'k8s/deployment.yaml',
    'image': 'myapp:v1.0.0',
    'dry_run': False
})
print(response.json())

Configuration

Config File Location

Config File Location

~/.ngen-gitops/.env

Config Structure

The configuration uses standard environment variable format (key=value).

# Bitbucket
BITBUCKET_USER=your-username
BITBUCKET_APP_PASSWORD=your-app-password
BITBUCKET_ORG=your-org

# Server
SERVER_HOST=0.0.0.0
SERVER_PORT=8080

# Git
GIT_DEFAULT_REMOTE=bitbucket.org
GIT_DEFAULT_ORG=your-org

# Notifications
TEAMS_WEBHOOK=https://your-org.webhook.office.com/webhookb2/...

Configuration Options

Bitbucket Settings

  • BITBUCKET_USER: Your Bitbucket username
  • BITBUCKET_APP_PASSWORD: Bitbucket app password
  • BITBUCKET_ORG: Default organization

Server Settings

  • SERVER_HOST: Server bind address
  • SERVER_PORT: Server port

Git Settings

  • GIT_DEFAULT_REMOTE: Default git remote
  • GIT_DEFAULT_ORG: Default organization/user

Notifications Settings

  • TEAMS_WEBHOOK: Microsoft Teams webhook URL

Environment Variables

Override config with environment variables:

  • BITBUCKET_USER: Bitbucket username
  • BITBUCKET_APP_PASSWORD: Bitbucket app password
  • BITBUCKET_ORG: Bitbucket organization
  • TEAMS_WEBHOOK: Microsoft Teams webhook URL for notifications

Command Reference

GitOps Commands

Command Description
gitops create-branch Create a new branch from source branch
gitops set-image-yaml Update image in YAML file
gitops pull-request Create a pull request
gitops merge Merge a pull request
gitops k8s-pr Run complete K8s GitOps workflow
gitops server Start REST API server
gitops config Show configuration

Git Commands

Command Description
gitops clone Clone repository with multi-remote support
gitops pull Pull changes from remote
gitops push Push changes to remote
gitops fetch Fetch from remote
gitops commit Commit changes
gitops status Show git status

Common Flags

  • --json: Output as JSON (GitOps commands)
  • --dry-run: Preview changes without executing
  • --remote: Specify git remote (bitbucket.org, github.com, gitlab.com)
  • --org: Specify organization
  • --full: Clone all branches (instead of single-branch)
  • --force, -f: Force operation
  • --all, -a: Include all changes

Development

Build from Source

# Clone repository
git clone https://github.com/mamatnurahmat/ngen-gitops.git
cd ngen-gitops

# Install in development mode
pip install -e .

# Run commands
gitops --help

Build Package

# Use the build script
./build.sh

# Or use the build script with options
./build.sh --build-only    # Build only, don't publish
./build.sh --release        # Build and publish to PyPI

Publish to PyPI

# Update version in pyproject.toml and ngen_gitops/__init__.py

# Build and publish
./build.sh --release

Troubleshooting

Authentication Errors

Problem: Bitbucket credentials not configured

Solution:

  1. Check config file exists: ~/.ngen-gitops/.env
  2. Verify credentials are set correctly
  3. Or set environment variables: BITBUCKET_USER and BITBUCKET_APP_PASSWORD

Branch Not Found

Problem: Source branch 'xyz' not found

Solution:

  1. Verify branch name is correct (case-sensitive)
  2. Check branch exists in Bitbucket repository
  3. Use gitops status to check current branch

Git Clone Fails

Problem: git command not found or authentication fails

Solution:

  1. Ensure git is installed: git --version
  2. For private repos, ensure credentials are configured
  3. Check remote is correct: bitbucket.org, github.com, or gitlab.com

Image Update Fails

Problem: File 'k8s/deployment.yaml' not found

Solution:

  1. Verify YAML path is correct relative to repository root
  2. Check file exists in the specified branch
  3. Ensure YAML file contains image: field

License

MIT License - see LICENSE file for details

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Author

ngen-gitops contributors

Related Projects

  • ngen-j - Jenkins API management CLI

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ngen_gitops-0.1.9.tar.gz (28.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ngen_gitops-0.1.9-py3-none-any.whl (25.8 kB view details)

Uploaded Python 3

File details

Details for the file ngen_gitops-0.1.9.tar.gz.

File metadata

  • Download URL: ngen_gitops-0.1.9.tar.gz
  • Upload date:
  • Size: 28.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for ngen_gitops-0.1.9.tar.gz
Algorithm Hash digest
SHA256 6aaf0f50efa8cf92157d85c8c8b5cc4d679a673e07aed8ce7d0e2511a920732a
MD5 32672d06de94ea225cce2f2a98afb7a9
BLAKE2b-256 68ad9318358c84b975a977cee16611cb9b997106254ec70d3138fe14362d33c8

See more details on using hashes here.

File details

Details for the file ngen_gitops-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: ngen_gitops-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 25.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for ngen_gitops-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 ed1e865ea901fd1d7f29cc38efd6ecbd503210c2e2b68ce3f8780b1b260ec47a
MD5 3c1055cf53e9dba6fedc0fa27ecd5873
BLAKE2b-256 925f83fdeda88a45ecce3b9092661eb7da98df6f149b6e3b141693c7389e71d9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page