6

I have simple Envoy setup. A server:

@servers(['ws' => 'ws.sk'])

... And simple "ping" task:

@task('ping-ws', ['on' => 'ws'])
    echo "Hello world from WS server!"
    echo $(pwd)
    pwd
    var_1="Hello"
    echo "${var_1}"
@endtask

Where I would like to assign some values to variables and access them later. Although the result is quite unexpected:

envoy run ping-ws
Hello world from WS server!
/Users/davidlukac/dev/drupal/_devdesktop/davidlukac
/home
  1. The $(pwd) command is evaluated locally.

  2. Variable var_1 is either not assigned, or out of scope on the next line.

Is this expected behaviour? Is there a workaround for it?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
David Lukac
  • 728
  • 7
  • 20
  • Similar issue: when invoking a `bash` script from an Envoy task, that includes _pipe_ (`|`; `command | grep "something"`), this interrupts the script and task, when it gets to the pipe. – David Lukac Mar 18 '16 at 17:07

2 Answers2

2

Looking at the code, we can see the method being used to pass the commands. First the command is built:

ssh ws.sk 'bash -se' << EOF-LARAVEL-ENVOY
echo "Hello world from WS server!"
echo $(pwd)
pwd
var_1="Hello"
echo "${var_1}"
EOF-LARAVEL-ENVOY

And then, that command is sent off to be run by PHP's proc_open command.

Since the input is being passed via STDIN, it's getting interpreted by your local environment before being sent. You can copy and paste the above into your terminal to see the same thing.

All that's needed is to escape any characters that might be interpreted by the local environment; in this case, the $ characters.

@task('ping-ws', ['on' => 'ws'])
    echo "Hello world from WS server!"
    echo \$(pwd)
    pwd
    var_1="Hello"
    echo "\${var_1}"
@endtask

Note you may need to double escape, not sure if Envoy will try to take the first escaping for itself.

miken32
  • 42,008
  • 16
  • 111
  • 154
  • Glad to help. I'd suggest this should be filed as a bug in the product. It should be doing the escaping for you at run time. – miken32 Mar 17 '16 at 21:27
1

If needed, you may pass option values into Envoy tasks using the command line:

envoy run deploy --branch=master

You may access the options in your tasks via Blade's "echo" syntax. Of course, you may also use if statements and loops within your tasks. For example, let's verify the presence of the $branch variable before executing the git pull command:

@servers(['web' => '192.168.1.1'])

@task('deploy', ['on' => 'web'])
    cd site

    @if ($branch)
        git pull origin {{ $branch }}
    @endif

    php artisan migrate
@endtask

It's from official envoy documentation, so you are welcome to learn more

If you want to add more than one variable just append as much as you need.

envoy run deploy --var1=var1Value --var2=var2Value
Andrew
  • 671
  • 2
  • 8
  • 21