如何自动推送本地仓库代码至远程Bitbucket?含Java代码自动推送场景
Hey there! Let's walk through how to solve both of your automation needs—they're straightforward with a bit of Git scripting and Linux tools.
1. 实现本地仓库代码自动推送至远程Bitbucket仓库
First things first, make sure your local repo is already linked to your Bitbucket remote. If not, run this to set it up (replace placeholders with your actual Bitbucket details):
git remote add origin git@bitbucket.org:your-username/your-repo-name.git
Key Prep: Set up SSH Key Authentication
To avoid entering passwords every time you push, add your Linux machine's SSH public key to your Bitbucket account:
- Generate a key if you don't have one:
ssh-keygen -t ed25519 -C "your-email@example.com"(just press enter through all prompts) - Copy the public key:
cat ~/.ssh/id_ed25519.pub - Paste it into Bitbucket under Settings > SSH keys
Create an Auto-Push Script
Make a bash script (e.g., auto-push-local.sh) that checks for changes, commits, and pushes automatically:
#!/bin/bash # Navigate to your local repo directory cd /path/to/your/local/repo # Check if there are any uncommitted changes if git status --porcelain | grep -q .; then # Stage all changes git add . # Commit with a timestamped message git commit -m "Auto-push: Local changes synced at $(date '+%Y-%m-%d %H:%M:%S')" # Push to Bitbucket remote git push origin main else echo "No changes to push at $(date '+%Y-%m-%d %H:%M:%S')" fi
Make the Script Executable & Schedule It
- Give execution permissions:
chmod +x auto-push-local.sh - To run it on a schedule (e.g., every 10 minutes), edit your crontab:
Add this line (replace the path with your script's actual location):crontab -e
This logs output to a file so you can debug if something goes wrong.*/10 * * * * /path/to/auto-push-local.sh >> /var/log/auto-push.log 2>&1
2. 自动推送Linux下生成的、文件夹名称不同的Java代码到Bitbucket
For this scenario, we'll create a script that finds your newly generated Java folders, adds them to your Bitbucket repo, and pushes. Let's assume your generated code lives in a directory like /home/you/generated-java, and your Bitbucket local repo is /home/you/bitbucket-java-repo.
Create the Auto-Push Script for Generated Code
Make a script (e.g., auto-push-generated-java.sh):
#!/bin/bash # Define paths GENERATED_DIR="/home/you/generated-java" REPO_DIR="/home/you/bitbucket-java-repo" # Find all new generated folders (adjust the pattern to match your folder naming) # Example: folders starting with "java-gen-" created in the last 1 hour NEW_FOLDERS=$(find "$GENERATED_DIR" -type d -name "java-gen-*" -mmin -60) if [ -n "$NEW_FOLDERS" ]; then # Copy new folders to the Bitbucket repo (use `mv` instead of `cp` if you don't need originals) cp -r $NEW_FOLDERS "$REPO_DIR/" # Navigate to repo and push changes cd "$REPO_DIR" git add . git commit -m "Auto-push: New generated Java code from $(date '+%Y-%m-%d %H:%M:%S')" git push origin main # Optional: Clean up the original generated folders after pushing # rm -r $NEW_FOLDERS else echo "No new generated Java folders found at $(date '+%Y-%m-%d %H:%M:%S')" fi
Customize & Schedule the Script
- Adjust the
findcommand to match your actual folder naming pattern and time window (e.g.,-mtime -1for folders created in the last 24 hours). - Make it executable:
chmod +x auto-push-generated-java.sh - Schedule it via crontab (e.g., run every hour):
Add:crontab -e0 * * * * /path/to/auto-push-generated-java.sh >> /var/log/auto-push-java.log 2>&1 - Alternative: If your code generation task runs on demand, you can call this script directly at the end of that task (add
/path/to/auto-push-generated-java.shto the end of your code generation script) instead of using crontab.
内容的提问来源于stack exchange,提问作者Saranya Unni

