Could I override the grails.env during runtime - grails

Grails seems to build the artefacts per env.
with the command "grails -Dgrails.env=test war
Is it possible for me to override the running environment programmatically?
The issue here is that we produce the war file using production env config. But I want to run the same war file in other environments, like Test, Staging, DR etc.
There are two options:
1. set grails.env in command line
2. programmatically set the current env in the code.
I wonder how could I achieve option2. Our deploy script is shared, not grails specific. There is a generic environment variable named SYSTEM is passed in command line. If I could map that env to the grails.env in code, that would be idea.

Related

How to unset (or hide) all environment variables prior to starting pytest

In Jenkins, I am running my pytests inside of a Docker container that has several environment variables set. Before running ANY tests, I would like to ensure that these environment variables are empty or not-available to the code running in the tests.
Why do this?
I want to understand the dependencies of my source code on the environment variables. If a chunk of code requires an environment variable be set, I would prefer to explicitly set/mock it.
I want developers to be able to easily run the tests locally in their IDE without needing to do complex env setup. Right now people frequently add code that depends on the environment variables which causes the tests to break when running locally.
Is there a pytest fixture that I should consider using to unset or hide all environment variables?

Unable to access environment variable in Bamboo Build Plan

I have a Bamboo Build Plan, with the following set of tasks.
Source Code Checkout
Artifactory Generic Resolve (To Get the zip file from Artifactory)
Script (To Extract the contents of zip file and to set to CATALINA_HOME & PATH environment variable)
Ant (For Build)
Task 3 has the following content in it:
APP_HOME=${bamboo.build.working.directory}
unzip $APP_HOME/tomcat/apache-tomcat-6.0.45-windows-x64.zip
export CATALINA_HOME=$APP_HOME/tomcat/apache-tomcat-6.0.45
export PATH="$PATH:$CATALINA_HOME/bin"
But when I execute 4th Task (Ant), the Build is not considering the CATALINA_HOME & PATH variable which is set as part of Task 3. What is wrong here? Why am I not able to access the environment variable that is set in Task 3?
Every Script Task runs in its own non-interactive shell, eventually invoked through the ExternalProcessBuilder. Existing environment variables are made available to the process (i.e. shell), as well as the additional environment variables defined in the task itself as documented. However, newly exported variables are not carried over to the next task as it is an entirely new, isolated shell.
What you could do is to dump the export statements to a file, and 'source' that file at the start of the next script task.

How to build a custom environment in Grails

I'm trying to add a new environment to our Grails WAR (let's call it "staging"). I can manage the configuration in Config.groovy and DataSource.groovy and access the right configuration at run-time with -Dgrails.env, but how do I build this WAR?
The Grails documentation does not cover this case and the links on the page seem to be outdated.
You are so very close to having the right combination in your question, this should work:
grails -Dgrails.env=staging war
Actually, the documentation for the war command even uses 'staging' as the environment used.
The same goes for any environment-specific command:
grails -Dgrails.env=<environment name> <command>

Continuous Deployment with Codeship doesn't recognize environment variables

Recently I started to use Codeship as CI/CD tool for a small website that I am maintaining. I set up my Codeship project to deploy via sftp as described in their guide here.
The part where it fails is in the production script. I created a deploy folder and a production.sh script which contains the line:
put -rp "${HOME}/clone/build/*" /path/to/remote/dir
However when running the build I get the following error:
sftp> put -rp "${HOME}/clone/build/*" /path/to/remote/dir
stat ${HOME}/clone/build/*: No such file or directory
Echoing $HOME in a test script directly in Codeship gives me my home directory, so the environment variable works. However, at the moment the batch script is run, the environment variable is unrecognized.
How can I fix this? I'd rather not hardcode the path in my deployment script. It also doesn't seem possible that this happens because I suffixed production.sh, whereas in the docs they only have a production script?
With no answer coming from the people from Codeship, I resulted to writing the absolute path to the ${HOME} directory. I've been doing this for a time now with a few different projects and it all seems to work.
replace ${HOME}/clone with ~/clone
this worked for me

Override environment variable when running on Jenkins

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?
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.
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.

Resources