Override environment variable when running on Jenkins
Asked Answered
T

2

6

I'm testing a Zend Framework application using PHPUnit and Jenkins. I need to override the APPLICATION_ENV environment variable which is access using PHP's 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 can I set the variable to testing-ci when it runs in Jenkins? Is there any way to set it in build.xml for Ant or Phing?

Tawana answered 3/2, 2012 at 12:30 Comment(0)
A
7

Step 1: Add the environment variables to Jenkins.

Open either the global or project-specific configuration page depending on your needs and scan down for the Environment variables section. Check the checkbox and use the Add button to add key/value pairs.

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

Step 2: Load them into Ant.

Near the top of your Ant build.xml script, load all environment variables with an env prefix so they don't interfere with other properties.

<property environment="env"/>

Now all imported variables will be available using the env prefix, e.g. ${env.HOME}.

Step 3: Pass them to PHPUnit.

Assuming you're using the <exec> task to run PHPUnit, you can pass each needed variable to it using the <env> child element.

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

Note: You might want to try just the first step to see if Ant will pass the environment variables along to executed child processes, but I think the other two steps are good for making it clear what is required to other developers.

Allium answered 4/2, 2012 at 21:29 Comment(2)
Simpler and more elegant, I'll grant you that. But mine works via phpunit itself without an "ant" dependency.Tajuanatak
@TheodoreR.Smith - Rafael specifically asked about Jenkins and Ant (build.xml) or Phing.Allium
T
0

OK.

Here's what you do...

First, create a new file called bootstrap.php.

Next, in boostrap.php, put the following code:

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

Load the bootstrap.php into your testsuite or (even better) phpunit.xml.

Finally, via your CI build config, or via the console or wherever, execute your unit tests like phpunit UnitTest.php --environment dev.

You're good to go.

Tajuanatak answered 4/2, 2012 at 0:21 Comment(5)
Sorry I don't know if you understood me but I already have bootstrap.php and phpunit.xml, nowdays It works locally on my machine database but when goes to jenkins it's another database password. I need a solution to change the env variable in jenkins configuration or in the build.xml so that won't break my local testing and will work on jenkins.Tawana
Oh I'm sorry. I'm editing my post now with an updated solution.Tajuanatak
It would be a solution however in my case I'm running a suite test case configured by xml (phpunit --configuration ./tests/phpunit.xml). PHPUnit doesn't accept receive --environment but anyway thanks :)Tawana
Yes it does. Man. Do some research before you dismiss my answer. phpunit.de/manual/3.6/en/appendixes.configuration.html See the PHP INI settings. You'd want <env name="env" value="dev"/>.Tajuanatak
I know it, I was using it before but this solution needed 2 config xml (phpunit.xml and phpunit-ci.xml) it works but I need to keep two xml files otherwise it will apply the environment to my local machine and jenkins.Tawana

© 2022 - 2024 — McMap. All rights reserved.