deployer icon indicating copy to clipboard operation
deployer copied to clipboard

[7.0-beta] How to set default stage / host

Open shopapps opened this issue 4 years ago • 13 comments

  • Deployer version: 7.0.0-beta.26
  • Deployment OS: MacOS

(using laravel recipe) with ver 6.8 to deploy to either production or staging in our setup we ran:

for staging

dep deploy
(which is equivalent to...)
dep deploy staging

for production

dep deploy production

Question 1: where can we set the default stage? (in your terminology i think this a host 'alias' ) Question 2: How can we pass the git branch to deploy?

snippet from our deploy.php


set('default_stage', 'staging');  // this no longer works

host('production')
    ->setHostname(test.co.uk')
    ->set('stage', 'production')
    ->setRemoteUser('admin')
    ->setIdentityFile( '~/.ssh/id_rsa')
//    ->set('branch', '{{branch}}')
    ->setDeployPath('{{root_dir}}/{{application}}/{{stage}}')
    ->set('cachetool', '/var/run/php{{php_ver}}-fpm-admin.{{hostname}}.sock')
    ->set('labels', [
        'stage' => 'production'
    ])
;

host('staging')
    ->setHostname('test.co.uk')
    ->set('stage', 'staging')
    ->setRemoteUser('admin')
    ->setIdentityFile('~/.ssh/id_rsa')
//    ->set('branch', '{{branch}}')
    ->setDeployPath('{{root_dir}}/{{application}}/{{stage}}')
    ->set('cachetool', '/var/run/php{{php_ver}}-fpm-staging-admin.{{hostname}}.sock')
    ->set('labels', [
        'stage' => 'staging'
    ])
;


host('test')
    ->setHostname('test.co.uk')
    ->set('stage', 'test')
    ->setRemoteUser('admin')
    ->setIdentityFile('~/.ssh/id_rsa')
//    ->set('branch', '{{branch}}')
    ->setDeployPath('{{root_dir}}/{{application}}/{{stage}}')
    ->set('cachetool', '/var/run/php{{php_ver}}-fpm-uat-admin.{{hostname}}.sock')
    ->set('labels', [
        'stage' => 'test'
    ])
;

NOTE: I did try adding this to the top of our deploy.php file but it didnt work...

/*
 * Workaround for Version 7 to set a default host
 */
$is_deploy = false;
if(array_key_exists(1, $_SERVER['argv'])) {
    if($_SERVER['argv'][1] == 'deploy')
    {
        $is_deploy = true;
    }
}

if($is_deploy && !array_key_exists(2, $_SERVER['argv'])) {
    $_SERVER['argv'][2] = 'staging';
}

shopapps avatar Sep 30 '21 15:09 shopapps

For the stage, read the upgrade documentation (use stage=prod). Personally I just check out the git branch manually.

kjkooistra-youwe avatar Nov 05 '21 07:11 kjkooistra-youwe

I'd love to do the opposite — assign the value of the host alias based on the current git branch.

If on branch staging, default to dep deploy staging if on branch production, default to dep deploy production

This lets me keep the logic in dep in the repo, rather that putting the logic in a GitHub actions skip.

campaignupgrade avatar Dec 28 '21 23:12 campaignupgrade

Yeah I'm struggling with this too, I loved the feature to have it deploy a "feature" branch by default to staging when a stage wasn't set but when it was production to always deploy from the main/master branch. If I figure out how I'll share here or try to contribute to the docs if @antonmedv could stub out a place he'd like to see it?

Also now instead of dep deploy will ask me which server to deploy to when really it's likely 2+, so the workflow is going to take some getting used to.

Here's our 7.x recipe WIP https://github.com/ubc-cpsc/deployer-recipes/tree/feature/7.x-compatibility

joelpittet avatar Jan 24 '22 17:01 joelpittet

I also tried this, but am not convinced is working:

host('staging')
    ->setHostname('test.co.uk')
    ->setRemoteUser('admin')
    ->setIdentityFile('~/.ssh/id_rsa')
    ->set('stage', function () {
        return (input()->hasOption('stage')) ? input()->getOption('stage') : 'staging';
    })
    ->set('branch', function () {
        return (input()->hasOption('branch')) ? input()->getOption('branch') : 'develop';
    })
    ->setDeployPath('{{root_dir}}/{{application}}/{{stage}}')
    ->set('cachetool', function() {
        $stage    = get('stage');
        $php_ver  = get('php_ver');
        $hostname = get('hostname');

        if($stage == 'production') {
            $stage = '';
        }

        return "/var/run/php/php{$php_ver}-fpm-{$hostname}.sock --tmp-dir=/home/admin/tmp";
    })
    ->set('cachetool_args', '--web=FileGetContents --web-path=./public --web-url=https://{{hostname}}')
    ->set('labels', [
        'stage' => '{{stage}}',
    ])
;

shopapps avatar Jan 24 '22 19:01 shopapps

and i also tried adding this to near the top of deploy.php.

but still doesnt work :-(

set('branch', function () {

    $branch = null;
    if (input()->hasOption('branch') && !empty(input()->getOption('branch'))) {
        $branch = input()->getOption('branch');
    }

    if(!$branch) {
        /*
         * bit hacky but check if a vlaue has been passed
         */
        switch(input()->getOption('host')) {
            case 'staging':     $branch = 'develop';    break;
            case 'test':        $branch = 'master';     break;
            case 'production':  $branch = 'master';     break;
            default:            $branch = 'develop';    break;
        }
    }

    if(!$branch) $branch = 'develop';

    return $branch;
});

shopapps avatar Jan 24 '22 19:01 shopapps

If you can explain what you are trying to achieve, I’ll be able to help.

Clearly I don’t understand the struggle.

antonmedv avatar Jan 24 '22 20:01 antonmedv

Hi @antonmedv ,

Sorry if i was not clear in my first post.

We have 2 deployment environments, 'production' and 'staging' and use gitflow as our VCS workflow meaning we have 'master' and 'develop' branches that equate to the environments. On (rocketeer originally and then...) the old 6.8 release we used to run

Staging release from develop: dep deploy (which was equivalent to...) dep deploy staging

Production release from master dep deploy production

which was nice and simple.

with the new 7 release (RC4) this no longer works.

i have configure two hosts one called staging, one called production detailed below, but if i run

dep deploy staging

it deploys the HEAD branch to the staging environment not the desired develop.

current host configuration:


host('production')
    ->setHostname('production.test.co.uk')
    ->setRemoteUser('admin')
    ->setIdentityFile( '~/.ssh/id_rsa')
    ->set('stage', function () {
        return (input()->hasOption('stage')) ? input()->getOption('stage') : 'production';
    })
    ->set('branch', function () {
        return (input()->hasOption('branch')) ? input()->getOption('branch') : 'master';
    })
    ->setDeployPath('{{root_dir}}/{{application}}/{{stage}}')
    ->set('cachetool', function() {
        $stage    = get('stage');
        $php_ver  = get('php_ver');
        $hostname = get('hostname');

        if($stage == 'production') {
            $stage = '';
        }

        return "/var/run/php/php{$php_ver}-fpm-{$hostname}.sock --tmp-dir=/home/admin/tmp";
    })
    ->set('cachetool_args', '--web=FileGetContents --web-path=./public --web-url=https://{{hostname}}')
    ->set('labels', [
        'stage' => '{{stage}}',
    ])
;

host('staging')
    ->setHostname('staging.test.co.uk')
    ->setRemoteUser('admin')
    ->setIdentityFile('~/.ssh/id_rsa')
    ->set('stage', function () {
        return (input()->hasOption('stage')) ? input()->getOption('stage') : 'staging';
    })
    ->set('branch', function () {
        return (input()->hasOption('branch')) ? input()->getOption('branch') : 'develop';
    })
    ->setDeployPath('{{root_dir}}/{{application}}/{{stage}}')
    ->set('cachetool', function() {
        $stage    = get('stage');
        $php_ver  = get('php_ver');
        $hostname = get('hostname');

        if($stage == 'production') {
            $stage = '';
        }

        return "/var/run/php/php{$php_ver}-fpm-{$hostname}.sock --tmp-dir=/home/admin/tmp";
    })
    ->set('cachetool_args', '--web=FileGetContents --web-path=./public --web-url=https://{{hostname}}')
    ->set('labels', [
        'stage' => '{{stage}}',
    ])
;

in addition we also occasionally want to test a feature branch in staging ( or production) in which case this command does work as expected:

dep deploy staging --branch='feature/my_test_feature_branch'

if the above is still not clear please let me know.

shopapps avatar Jan 25 '22 08:01 shopapps

@antonmedv what was behind the decision to remove the 'stage' argument?

joelpittet avatar Jan 27 '22 04:01 joelpittet

@antonmedv Not sure if this would work but around here in the SelectCommand https://github.com/deployphp/deployer/blob/42734c5f352e04279097fde6f4cc9182e342755b/src/Command/SelectCommand.php#L56 Could we have a hook to provide a default selector? I'd sure enough set it to stage=staging

joelpittet avatar Feb 25 '22 01:02 joelpittet

Related: #3197

tomaszkane avatar Jul 15 '22 10:07 tomaszkane

Why still no "default_stage" replacement as of 7.x? It should be certainly implemented.

By default deployer connects to all defined hosts and attempts to begin deployment to all of them. Imagine you have dev and prod and it'll begin deployment everywhere just because you did not specify your selector.

In certain situations this can lead to dangerous mistakes and default_stage was the security switch, where you could define dev environment as default one. This is very basic functionality and honestly i cannot understand why it's not implemented yet and why it was removed without implementing any replacement.

Is there any workaround to avoid deploying to all hosts by mistake when no selector is provided via cli?

boooyu44 avatar Aug 31 '22 02:08 boooyu44

By default deployer connects to all defined hosts and attempts to begin deployment to all of them.

Not anymore. In Deployer v7 in selector not specified, Deployer will ask which hosts to deploy.

antonmedv avatar Aug 31 '22 05:08 antonmedv

By default deployer connects to all defined hosts and attempts to begin deployment to all of them.

Not anymore. In Deployer v7 in selector not specified, Deployer will ask which hosts to deploy.

Thanks for info

boooyu44 avatar Aug 31 '22 11:08 boooyu44