Override Jenkins Environment Variable

I am testing a Zend Framework application using PHPUnit and Jenkins. I need to override the environment variable APPLICATION_ENV , which is accessed using PHP getenv in the PHPUnit bootstrap.php file:

 <?php // Define application environment defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing')); ... use APPLICATION_ENV to pick database, configuration, etc. ... 

I have two environments: testing (for local machines) and testing-ci (for Jenkins machine). How to set a variable in testing-ci when it works in Jenkins? Is there a way to install it in build.xml for Ant or Phing?

+6
source share
2 answers

Step 1: Add environment variables to Jenkins.

Open the global or project configuration page, depending on your needs, and scan for the Variable Environment section. Check the box and use the Add button to add key / value pairs.

They will be passed by Jenkins to your Ant build script.

Step 2: Download them to Ant.

At the top of your Ant build.xml script, load all environment variables with the env prefix so that they do not interfere with other properties.

 <property environment="env"/> 

Now all imported variables will be accessible using the env prefix, for example. ${env.HOME} .

Step 3: Pass them to PHPUnit.

Assuming you use the <exec> task to run PHPUnit, you can pass each variable it needs using a child element of <env> .

 <exec taskname="test" executable="phpunit"> <env key="APPLICATION_ENV" value="${env.APPLICATION_ENV}"/> ... </exec> 

Note. . You can only try the first step to see if Ant will pass environment variables along with executable child processes, but I think the other two steps are good for creating it clearly what other developers need.

+7
source

OK

Here is what you do ...

First create a new bootstrap.php file.

Then, in boostrap.php, enter the following code:

 if (!empty($argv) && ($key = array_search('--environment', $argv)) !== FALSE) { $env = $argv[$key + 1]; putenv('APPLICATION_ENV=' . $env); } 

Download bootstrap.php in testuite or (even better) phpunit.xml.

Finally, through the configuration of the CI assembly or through the console or somewhere, run your unit tests, for example phpunit UnitTest.php --environment dev .

You will go well.

0
source

Source: https://habr.com/ru/post/907564/


All Articles