Skip to content

Commit

Permalink
Switch standard deploy setup to bitbucket
Browse files Browse the repository at this point in the history
  • Loading branch information
Numkil committed Oct 27, 2023
1 parent 52f878d commit 421c275
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 143 deletions.
47 changes: 0 additions & 47 deletions TODO

This file was deleted.

28 changes: 28 additions & 0 deletions bitbucket-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
image: statikbe/bitbucket-php81:latest

pipelines:
branches:
develop:
- step:
name: Deploy to staging
deployment: staging
caches:
- composer
script:
# build frontend and run composer
- composer install --verbose --prefer-dist --no-progress --no-interaction --no-dev --optimize-autoloader
# deploy:
- cp .deploy/ssh/* ~/.ssh/. && chmod 400 ~/.ssh/bitbucket_pipelines*
- dep deploy staging -vv
main:
- step:
name: Deploy to live
deployment: production
caches:
- composer
script:
# build frontend and run composer
- composer install --verbose --prefer-dist --no-progress --no-interaction --no-dev --optimize-autoloader
# deploy:
- cp .deploy/ssh/* ~/.ssh/. && chmod 400 ~/.ssh/bitbucket_pipelines*
- dep deploy production -vv
4 changes: 2 additions & 2 deletions config/htaccess-production
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ SetEnvIf Request_URI "/img/site/logo.png" publiclogo
Order allow,deny
AuthType Basic
AuthName "Authentication Required"
AuthUserFile /data/sites/web/crabaslivestatikbe/.htpasswd
AuthUserFile /data/sites/web/[PROJECT_CODE_HERE]livestatikbe/.htpasswd
Require valid-user
Allow from 81.82.199.174
Allow from 94.225.165
Expand All @@ -16,7 +16,7 @@ Satisfy Any
RewriteEngine On

# craft3.live.statik.be to real url
# RewriteCond %{HTTP_HOST} ^crabas.live.statik.be$ [NC]
# RewriteCond %{HTTP_HOST} ^[PROJECT_CODE_HERE].live.statik.be$ [NC]
# RewriteRule (.*)$ https://www.base-install.be/$1 [R=301,L]

# Set a seperate robots to all .statik domains
Expand Down
2 changes: 1 addition & 1 deletion config/htaccess-staging
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ SetEnvIf Request_URI "/img/site/logo.png" publiclogo
Order allow,deny
AuthType Basic
AuthName "Authentication Required"
AuthUserFile /data/sites/web/crabaslivestatikbe/.htpasswd
AuthUserFile /data/sites/web/[PROJECT_CODE_HERE]livestatikbe/.htpasswd
Require valid-user
Allow from 81.82.199.174
Allow from 94.225.165
Expand Down
149 changes: 149 additions & 0 deletions deploy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php
namespace Deployer;

require_once 'recipe/common.php';
require_once 'contrib/rsync.php';
require_once 'contrib/cachetool.php';
require_once 'contrib/yarn.php';

// Config
set('repository', '[email protected]:statikbe/[PROJECT_CODE_HERE].git');
//change writeable mode to chown because combell does not have acl installed:
set('writable_mode', 'chown');
set('keep_releases', 2);
//set('copy_dirs', ['vendor', 'node_modules']);

// [Optional] Allocate tty for git clone. Default value is false.
set('git_tty', true);

set('cachetool_args', '--web --web-path=./web --web-url=https://{{http_host}}');

// Shared files/dirs between deploys
set('shared_files', [
'.env'
]);
set('shared_dirs', [
'storage',
'public/files'
]);

// Writable dirs by web server
set('writable_dirs', [
'storage',
'storage/runtime',
'storage/logs',
'storage/rebrand',
'public/cpresources',
'public/frontend'
]);

// Set the worker process user
set('http_user', 'worker');

// Disable multiplexing
set('ssh_multiplexing', false);

//configure rsync:
set('rsync_src', __DIR__);
set('rsync_dest','{{release_path}}');
set('rsync', [
'exclude' => [
'.git',
'deploy.php',
'node_modules',
'public/frontend'
],
'exclude-file' => false,
'include' => [],
'include-file' => false,
'filter' => [],
'filter-file' => false,
'filter-perdir' => false,
'flags' => 'rz',
'options' => ['delete'],
'timeout' => 300,
]);

// Hosts
import('hosts.yml');

desc('Copy the correct .htaccess file for the given stage');
task('statik:copy_htaccess', function () {
$htaccessFile = get('htaccess_file');
if($htaccessFile){
run("cp {{release_path}}/config/$htaccessFile {{release_path}}/public/.htaccess");
}
});

desc('Copy the correct robots.txt file for the given stage');
task('statik:copy_robots', function () {
$robotsFile = get('robots_file');
if($robotsFile){
run("cp {{release_path}}/config/$robotsFile {{release_path}}/public/robots.txt");
}
});

desc('Give execute permissions for the Craft console command');
task('craft:set_permissions', function () {
run('chmod +x {{release_path}}/craft');
})->once();

desc('Craft up');
task('craft:up', function () {
run('{{release_path}}/craft up');
})->once();

desc('Cache clear');
task('craft:clear_caches', function () {
run('{{release_path}}/craft clear-caches/all');
})->once();

desc('Frontend build');
task('statik:frontend_build', function () {
run('cd {{release_path}} && yarn install --ignore-optional && yarn run prod');
})->once();

desc('Fichenbak versioning');
task('statik:fichenbak_versioning', function () {
run('if [ -s {{release_path}}/fichenbak-versioning.sh ]; then sh {{release_path}}/fichenbak-versioning.sh; fi');
})->once();

desc('Symlink current/public to www');
task('statik:symlink', function () {
$stage = get('stage');

if ($stage === 'production') {
run('if [ ! -L "/data/sites/web/[PROJECT_CODE_HERE]livestatikbe/www" ]; then ln -s subsites/[PROJECT_CODE_HERE].live.statik.be/current/public /data/sites/web/[PROJECT_CODE_HERE]livestatikbe/www; fi');
} else {
run('echo "only run this task on production"');
}

})->once();

//overwrite the deploy:prepare task, to change the git clone command with rsync:
task('deploy:prepare', [
'deploy:info',
'deploy:setup',
'deploy:lock',
'deploy:release',
'rsync',
'deploy:shared',
'deploy:writable',
]);

task('deploy', [
'deploy:prepare',
'craft:set_permissions',
'craft:up',
'craft:clear_caches',
'statik:copy_htaccess',
'statik:copy_robots',
'statik:frontend_build',
'statik:fichenbak_versioning',
'deploy:publish',
]);

//Adjust standard deployment:
after('deploy:failed', 'deploy:unlock');
after('deploy', 'statik:symlink');
after('deploy', 'deploy:success');
21 changes: 21 additions & 0 deletions hosts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
hosts:
staging:
stage: staging
hostname: [PROJECT_CODE_HERE].ssh.statik.be
remote_user: [PROJECT_CODE_HERE]livestatikbe
deploy_path: ~/subsites/[PROJECT_CODE_HERE].staging.statik.be
http_user: [PROJECT_CODE_HERE]livestatikbe
htaccess_file: htaccess-staging
robots_file: robots-staging
configFile: ~/.ssh/config
http_host: [PROJECT_CODE_HERE].staging.statik.be
production:
stage: production
hostname: [PROJECT_CODE_HERE].ssh.statik.be
remote_user: [PROJECT_CODE_HERE]livestatikbe
deploy_path: ~/subsites/[PROJECT_CODE_HERE].live.statik.be
http_user: [PROJECT_CODE_HERE]livestatikbe
htaccess_file: htaccess-production
robots_file: robots-production
configFile: ~/.ssh/config
http_host: [PROJECT_CODE_HERE].live.statik.be
34 changes: 20 additions & 14 deletions modules/statik/src/console/controllers/SetupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@

class SetupController extends Controller
{
private const DEPLOY_FILES = [
CRAFT_BASE_PATH . '/deploy.php',
CRAFT_BASE_PATH . '/hosts.yml',
CRAFT_BASE_PATH . '/bitbucket-pipelines.yml',
];
private const PROJECT_CODE_PLACEHOLDER = '[PROJECT_CODE_HERE]';

// Public Methods
// =========================================================================
public function actionIndex(): int
Expand Down Expand Up @@ -43,9 +50,6 @@ public function actionIndex(): int
EOD;

top:

$this->stdout(str_replace("\n", PHP_EOL, $statik), Console::FG_BLUE);

$this->setSystemName();
Expand Down Expand Up @@ -75,27 +79,29 @@ private function setProjectCode(): void
{
$newProjectCode = $this->prompt('Enter a the project code:');
if ($newProjectCode) {
// Replace CRABAS in htaccess-staging and htaccess-production
$this->replaceInHtaccess(Craft::$app->path->getConfigPath() . '/htaccess-staging', $newProjectCode);
$this->replaceInHtaccess(Craft::$app->path->getConfigPath() . '/htaccess-production', $newProjectCode);
// Replace self::PROJECT_CODE_PLACEHOLDER in htaccess-staging and htaccess-production
$this->replaceInFile(Craft::$app->path->getConfigPath() . '/htaccess-staging', $newProjectCode);
$this->replaceInFile(Craft::$app->path->getConfigPath() . '/htaccess-production', $newProjectCode);
foreach(self::DEPLOY_FILES as $deployFile) {
$this->replaceInFile($deployFile, $newProjectCode);
}
}
}

private function replaceInHtaccess(string $htaccessPath, string $projectCode): bool
private function replaceInFile(string $filePath, string $projectCode): bool
{
if (file_exists($htaccessPath)) {
$htaccess = file_get_contents($htaccessPath);
$htaccess = str_replace('crabas', strtolower($projectCode), $htaccess);
file_put_contents($htaccessPath, $htaccess);
if (file_exists($filePath)) {
$fileContents = file_get_contents($filePath);
$fileContents = str_replace(self::PROJECT_CODE_PLACEHOLDER, strtolower($projectCode), $fileContents);
file_put_contents($filePath, $fileContents);
} else {
$this->stderr("$htaccessPath file not found." . PHP_EOL, Console::FG_RED);
$this->stderr("$filePath file not found." . PHP_EOL, Console::FG_RED);
return false;
}
$this->stdout("Updated $htaccessPath with $projectCode!" . PHP_EOL, Console::FG_GREEN);
$this->stdout("Updated $filePath with $projectCode!" . PHP_EOL, Console::FG_GREEN);
return true;
}


private function removeAccountFlow(): bool
{
if ($this->confirm("Do you want to remove the frontend account flow in Craft?", false)) {
Expand Down
Loading

0 comments on commit 421c275

Please sign in to comment.