Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dennisfrank/8493df3034a1abbbdeb66f52529be40c to your computer and use it in GitHub Desktop.
Save dennisfrank/8493df3034a1abbbdeb66f52529be40c to your computer and use it in GitHub Desktop.
Buddy.works Craft 3 Atomic Deployment Pipeline

Buddy.works Craft 3 Atomic Deployment Pipeline

(This is totally work in progress, unfinished and unpolished.)

Inspired by Buddy Atomic deployments and croxton/pipeline.yml.

Assumptions

Preperations

Before running the pipeline you have to create the following Buddy environment variables:

  • host: remote host IP or domain
  • user: remote ssh user
  • remote_path: remote path
  • webroot: Craft’s web root directory name (usally web)
  • php_version: PHP-FPM version (e. g. 7.2)
- pipeline: "Build and deploy to [environment]"
trigger_mode: "ON_EVERY_PUSH"
ref_name: "develop"
actions:
- action: "Init: Atomic Deployment"
type: "SSH_COMMAND"
working_directory: "${remote_path}/"
login: "${user}"
host: "${host}"
port: "22"
env_key: "id_rsa"
authentication_mode: "ENV_KEY"
commands:
- "if [ ! -d \"${remote_path}\" ];"
- "then"
- "echo \"ERROR: Remote path ${remote_path} does not exist.\""
- "exit 1"
- "fi"
- "mkdir -p deploy-cache"
- "mkdir -p releases"
- "mkdir -p _config"
- "mkdir -p _storage"
- "mkdir -p _data"
run_as_script: true
shell: "BASH"
trigger_condition: "ALWAYS"
- action: "Execute: yarn craft"
type: "BUILD"
docker_image_name: "library/node"
docker_image_tag: "6"
execute_commands:
- "yarn"
- "yarn craft"
setup_commands:
- "npm install -g gulp"
trigger_condition_paths:
- "/src"
shell: "SH"
trigger_condition: "ON_CHANGE_AT_PATH"
- action: "Execute: composer install"
type: "BUILD"
docker_image_name: "library/php"
docker_image_tag: "7.1"
execute_commands:
- "cd craft"
- "composer install --ignore-platform-reqs --no-interaction --optimize-autoloader"
setup_commands:
- "apt-get update && apt-get install -y git zip"
- "curl -L https://phar.phpunit.de/phpunit.phar -o /usr/local/bin/phpunit"
- "chmod +x /usr/local/bin/phpunit"
- "curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer"
services:
- type: "MYSQL"
version: "5.7"
persistent: true
connection:
host: "mysql"
port: 3306
user: "root"
password: "qwerty"
trigger_condition_paths:
- "craft/composer.json"
- "craft/composer.lock"
shell: "SH"
trigger_condition: "ON_CHANGE_AT_PATH"
- action: "Execute: backup_db.sh"
type: "SSH_COMMAND"
working_directory: "${remote_path}"
login: "${user}"
host: "${host}"
port: "22"
env_key: "id_rsa"
authentication_mode: "ENV_KEY"
commands:
- "if [ -f \"releases/${execution.to_revision.revision}/scripts/backup_db.sh\" ];"
- "then"
- "echo \"Backup database\""
- "${remote_path}/releases/${execution.to_revision.revision}/scripts/backup_db.sh"
- "fi"
run_as_script: true
shell: "BASH"
trigger_condition: "ALWAYS"
- action: "Deploy: Rsync to Host"
type: "RSYNC"
local_path: "craft/"
remote_path: "${remote_path}/deploy-cache"
login: "${user}"
host: "${host}"
port: "22"
env_key: "id_rsa"
authentication_mode: "ENV_KEY"
archive: true
delete_extra_files: true
compress: true
deployment_excludes:
- "/storage/"
- "/${webroot}/cpresources/"
- "/${webroot}/files/"
- ".env"
- ".env.sh"
- ".env.php"
trigger_condition: "ALWAYS"
- action: "Deploy: Post-deployment action"
type: "SSH_COMMAND"
working_directory: "${remote_path}/"
login: "${user}"
password: "${ssh_password}"
host: "${host}"
port: "22"
env_key: "id_rsa"
authentication_mode: "ENV_KEY"
commands:
- "if [ -d \"releases/${execution.to_revision.revision}\" ] && [ \"${execution.refresh}\" = \"true\" ];"
- "then"
- "echo \"Removing: releases/${execution.to_revision.revision}\""
- "rm -rf releases/${execution.to_revision.revision};"
- "fi"
- "if [ ! -d \"releases/${execution.to_revision.revision}\" ];"
- "then"
- "echo \"Creating: releases/${execution.to_revision.revision}\""
- "cp -dR deploy-cache/ releases/${execution.to_revision.revision};"
- "fi"
- "echo \"Linking current to revision: ${execution.to_revision.revision}\""
- "rm -f current"
- "ln -s releases/${execution.to_revision.revision} current"
- "echo \"Creating: persistent directories\""
- "mkdir -p _storage/craft"
- "mkdir -p _storage/cpresources"
- "mkdir -p _data/files"
- "echo \"Symlinking: persistent directories\""
- "ln -nfs ${remote_path}/_storage/craft ${remote_path}/releases/${execution.to_revision.revision}/storage"
- "ln -nfs ${remote_path}/_storage/cpresources ${remote_path}/releases/${execution.to_revision.revision}/${webroot}/cpresources"
- "ln -nfs ${remote_path}/_data/files ${remote_path}/releases/${execution.to_revision.revision}/${webroot}/files"
- "echo \"Creating: config directories\""
- "mkdir -p _config"
- "echo \"Symlinking: config files\""
- "ln -nfs ${remote_path}/_config/.env ${remote_path}/releases/${execution.to_revision.revision}/"
- "ln -nfs ${remote_path}/_config/.env.php ${remote_path}/releases/${execution.to_revision.revision}/"
- "ln -nfs ${remote_path}/_config/.env.sh ${remote_path}/releases/${execution.to_revision.revision}/scripts/"
- "echo \"Removing old releases\""
- "cd releases && ls -t | tail -n +6 | xargs rm -rf"
run_as_script: true
shell: "SH"
trigger_condition: "ALWAYS"
- action: "Clean up: Clear caches"
type: "SSH_COMMAND"
working_directory: "${remote_path}"
login: "${user}"
host: "${host}"
port: "22"
env_key: "id_rsa"
authentication_mode: "ENV_KEY"
commands:
- "# Clear Craft caches"
- "current/scripts/clear_caches.sh"
- "# Restart PHP and clear OPcache"
- "echo \"\" | sudo -S service php${php_version}-fpm reload"
run_as_script: true
shell: "SH"
trigger_condition: "ALWAYS"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment