Back to Posts

Setup a private Git Server with Automated CI/CD

Setup a private Git Server with Automated CI/CD

Need a self-hosted Git server? Whether you're working on private projects or collaborating over a local network, setting up a Git server on an external hard drive can be a simple and efficient solution. In this guide, we'll walk you through the process of:

1. Setting Up a Git Server on an External Hard Drive

First, plug in your external hard drive and determine its drive letter (e.g., E:\).

Option 1: Git Over SSH (Best for Security)

Install Git for Windows and enable OpenSSH Server:


    Windows Powershell
    Add-WindowsFeature -Name OpenSSH-Server
    Start-Service sshd
    Set-Service -Name sshd -StartupType Automatic

Create a bare Git repository on the external drive:


bash
cd /mnt/e/GitRepos
mkdir myproject.git
cd myproject.git
git init --bare

On a remote machine, add the Git server:


bash
git remote add origin ssh://your-username@your-windows-ip/e/GitRepos/myproject.git
git push origin main

Option 2: Git Over Network Share (Simpler, Less Secure)

  1. Right-click the E:\GitRepos folder, go to Properties → Sharing.
  2. Enable "Advanced Sharing" and allow Full Control for your user.
  3. On another PC, map the network drive and clone the repository:

bash
git clone \\YourWindowsPC\GitRepos\myproject.git
git push origin main

2. Automating Backups of Your Git Repositories

Using Windows Task Scheduler + Robocopy

Create a script called backup_git.bat:


bat
@echo off
robocopy E:\GitRepos F:\GitBackup /E /MIR /LOG:C:\backup_logs\git_backup.log /NFL /NDL

Schedule this script to run daily using Windows Task Scheduler.

3. Setting Up a Web Interface with Gitea

Download and install Gitea:


powershell
cd C:\Gitea
.\gitea.exe web

Gitea will now be available at http://localhost:3000. Set it up to manage repositories via a web interface.

4. Automating Deployments with Git Hooks

Inside your repository's hooks folder, create a post-receive script:


bash
#!/bin/bash
echo "Push received at $(date)" >> /mnt/e/GitRepos/logs/push.log
GIT_WORK_TREE=/var/www/myproject git checkout -f main

Make it executable:


bash
chmod +x post-receive

5. Setting Up CI/CD Pipelines

GitHub Actions (For Remote Repos)

Create a GitHub Actions workflow in .github/workflows/deploy.yml:


yaml
name: Deploy

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Deploy via SSH
        run: |
          ssh user@your-server 'cd /var/www/myproject && git pull origin main'

Jenkins (For Local CI/CD)

Install Jenkins and create a job that pulls changes from E:\GitRepos\myproject.git every 5 minutes.


bash
cd /var/www/myproject
git pull origin main

Conclusion

By following this guide, you've successfully transformed an external hard drive into a private Git server with automation features. Now, you can push, manage, and deploy your code seamlessly!

Back to Posts