Skip to content

Instantly share code, notes, and snippets.

@mike-moreau
Last active January 24, 2025 21:34
Show Gist options
  • Save mike-moreau/0d566d6b8c4175f17cf5c02fc879f6bf to your computer and use it in GitHub Desktop.
Save mike-moreau/0d566d6b8c4175f17cf5c02fc879f6bf to your computer and use it in GitHub Desktop.
An example deployment script for Craft CMS projects not using a deployment service.
#!/bin/bash
# Run this script from the project root:
# Usage: deploy.sh <branch-name:string> [build-assets=true]
# Example - Deploy staging (with assets building by default): `bash ./deploy.sh staging`
# Example - Deploy main without building assets: `bash ./deploy.sh main false`
# Check if a branch name argument is provided
if [ $# -eq 0 ]; then
echo "Error: Branch name argument is required"
echo "Usage: $0 <branch-name>"
exit 1
fi
deploy_branch=${1:-main}
current_branch=$(git symbolic-ref --short HEAD 2>/dev/null)
build_assets=${2:-true}
if [ $? -ne 0 ]; then
echo 'Error: Not a git repository'
exit 1
fi
if [[ $current_branch == $deploy_branch ]]; then
echo "Deploying $deploy_branch branch"
else
echo "Error: Must be on $deploy_branch branch to proceed. Currently on: $current_branch"
exit 1
fi
echo 'Taking CMS offline'
php ./craft off
echo 'Clearing Cached Data'
php ./craft cache/flush-all
php ./craft clear-caches/all
# Optional Granular Cache Clearing (Modify to suit your project needs)
# php ./craft clear-caches/compiled-classes
# php ./craft clear-caches/compiled-templates
# php ./craft clear-caches/cp-resources
# php ./craft clear-caches/temp-files
# php ./craft clear-caches/data
# php ./craft clear-caches/retour-redirect-caches
# php ./craft clear-caches/seomatic-frontendtemplate-caches
# php ./craft clear-caches/seomatic-metabundle-caches
# php ./craft clear-caches/seomatic-schema-caches
# php ./craft clear-caches/seomatic-sitemap-caches
# php ./craft clear-caches/vite-file-cache
php ./craft clear-deprecations/index
echo 'Clearing Template Caches'
php ./craft invalidate-tags/all
echo "Pulling the $deploy_branch branch from the repo"
git pull origin $deploy_branch
echo 'Installing Composer Packages'
composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader
echo 'Updating Craft'
php ./craft up --interactive=0
if [[ $build_assets == true ]]; then
echo 'Building Assets'
npm ci
npm run build
fi
echo 'Taking Craft Back Online'
php ./craft on
echo 'Done!'
@mike-moreau
Copy link
Author

A note on cache clearing: Many plugin caches that can be cleared manually (SEOmatic, Retour, Vite) are Yii2 data caches that would be cleared by calling php ./craft cache/flush-all. While it would not be harmful, it's possible to introduce unnecessary redundant cache clearing.

@mike-moreau
Copy link
Author

Added support to check the current git branch before proceeding.

@mike-moreau
Copy link
Author

Added support for branch name and optional asset building.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment