Phing exec command to set environment variable
Asked Answered
L

3

7

I'm trying to set an environment variable in a build script with phing. This is normally done command line like this:

export MY_VAR=value

In Phing I did the following but it isn't working.

<exec command="export MY_VAR=value" />
Labrecque answered 27/4, 2011 at 13:23 Comment(0)
F
6

Bold claim: There is no way to set/export a (Unix) shell variable in PHP so that it is visible inside the scope that started the php script.

php myfile.php (does putenv or shell_exec('export foo=bar');)
echo $foo

Will return nothing. As PHP can not do it so neither can phing.

Accessing shell environment variables accross multiple script runs (if its that what you want) seems also like an unideal design decision, pretty stateful.

Apart from that I'd urge you to stick to phing and learn its lean lesson. Phing helps stateless thinking to some degree.

Faqir answered 28/4, 2011 at 13:40 Comment(0)
S
10

I see that this is quite an old question, but I don't think it has been answered in the best way. If you wish to export a shell variable, for example say you are running phpunit from phing and want to do an export before invoking phpunit, try:

<exec command="export MY_VAR=value ; /path/to/phpunit" />

Simply do the export and invoke your command inside the same exec tag. Separate the export statement and the shell executable with a semicolon as shown. Your script will be able to access the value using the standard php function:

$myVar = getenv('MY_VAR');
Saltire answered 15/7, 2014 at 13:35 Comment(2)
This is exactly my use case and exactly the right answer. You are a scholar and a gentleman.Withdrew
having export is not portable, but is suitable for single-platformTimikatiming
F
6

Bold claim: There is no way to set/export a (Unix) shell variable in PHP so that it is visible inside the scope that started the php script.

php myfile.php (does putenv or shell_exec('export foo=bar');)
echo $foo

Will return nothing. As PHP can not do it so neither can phing.

Accessing shell environment variables accross multiple script runs (if its that what you want) seems also like an unideal design decision, pretty stateful.

Apart from that I'd urge you to stick to phing and learn its lean lesson. Phing helps stateless thinking to some degree.

Faqir answered 28/4, 2011 at 13:40 Comment(0)
C
4

I'd never heard of phing before, but this looks very promising as a build tool. Thanks for posting! I looked through the doc on phing.info, I found the following possibility:

#0 I would like to clarify one point. Are you saying that

prompt$ > export MY_VAR=value
prompt$ > phing build.xml

doesn't set MY_VAR to value so it is visible inside the running phing processes? I'd be surprised, but I would understand if this is not how you want to run your build script.

#1 I think in the context of a build tool, a feature like exec is meant to run a stand-alone program, so, while the exec may run and set MY_VAR, this is all happening in a subprocess that disappears immediately as the exec finishes and continues processing the next task in the build.xml.

If you're just trying to ensure that your phing script runs with specific values for env_vars, you could try

Command-line arguments:
....
-D<property>=<value>    
  // Set the property to the specified value to be used in the buildfile

So presumably, you can do

phing -DMY_VAR=value build.xml

#2 did you consider using a properites file? See http://www.phing.info/docs/guide/stable/chapters/appendixes/AppendixF-FileFormats.html and scroll down for info on build.properties

#3 also ...

Phing Built-In Properties
Property    Contents
    env.*   Environment variables, extracted from $_SERVER.

you would access them with something like

 ${env.MY_VAR}

#4 This looks closer to what you really want

<replacetokens>
  <token key="BC_PATH" value="${top.builddir}/"/>
  <token key="BC_PATH_USER" value="${top.builddir}/testsite/user/${lang}/"/>
</replacetokens>

I hope this helps.

Cymoid answered 27/4, 2011 at 13:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.