Last active
November 2, 2022 16:26
-
-
Save Sogl/6109dd405ed1420694632c63fc5108b1 to your computer and use it in GitHub Desktop.
v.7 Deployer PHP Grav CMS recipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Deployer; | |
require 'recipe/common.php'; | |
// Config | |
//add('recipes', ['grav']); | |
// Number of releases to preserve in releases folder. | |
set('keep_releases', 5); | |
//php 7.4 hosting | |
set('bin/php', '/usr/local/bin/ea-php74'); | |
set('bin/composer', '/usr/local/bin/ea-php74 /opt/cpanel/composer/bin/composer'); | |
set('bin/npm', '~/node/bin/npm'); | |
//disable relative symlinks | |
set('use_relative_symlink', false); | |
// Project name | |
set('application', 'domain.com'); | |
set('repository', 'ssh://repo.git'); | |
add('shared_files', []); | |
//add('shared_dirs', []); | |
add('shared_dirs', [ | |
'backup', | |
'logs', | |
]); | |
add('writable_dirs', []); | |
/** | |
* Run grav related command. | |
* | |
* @param string $executable The grav executable (grav, gpm, plugin). | |
* @param string $command The grav related command (with cli options if any). | |
* @param string $path Base path the executable lives in (defaults to release_path) | |
* @return callable A function that can be used as a task. | |
*/ | |
function bin(string $executable, string $command, string $path = '{{release_path}}'): callable | |
{ | |
return function () use ($path, $executable, $command) { | |
if (test("[ -d $path ]")) { | |
within($path, function () use ($executable, $command) { | |
$bin = "bin/$executable"; | |
if (test("[ -f $bin ]")) { | |
run("{{bin/php}} $bin $command"); | |
} else { | |
warning("$bin not found, skipping."); | |
} | |
}); | |
} | |
}; | |
} | |
function grav($command, $path = '{{release_path}}') | |
{ | |
return bin('grav', $command, $path); | |
} | |
desc('Create backup from Grav content'); | |
task('grav:backup', grav('backup', '{{current_path}}')); | |
desc('Removes extraneous files and folders from Grav'); | |
task('grav:clean', grav('clean')); | |
desc('Install Grav dependencies listed in .dependencies'); | |
task('grav:install', grav('install')); | |
desc('Scan contents for security vulnerabilities'); | |
task('grav:security', grav('security')); | |
set('grav_repository', 'https://github.com/getgrav/grav.git'); | |
set('grav_zip', 'https://getgrav.org/download/core/grav-admin/latest'); | |
set('grav_version', function () { | |
return getenv('GRAV_VERSION') ?: 'master'; | |
}); | |
function gpm($command, $path = '{{release_path}}') | |
{ | |
return bin('gpm', $command, $path); | |
} | |
desc('Grav package manager install'); | |
task('gpm:install', gpm('install admin breadcrumbs email error external_links flex-objects form google-analytics highlight license-manager login maintenance markdown-color markdown-notices metrika page-toc problems shortcode-core simplesearch sitemap tntsearch youtube')); | |
// task('gpm:install', gpm('install breadcrumbs external_links google-analytics highlight license-manager maintenance markdown-color metrika page-toc shortcode-core simplesearch sitemap tntsearch youtube')); | |
desc('Grav install Premium plugins (check you have user\data\licenses.yaml in your PRIVATE repo)'); | |
task('gpm:install_premium', gpm('install lightbox-gallery')); | |
function plugin($command, $path = '{{release_path}}') | |
{ | |
return bin('plugin', $command, $path); | |
} | |
desc('Indexing TNTSearch'); | |
task('plugin:tnt_index', plugin('tntsearch index')); | |
desc('Download Grav'); | |
task('grav:download', function () { | |
run('{{bin/git}} clone --branch {{grav_version}} --depth 1 {{grav_repository}} {{release_path}}'); | |
// remove all git related files | |
run('rm -rf {{release_path}}/.git'); | |
// remove all files in user (will be populated from the repository) | |
run('rm -rf {{release_path}}/user/*'); | |
}); | |
desc('Download Grav + Admin zip'); | |
task('grav:download_zip', function () { | |
run('mkdir -p test'); | |
// get by url | |
run('wget -O grav_latest.zip {{grav_zip}}'); | |
// unzip it in same directory | |
run('unzip grav_latest.zip'); | |
// move all grav-admin files (+ dotfiles dotfolders) | |
run('mv ~/grav-admin/* ~/grav-admin/.[!.]* ~/test/'); | |
// remove zip and extracted folder | |
run('rm -r grav_latest.zip grav-admin'); | |
// delete all in user except plugins | |
run('find ~/test/user -mindepth 1 -maxdepth 1 -name plugins -prune -o -exec rm -rf {} \;'); | |
//run("find ./test/user -mindepth 1 ! -regex '^./test/user/plugins\(/.*\)?' -delete"); | |
}); | |
desc ('Code update my 2'); | |
task('my:git', function () { | |
//GIT part | |
cd('~/test/user'); | |
$git = get('bin/git'); | |
$repository = get('repository'); | |
run("$git init"); | |
run("$git remote add origin $repository"); | |
run("$git fetch"); | |
run("$git checkout -t origin/master -f"); | |
// learn4 theme submodule | |
run("$git submodule update --init"); | |
}); | |
desc('Snappygrav (deleted from GH, stored in my repo) composer install'); | |
task('my:snappygrav_vendors', function () { | |
cd('{{release_path}}/user/plugins/snappygrav'); | |
run('{{bin/composer}} install'); | |
}); | |
desc('MY CUSTOM sogl-flex composer install for autoload'); | |
task('my:soglflex_vendors', function () { | |
cd('{{release_path}}/user/plugins/sogl-flex'); | |
run('{{bin/composer}} install'); | |
}); | |
desc('js css plugins for Flex (Choices.js) npm install'); | |
task('npm:install_in_theme', function () { | |
cd('{{release_path}}/user/themes/mytheme4'); | |
run("{{bin/npm}} install"); | |
}); | |
desc ('Code update my'); | |
task('my:update_code', function () { | |
$git = get('bin/git'); | |
$repository = get('repository'); | |
$bare = parse('{{deploy_path}}/.dep/repo'); | |
$env = [ | |
'GIT_TERMINAL_PROMPT' => '0', | |
'GIT_SSH_COMMAND' => get('git_ssh_command') | |
]; | |
start: | |
// Clone the repository to a bare repo. | |
run("[ -d $bare ] || mkdir -p $bare"); | |
run("[ -f $bare/HEAD ] || {{bin/git}} clone --mirror $repository $bare 2>&1", ['env' => $env]); | |
cd($bare); | |
run("{{bin/git}} remote update 2>&1", ['env' => $env]); | |
//if no .git and without submodules | |
//run("{{bin/git}} archive HEAD | tar -x -f - -C {{release_path}}/user 2>&1"); | |
//with .git folder and submodules | |
cd('{{release_path}}/user'); | |
run("$git clone --recurse-submodules -l $bare ."); | |
run("$git checkout --force HEAD"); | |
// Save git revision in REVISION file. | |
$rev = escapeshellarg(run("{{bin/git}} rev-list HEAD -1")); | |
run("echo $rev > {{release_path}}/REVISION"); | |
}); | |
// Hosts | |
host('my.host') | |
->setForwardAgent(true) | |
->setRemoteUser('user') | |
->setPort(21) | |
//->setConfigFile('~/.ssh/config') | |
->setIdentityFile('~/.ssh/key') | |
->setSshArguments([ | |
'-o UserKnownHostsFile=/dev/null', | |
'-o StrictHostKeyChecking=no', | |
]) | |
->setDeployPath('~/public_html/{{application}}'); | |
// Hooks | |
task('deploy:prepare', [ | |
'deploy:info', | |
'deploy:setup', | |
'deploy:lock', | |
'deploy:release', | |
]); | |
task('deploy', [ | |
//'grav:backup', | |
'deploy:prepare', | |
'grav:download', | |
// 'deploy:update_code', | |
'my:update_code', | |
'deploy:shared', | |
'deploy:writable', | |
'deploy:vendors', | |
//'grav:install', | |
'my:soglflex_vendors', | |
'gpm:install', | |
'my:snappygrav_vendors', | |
'gpm:install_premium', | |
'npm:install_in_theme', | |
'grav:clean', | |
'plugin:tnt_index', | |
//'grav:security', | |
'deploy:publish', | |
]); | |
after('deploy:failed', 'deploy:unlock'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment