CI/CD Automation with Git, Bitbucket & Cron

Fri Sep 05 2025

🚀 CI/CD Automation with Git, Bitbucket & Cron

In this guide, we’ll implement a lightweight CI/CD pipeline using Git, Bitbucket, and cron jobs on a Linux server. This ensures that whenever developers push new code, it gets automatically deployed on the target server.


🔑 Step 1: Configure SSH Key for Bitbucket

  1. Check if an SSH key already exists:
ls -la /usr/local/apache/.ssh/
  1. If not found, generate a new key:
sudo -u techwithkr ssh-keygen -t rsa -b 4096 -C "bitbucket techwithkr"
  1. Add the SSH key to Bitbucket:
cat /usr/local/apache/.ssh/id_rsa.pub
  • Copy the key and go to Bitbucket → Personal Settings → SSH Keys → Add Key.
  1. Test connection:
ssh -T git@bitbucket.org

✅ Expected output:

authenticated via ssh key.
You can use git to connect to Bitbucket. Shell access is disabled

📦 Step 2: Setup Git Repository on Server

  1. Install Git:
sudo yum install git -y
  1. Navigate to project directory:
cd /pub.local/aj/prod/project/titleResolver/aj
  1. Initialize Git repo:
sudo -u techwithkr git init
  1. Set Git user info:
sudo -u techwithkr git config user.name "W3 Server"
sudo -u techwithkr git config user.email "techwithkr@techwithkr.com"
  1. Add Bitbucket remote:
sudo -u techwithkr git remote add origin git@bitbucket.org:chatbot-techwithkr/job-title-resolver-cj.git

👉 If multiple repos exist, ensure correct one is set.

  1. Check remote URL:
sudo -u techwithkr git remote -v
  1. Sync with remote branch:
sudo -u techwithkr git branch
sudo -u techwithkr git fetch --all
sudo -u techwithkr git checkout main
sudo -u techwithkr git pull origin main

⚙️ Step 3: Create Auto-Trigger Script

We’ll use a Bash script to detect changes in Bitbucket and automatically update code on the server.

📄 Script location: cicd.sh

Ensure we stay on main branch

sudo -u techwithkr git checkout main > /dev/null 2>&1

⏲ Step 4: Schedule with Cron

Set up a cron job to run the script every 15 minutes:

crontab -e

Add:

*/15 * * * * /bin/bash /opt/wdgtl-scripts/bitbucket/bitbucketpull-resolver-code.sh >> /opt/wdgtl-scripts/bitbucket/log-w3deploy/cron.log 2>&1

🛡 Step 5: Auto-Set File Permissions

Sometimes after pulling code, permissions need fixing. We can automate this with a Git hook.

  1. Go to hooks folder:
cd .git/hooks
  1. Create post-checkout hook:
sudo -u techwithkr vi post-checkout
  1. Add script:
#!/bin/bash
find . -type f -name "*.py" -exec chmod 755 {} \;
  1. Save and make it executable:
chmod 775 post-checkout

✅ Conclusion

With this setup:

  • Code is automatically pulled from Bitbucket
  • Logs track all updates
  • Cron ensures regular syncs
  • Permissions are auto-managed

This forms a simple yet powerful CI/CD automation pipeline for small to mid-scale projects without relying on external CI/CD tools.