git logo

1. Repository Setup in cPanel

  • Go to Files → Git Version Control in cPanel.
  • Choose Clone Repository and paste your GitHub/GitLab URL:
    Code
    https://github.com/yourusername/yourproject.git
    
  • Select the target directory (e.g., /home/username/repos/project).

👉 Pro Tip: Keep repositories outside public_html to avoid exposing .git directories publicly. Deploy into public_html via scripts instead.

2. Deployment with .cpanel.yml

cPanel looks for a .cpanel.yml file in the repo root. This file defines deployment tasks that run after each push.

Example for a PHP/Laravel app:

yaml
deployment:
  tasks:
    - export DEPLOYPATH=/home/username/public_html/
    - /bin/rsync -av --exclude='.git' --exclude='storage/' ./ $DEPLOYPATH
    - /usr/local/bin/composer install --no-dev --optimize-autoloader -d $DEPLOYPATH
    - /usr/local/bin/php $DEPLOYPATH/artisan migrate --force

What’s happening here:

  • rsync copies files into public_html while excluding sensitive directories.
  • composer install ensures dependencies are installed on the server.
  • artisan migrate runs database migrations automatically after deployment.

3. Branch-Specific Deployments

You can configure deployments to trigger only on certain branches. For example, staging vs production:

yaml
deployment:
  tasks:
    - if [ "$CPANEL_BRANCH" == "staging" ]; then
        export DEPLOYPATH=/home/username/staging/;
        /bin/rsync -av ./ $DEPLOYPATH;
      fi
    - if [ "$CPANEL_BRANCH" == "main" ]; then
        export DEPLOYPATH=/home/username/public_html/;
        /bin/rsync -av ./ $DEPLOYPATH;
      fi

👉 This lets you push to staging for testing, and main for production.

4. Custom Git Hooks

Beyond .cpanel.yml, you can edit hooks directly in:

Code
/home/username/repos/project/.git/hooks/

Examples:

  • pre-receive: Run tests before accepting commits.
  • post-receive: Trigger notifications (e.g., Slack webhook).

Sample post-receive hook:

bash
#!/bin/bash
DEPLOYPATH=/home/username/public_html/
git --work-tree=$DEPLOYPATH --git-dir=/home/username/repos/project checkout -f main
curl -X POST -H 'Content-type: application/json' \
     --data '{"text":"Deployment completed on production!"}' \
     https://hooks.slack.com/services/XXXX/YYYY/ZZZZ

5. CI/CD Integration

For advanced automation, connect GitHub Actions or GitLab CI to cPanel:

GitHub Actions workflow (.github/workflows/deploy.yml):

yaml
name: Deploy to cPanel
on:
  push:
    branches: [ "main" ]

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

      - name: Deploy via SSH
        uses: appleboy/[email protected]
        with:
          host: ${{ secrets.CPANEL_HOST }}
          username: ${{ secrets.CPANEL_USER }}
          key: ${{ secrets.CPANEL_SSH_KEY }}
          script: |
            cd repos/project
            git pull origin main
            /bin/rsync -av ./ /home/username/public_html/

👉 This ensures every push to main automatically updates your live site.

6. Best Practices

  • SSH Keys: Add your public key in cPanel → SSH Access for secure Git pulls.
  • Environment Variables: Use .env files for secrets, never commit them.
  • Rollback Strategy: Keep tagged releases (git tag v1.0) so you can quickly revert.
  • Monitoring: Check deployment logs in cPanel under Git Version Control → Manage.

🔑 Key Takeaway

For advanced users, cPanel’s Git integration isn’t just about version control — it’s a deployment automation platform. By combining .cpanel.yml, hooks, and external CI/CD pipelines, you can achieve professional-grade workflows even on shared hosting.

 

If you’re looking for more beginner level git help go here Git with cpanel for beginners

By staff