Conditional set
based on a parameter set on the host
#3373
-
Hello, (Using Deployer version 6) I have several (20+) projects that all share similar environments and are set up to use one of two - either private servers or cPanels. We also have a mix of environments for individual projects (e.g. staging might be a VPS but live a cPanel). As all of our websites are set up the same, I wanted to share some config between them with a deployer meta package. I was wondering - how can I conditionally set certain parameters and fall back to defaults if not set? E.g., the composer path. On the VPS, the composer path needs to be set to host('production')
->set('ll_deployer_environment', 'cpanel') // can be cpanel or vps
->set('deploy_path', '/dir')
; I originally tried wrapping the if (get('ll_deployer_environment') === 'vps') {
set('bin/composer','/usr/local/bin/composer');
} Next, I tried the set('bin/composer', function () {
if (get('ll_deployer_environment') === 'vps') {
return '/usr/local/bin/composer';
}
return get('bin/composer');
}); Lastly i tried caching the value first, but that returns an error of $composer = get('bin/composer');
set('bin/composer', function () use ($composer) {
if (get('ll_deployer_environment') === 'vps') {
return '/usr/local/bin/composer';
}
return $composer;
}); I would like to be able to set something if a value is set on the host, and if not return the original function. Please don't suggest:
Thanks for any suggestions, I will think of another way if this isn't possible. Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Use a separate task before deploy to set correct composer version. |
Beta Was this translation helpful? Give feedback.
-
Thanks @antonmedv, worked perfectly. For reference, this is what I ended up doing: /**
* Set config based on environment
*/
task('environment:prepare', function () {
switch (get('ll_deployer_environment')) {
case 'vps':
/**
* writable_use_sudo
* @package deployer
*
* Using sudo in writable commands?
*/
set('writable_use_sudo', true);
/**
* bin/composer
* @package deployer
*
* Set default composer path
*/
set('bin/composer', '/usr/local/bin/composer');
break;
case 'cpanel':
writeln('Environment: cPanel');
/**
* writable_mode
* @package deployer
*
* What writeable mode should we use?
*/
set('writable_mode', 'chmod');
break;
default:
writeln('Environment: Default');
break;
}
});
before('deploy:prepare', 'environment:prepare'); I can then do the following to use the specific config: host('production')
->set('ll_deployer_environment', 'cpanel') // can be cpanel or vps
->set('deploy_path', '/dir')
; |
Beta Was this translation helpful? Give feedback.
Thanks @antonmedv, worked perfectly.
For reference, this is what I ended up doing: