Deployer: Cannot access options outside tasks

Created on 9 Jan 2019  路  7Comments  路  Source: deployphp/deployer

I need to access the value for an option (or find a better workaround) before defining my hosts which necessarily comes before calling tasks similar to #1320. We have a single code base that handles two websites which need to be deployed to different folders on the remote hosts.

$ bin/vendor/dep deploy --acronym=SITE_ACRONYM beta

// deploy.php
option('acronym', 'a', InputOption::VALUE_REQUIRED, 'Site acronym');
...
// use input()->getOption('acronym') to set('site_folder') and other values used by host config
...
host(...)
    ->stage('prod')
    ->set('deploy_path', '/var/www/html/{{site_folder}}')

I could duplicate all four host configs, e.g. beta-site1 and beta-site2, but that duplicates a lot of values and invites errors. Is there a better way to handle this? I already have a deploy.sh that checks some arguments and rearranges the call to dep so I'm happy to add something there. But I still don't see how to have an option/argument affect the host configurations.

Most helpful comment

@Shaked Do you mean an example of using a closure? Here ya go:

set('branch', function () {
    if (input()->hasOption('branch') && !empty(input()->getOption('branch'))) {
        return input()->getOption('branch');
    }
    else {
        return 'master';
    }
});

All 7 comments

It's impossible to access options during creation of tasks. But you can use environment variable there.

Okay, an environment variable is a good fallback. I was hoping to have it listed in the options when using help, but I can just document it in the code.

I went with the environment variable, but I later realized I could access options from a closure. For anyone finding this in the future, this is probably easier.

@dharkness can you share an example?

Thanks!

@Shaked Do you mean an example of using a closure? Here ya go:

set('branch', function () {
    if (input()->hasOption('branch') && !empty(input()->getOption('branch'))) {
        return input()->getOption('branch');
    }
    else {
        return 'master';
    }
});

@dharkness - thanks. It's impossible to use it outside a task, right? I'd like to have different hosts depending on a specific option. Any idea how I'd be able to do that (if at all)?

UPDATE:

Found an idea that works for me...:

host(...[ip1,ip2...])...->stage('dev.type1');
host(...[ip10,ip11...])...->stage('dev.type2'); 

In my case Im trying to differentiate betweengpuandcpu` servers

@Shaked A context is created to call get() which will be used when accessing options and arguments. It's a hack, but it allows you to access them outside of tasks. I discovered this only after building my workaround using environment variables.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sweebee picture sweebee  路  3Comments

dima-stefantsov picture dima-stefantsov  路  5Comments

JonasDoebertin picture JonasDoebertin  路  4Comments

chouex picture chouex  路  4Comments

jolipixel picture jolipixel  路  4Comments