deployer icon indicating copy to clipboard operation
deployer copied to clipboard

has('previous_release') doesn't return true when multiple releases are present

Open sts-ryan-holton opened this issue 2 years ago • 0 comments

  • Deployer version: 6
  • Deployment OS: CentOS

My project keeps two releases (I'll attach my deploy.php file), I've got a custom task which checks using the has method provided by Deployer whether the previous_release is present, and I can indeed see two release folders on my production server in my releases directory.

When I add the previous_release check into an if statement it appears to always go into the else implying that it can't find a previous release?

I beleive this to be a bug with Deployer as I have followed what the example contains and it does not return true.

My deploy.php file

<?php
namespace Deployer;

require 'vendor/autoload.php';
require 'recipe/common.php';
require 'deploy/recipe/fudge-npm.php';
require 'deploy/recipe/fudge-server.php';
require 'deploy/recipe/fudge-composer.php';
require 'deploy/recipe/fudge-nuxt.php';
require 'deploy/recipe/fudge-duration.php';
require 'deploy/recipe/fudge-github.php';

// Project repository
set('repository', '[email protected]:company/repo.git');

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

// [Optional] Timeout in seconds, set to 14,400 (4 hours) for max compatibility.
set('default_timeout', 14400);

// Shared files/dirs between deploys
add('shared_files', ['.env', 'brand-theme.scss']);

// Set number of releases to keep
set('keep_releases', 2);

// Writable dirs by web server
set('allow_anonymous_stats', false);

// All brands
// See the README.md for list of friendly brand names for these hosts.
host('MYIP')
    ->set('branch', 'main')
    ->stage('all')
    ->set('repository', 'git@fudge:company/repo')
    ->user('root')
    ->set('deploy_path', '/var/www/include-forms/fudge');

// deploy
task('deploy', [
  'deploy:duration:datetime:start',
  'deploy:github:validate',
  'server:health:check',
  'server:health:validate',
  'deploy:prepare',
  'deploy:lock',
  'deploy:release',
  'deploy:update_code',
  'npm:install',
  'composer:install',
  'deploy:shared',
  'deploy:writable',
  'nuxt:generate',
  'deploy:clear_paths',
  'deploy:symlink',
  'deploy:unlock',
  'cleanup',
  'success',
  'deploy:duration:datetime:finish'
]);

// [Optional] if deploy fails automatically unlock.
after('deploy:failed', 'deploy:unlock');

The custom recipe in question is deploy/recipe/fudge-server.php which does the following (I've removed all code that isn't needed for the context of this bug):

<?php
/*
 * Custom server health recipe for us.
 */

namespace Deployer;

use Symfony\Component\Console\Input\InputOption;

option('disable-health-checks', false, InputOption::VALUE_OPTIONAL, 'disable disk health checks');

/*
** Disk health validation, will exit on insufficient amount.
*/
desc('Validate server health and exit if appropriate');
task('server:health:validate', function () {
    $hostname = get('hostname');

    // disable health checks
    $disableHealthChecks = false;
    if (input()->hasOption('disable-health-checks')) {
        $disableHealthChecks = input()->getOption('disable-health-checks');
    }

    // check if we should disable checks
    if ($disableHealthChecks) {
        writeln("<comment>[ ".$hostname." ][ health_validate: Disk health checks are disabled, this is highly discouraged. ]</comment>");
    } else {

        // execute on server
        on(host($hostname), function ($host) use ($hostname) {

            // if we have a previous release, we should check this first
            // for our file as we want our script to execute as early on
            // in the process as possible.
            if (has('previous_release')) {

                // make sure that we have the correct files for executing health
                // check otherwise we can't run it.
                if (
                    test('[ -f {{previous_release}}/deploy/scripts/server_disk_free.php ]') &&
                    test('[ -f {{previous_release}}/deploy/scripts/server_disk_total.php ]')
                ) {

                    // ... stuff here
                    
                } else {
                    writeln("<comment>[ ".$hostname." ][ health_validate: Unable to check disk health, scripts can't be found. ]</comment>");
                }

            } else {
                writeln("<comment>[ ".$hostname." ][ health_validate: Unable to check disk health, no previous release found. ]</comment>");
            }
        });
    }
});

/*
** General disk info.
** 5% or less = CRITIAL, take action, deployer will exit before deploy.
** 15% or less = ALERT, action is advised now.
** more than 15% = normal
*/
desc('Output server health');
task('server:health:check', function () {
    $hostname = get('hostname');

    // disable health checks
    $disableHealthChecks = false;
    if (input()->hasOption('disable-health-checks')) {
        $disableHealthChecks = input()->getOption('disable-health-checks');
    }

    // check if we should disable checks
    if ($disableHealthChecks) {
        writeln("<comment>[ ".$hostname." ][ health_check: Disk health checks are disabled, this is highly discouraged. ]</comment>");
    } else {

        // execute on server
        on(host($hostname), function ($host) use ($hostname) {

            // if we have a previous release, we should check this first
            // for our file as we want our script to execute as early on
            // in the process as possible.
            if (has('previous_release')) {

                // make sure that we have the correct files for executing health
                // check otherwise we can't run it.
                if (
                    test('[ -f {{previous_release}}/deploy/scripts/server_disk_free.php ]') &&
                    test('[ -f {{previous_release}}/deploy/scripts/server_disk_total.php ]')
                ) {

                    // ... stuff here

                } else {
                    writeln("<comment>[ ".$hostname." ][ health_check: Unable to check disk health, scripts can't be found. ]</comment>");
                }

            } else {
                writeln("<comment>[ ".$hostname." ][ health_check: Unable to check disk health, no previous release found. ]</comment>");
            }
        });

    }
});

See screenshot of multiple releases:

Screenshot 2022-07-26 at 10 51 33

sts-ryan-holton avatar Jul 26 '22 09:07 sts-ryan-holton