Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
shopapps opened this issue Sep 30, 2021 · 14 comments
Closed

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

shopapps opened this issue Sep 30, 2021 · 14 comments

Comments

@shopapps
Copy link

shopapps commented Sep 30, 2021

  • 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';
}

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@kjkooistra-youwe
Copy link
Contributor

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

@campaignupgrade
Copy link

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.

@joelpittet
Copy link
Contributor

joelpittet commented Jan 24, 2022

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

@shopapps
Copy link
Author

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
Copy link
Author

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;
});

@antonmedv
Copy link
Member

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

Clearly I don’t understand the struggle.

@shopapps
Copy link
Author

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.

@antonmedv antonmedv reopened this Jan 25, 2022
@joelpittet
Copy link
Contributor

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

@joelpittet
Copy link
Contributor

@antonmedv Not sure if this would work but around here in the SelectCommand

} else if ($input->isInteractive()) {

Could we have a hook to provide a default selector? I'd sure enough set it to stage=staging

@tomaszkane
Copy link
Contributor

Related: #3197

@boooyu44
Copy link

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?

@antonmedv
Copy link
Member

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.

@boooyu44
Copy link

boooyu44 commented Aug 31, 2022

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

Copy link
Contributor

github-actions bot commented Sep 9, 2024

This issue has been automatically closed. Please, open a discussion for bug reports and feature requests.

Read more: [https://github.com//discussions/3888]

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants