Automatic deployment from Git

deploy, git, tasks, composer, rsync · 0 found this helpful

A deploy file at the top of your Git repository tells us what to run when you click Deploy. The file must be named .cpanel.yml, which is the name the deploy system looks for. Use it to copy files into public_html and run build steps.

Set up the repository first. See Deploy with Git.

Rules

  • File name is .cpanel.yml, in the repository root, committed to Git.
  • The working tree must be clean. Don't edit files on the server.
  • Each task runs as its own command, from the repository folder.
  • If you push straight to the repository on your account, it deploys automatically. If you pull from GitHub with Pull, click Deploy afterwards.

Full example

---
deployment:
  tasks:
    # Where the live site lives
    - export DEPLOYPATH=/home/yourusername/public_html/

    # Install PHP dependencies (needs composer on PATH)
    - /usr/local/bin/composer install --no-dev --optimize-autoloader --no-interaction

    # Copy site files, deleting files removed from the repo,
    # but leave uploads, config, and logs alone
    - /bin/rsync -av --delete
        --exclude='.git'
        --exclude='.cpanel.yml'
        --exclude='.env'
        --exclude='wp-config.php'
        --exclude='wp-content/uploads'
        --exclude='error_log'
        ./ $DEPLOYPATH

    # Set sensible permissions
    - /bin/find $DEPLOYPATH -type d -exec chmod 755 {} \;
    - /bin/find $DEPLOYPATH -type f -exec chmod 644 {} \;

    # Clear caches (WordPress)
    - /usr/local/bin/wp --path=$DEPLOYPATH cache flush
    - /usr/local/bin/wp --path=$DEPLOYPATH litespeed-purge all

On our servers composer and wp live in /usr/local/bin.

A minimal version for a static site

---
deployment:
  tasks:
    - export DEPLOYPATH=/home/yourusername/public_html/
    - /bin/cp -R public/* $DEPLOYPATH

Run it

  1. Commit and push .cpanel.yml.
  2. Open Account → Hosting → your site → Developer.
  3. Under Git repositories, click Pull next to the repository.
  4. Click Deploy. The button appears once the repository has a .cpanel.yml file.
  5. Check the time shown beside Last deploy. If a task failed, test the commands in SSH to see the error.

Tips

  • Test the commands in SSH first.
  • Use --dry-run with rsync on the first run: rsync -av --dry-run ....
  • Use absolute paths for every command.
  • Keep secrets in environment files outside the repository. See Set environment variables for apps.
  • Don't deploy node_modules. Build and commit the output, or build on your computer.

If something goes wrong

Problem Fix
"Deployment cannot proceed" Uncommitted changes on the server, or .cpanel.yml invalid
Command not found Use full paths
rsync: command not found Use cp -R instead
Task fails midway Later tasks don't run. Fix and redeploy
Site missing files Your rsync --delete removed files not in Git. Exclude them

Was this helpful?

Still stuck? Ask us and we'll walk you through it.