Deployer: Command line options not used

Created on 22 Nov 2017  Â·  9Comments  Â·  Source: deployphp/deployer

| Q | A
| ----------------- | ---
| Issue Type | Bug / Question
| Deployer Version | v6.0.3
| Local Machine OS | Ubuntu 14.04 in Drupalvm
| Remote Machine OS | Jelastic PHP environment

Description

I have many custom options that my deploy task asks -tag / branch to deploy for instance- but for some setups I need to run these deployments programmatically so I need the -o parameter to pass stuff to my deployments

I built these scripts a while back, and I'm pretty sure they worked at some point, but lately I haven't been able to get any of these options to register via command line.

Steps to reproduce

I'm generating command to be run with bash, and the full command is something like:
/home/vagrant/drupalvm/deploy/bin/dep -f=/home/vagrant/drupalvm/deploy/hosts/boogi/boogi-deploy.php deploy klubitre.stage --log logs/klubitre.log -o branch=develop -o ask_source=0 -o deployment_type="Branch"

Content of deploy.php

Here I want to ask a question about what to deploy IF ask_source option is set to 1. I have that ask_source defined in my hosts file to 1, but I want to override that with command line option and define the branch / tag to checkout via command line as well.

  if (has('ask_source') && get('ask_source') == 1) {
    $deptype = askChoice('Haluutko deployaa suoraan konffissa olevan branchin ('.get('branch').') vai antaa ite?', [
      0 => 'Oletuksesta',
      1 => 'Branch',
      2 => 'Tag',
      /*3 => 'Revision',*/
    ], 0);

    set('deployment_type',$deptype);
}

For some reason any of my -o parameters are not picked up and that question about deployment type is asked no matter what I set in command line..

Am I missing something here?

Cheers,
Janne

bug

Most helpful comment

So we'll just have to set the overwrite-able params in deploy.php instead of hosts.yml.

Yeah, this isn't perfect for us. Preferably we'd like to set these in hosts file, because we want different values for different stages PLUS optionally override them with CLI

All 9 comments

Add this before if: var_dump(get('ask_source', 'no-defined'));

/home/vagrant/drupalvm/deploy/bin/dep -f=/home/vagrant/drupalvm/deploy/hosts/boogi/boogi-deploy.php deploy klubitre.stage --log logs/klubitre.log -o branch=develop -o ask_source=0 -o deployment_type="Branch"
➤ Executing task deploy
int(1)
Haluutko deployaa suoraan konffissa olevan branchin (stage) vai antaa ite? [0]
[0] Oletuksesta
[1] Branch
[2] Tag
>

Looks like this doesn't work: -o ask_source=0
Send me your full deploy.php

Here's the deploy.php that runs the deployment.

namespace Deployer;

$this_folder = dirname(__FILE__);

require 'recipe/common.php';
require $this_folder.'/../../recipes/shared.php';
require $this_folder.'/../../recipes/d7.php';
require 'recipe/hipchat.php';

inventory($this_folder.'/boogi-hosts.yml');

// Project name
set('application', 'Boogi');

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

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

// Shared files/dirs between deploys
add('shared_files', []);
add('shared_dirs', []);

// Writable dirs by web server
add('writable_dirs', []);


/**
 */
set('timestamp', function(){
  return time();
});
set('release_name', function(){
  return get('timestamp');
});

/**
 */
set('release_path', function(){
  $relpath = get('deploy_path') . '/releases/' . get('release_name');
  writeln('Release path: '.$relpath);
  return $relpath;
});

set('vain_boogi_nimi',function(){
  $boogi_name = get('boogi_name');
  $explode = explode('.', $boogi_name);
  $retval = reset($explode);
  return($retval);
});

task('deploy',function(){

  invoke('deploy:info');
  set('deployment_stage',1);

  $stage = get('stage');
  $synkkaa_data = '';
  if ($stage == 'stage' || $stage == 'develop') {
    $synkkaa_data = askChoice('Haluuks synkkaa kannan livestä?', [
      0 => 'Joo',
      1 => 'En',
    ], 0);
  }

  invoke('deploy:prepare');
  set('deployment_stage',2);
  invoke('deploy:update_code');
  set('deployment_stage',3);
  invoke('deploy:release');
  set('deployment_stage',4);
  invoke('deploy:update_git_logs');
  set('deployment_stage',5);
  invoke('deploy:build');
  set('deployment_stage',6);
  invoke('deploy:backup');
  set('deployment_stage',7);

  if ($synkkaa_data == 'Joo') {
    invoke('stage_data');
    set('deployment_stage',8);
  }

  invoke('deploy:drush');
  set('deployment_stage',9);
  invoke('deploy:symlink');
  set('deployment_stage',10);
  invoke('deploy:unlock');
  set('deployment_stage',11);
  invoke('cleanup');
  set('deployment_stage',12);
  invoke('maintenance_mode_0');
  set('deployment_stage',13);
  invoke('success');
  set('deployment_stage',14);
  invoke('hipchat:notify');
  set('deployment_stage',15);

});


task('stage_data',function(){

  // pitää ajaa tää aliasfunkkarilla
  run_drush_alias("@boogi.{{vain_boogi_nimi}}.live sql-dump > /tmp/prod.sql");
  // pitää droppaa kanta välissä
  run_drush('sql-drop -y');
  // ja normi sql tuonti
  run_drush("sqlc < /tmp/prod.sql");

})->onStage('stage');

task('testaa',function(){

  #print_r(get('file_operations'));


  if (has('tag')) {
    $tag = get('tag');
    writeln(" @ tag <fg=magenta>$tag</fg=magenta> ");
  }

  writeln("{{release_path}}");


  if (has('file_operations')) {
    $operations = get('file_operations');
    print_r($operations['deploy:build']);

    foreach ($operations['deploy:build'] as $command => $operation) {

      foreach ($operation as $from => $to) {

        if (is_numeric($from)) {
          writeln("$command $to");
        }
        else {
          writeln("$command $from $to");
        }
      }
    }

  }
});



after('deploy:failed', 'rollback');

And then here's the deploy:info that we've modified a bit.

/**
 *
 */
desc('Deploy infot + backup polku');
task('deploy:info', function () {

  var_dump(get('ask_source', 'no-defined'));

  if (has('ask_source') && get('ask_source') == 1) {
    $deptype = askChoice('Haluutko deployaa suoraan konffissa olevan branchin ('.get('branch').') vai antaa ite?', [
      0 => 'Oletuksesta',
      1 => 'Branch',
      2 => 'Tag',
      /*3 => 'Revision',*/
    ], 0);

    set('deployment_type',$deptype);

    // jos haluttu syöttää ite
    if ($deptype == 'Branch') {
      // kysytään branssi
      $read_branch = ask('Anna branch / julkasu','master');
      // tsekataa, että annettu branch on olemassa.
      $ret = runln("git ls-remote --heads {{repository}} $read_branch | wc -l");
      // outputti pitänee muuttaa intiks ni testi toimii
      $testi = intval($ret);
      if ($testi == 0) {
        set('branch',$read_branch);
      }
    }

    if ($deptype == 'Tag') {
      // kysytään tägi
      $read_tag = ask('Anna TAG: @'.get('branch'));
      // tsekataa, että annettu branch on olemassa.
      $ret = runln("git ls-remote {{repository}} --tags origin $read_tag | wc -l");

      // outputti pitänee muuttaa intiks ni testi toimii
      $testi = intval($ret);
      if ($testi == 0) {
        set('tag',$read_tag);
      }
    }
  }

  writeln('Info. Release name: {{release_name}}');

  $what = '';
  $branch = get('branch');

  if (!empty($branch)) {
    $what = "<fg=magenta>$branch</fg=magenta>";
  }

  if (has('tag')) {
    $tag = get('tag');
    $what .= " @ tag <fg=magenta>$tag</fg=magenta> ";
  }

  if (empty($what)) {
    $what = "<fg=magenta>HEAD</fg=magenta>";
  }

  writeln("✈︎ Deploying $what on <fg=cyan>{{url}}</fg=cyan>");
})
  ->shallow()
  ->setPrivate();

I'm a workmate of @jiisuominen .
It seems we can only overwrite parameters which are set with the set()-method in deploy.php but not those defined in hosts.yml.
So we'll just have to set the overwrite-able params in deploy.php instead of hosts.yml.

So we'll just have to set the overwrite-able params in deploy.php instead of hosts.yml.

Yeah, this isn't perfect for us. Preferably we'd like to set these in hosts file, because we want different values for different stages PLUS optionally override them with CLI

This could be done. Can you create self-contained example to better understand the problem? I'll try to solve it on this weekends.

Here's an example. The parameter defined in test-hosts.yml cannot be overwritten from cli:

test-hosts.yml:

develop:
  stage: develop
  # This can not be overwritten with -o:
  param_in_hosts: "This param is set in test-hosts.yml"

test-deploy.php:

<?php
namespace Deployer;
require 'recipe/common.php';

inventory(__DIR__ . '/test-hosts.yml');

// This can be overwritten with -o:
set('param_in_deploy', 'This param is set in test-deploy.php');

task('test_params', function() {
  writeln('param_in_hosts: ' . get('param_in_hosts'));
  writeln('param_in_deploy: ' . get('param_in_deploy'));
});

Attempting overwrite:

$ ./bin/dep -f=test-deploy.php test_params develop -o param_in_hosts="Overwrite from cli" -o param_in_deploy="Overwrite from cli"
➤ Executing task test_params
param_in_hosts: This param is set in test-hosts.yml
param_in_deploy: Overwrite from cli
✔ Ok

Fixed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dima-stefantsov picture dima-stefantsov  Â·  5Comments

ovaiskhan11 picture ovaiskhan11  Â·  4Comments

lsv picture lsv  Â·  4Comments

timkley picture timkley  Â·  4Comments

osbulbul picture osbulbul  Â·  3Comments