From 58d67f561dc9a77c25e3bde03d09643a774100ae Mon Sep 17 00:00:00 2001 From: Fery Wardiyanto Date: Thu, 29 Feb 2024 11:34:32 +0700 Subject: [PATCH] chore: add ability to reset database on deployment, default no-reset Signed-off-by: Fery Wardiyanto --- .github/workflows/deploy.yml | 9 ++++++++- scripts/deploy.php | 23 ++++++++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index b91b77e..013a2b2 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -13,6 +13,10 @@ on: description: Deployment target required: true type: environment + reset: + description: Reset database + default: false + type: boolean workflow_run: workflows: [Code Quality] types: [completed] @@ -78,8 +82,11 @@ jobs: - name: Deploy uses: deployphp/action@master + env: + DB_RESET: ${{ github.event_name == 'workflow_dispatch' && inputs.reset || false }} + DEPLOY_ARGS: --branch ${{ needs.prepare.outputs.target-branch }} --$([ $DB_RESET = true ] && echo 'reset' || echo 'no-reset') with: private-key: ${{ secrets.DEPLOY_SSH_RSAKEY }} ssh-config: ${{ secrets.DEPLOY_SSH_CONFIG }} verbosity: '' - dep: deploy -f scripts/deploy.php env=staging --branch ${{ needs.prepare.outputs.target-branch }} + dep: deploy -f scripts/deploy.php env=staging $DEPLOY_ARGS diff --git a/scripts/deploy.php b/scripts/deploy.php index ece97f6..9cfc980 100644 --- a/scripts/deploy.php +++ b/scripts/deploy.php @@ -3,7 +3,7 @@ namespace Deployer; use Illuminate\Support\Env; -use Illuminate\Support\Str; +use Symfony\Component\Console\Input\InputOption; $rootDir = \dirname(__DIR__); @@ -16,6 +16,8 @@ add('shared_dirs', ['public/build', 'public/vendor']); add('writable_dirs', []); +option('reset', null, InputOption::VALUE_NEGATABLE, 'Reset database', false); + // Hosts host('skeleton.creasi.dev') @@ -105,6 +107,20 @@ info('database '.$dbName.' created'); })->desc('Setup deployment environment'); +task('deploy:database', function () { + if (! input()->getOption('reset')) { + invoke('artisan:migrate'); + info('database migrated'); + return; + } + + invoke('artisan:down'); + invoke('artisan:migrate:fresh'); + invoke('artisan:db:seed'); + invoke('artisan:up'); + info('database freshly migrated'); +})->desc('Deploy database'); + task('deploy:assets', function () { $config = ['flags' => '-zrtv']; @@ -124,9 +140,6 @@ 'deploy:assets', 'artisan:storage:link', 'artisan:optimize:clear', - 'artisan:down', - 'artisan:migrate:fresh', - 'artisan:db:seed', - 'artisan:up', + 'deploy:database', 'deploy:publish', ]);