1

I have tried unsuccessfully accessing OpenShift environment variables either in application-config.xml or in POM.xml

application-config.xml

 <mongo:db-factory id="mongoDbFactory" mongo-ref="mongo"                 
    dbname="testMongoDb"
        username="${OPENSHIFT_MONGODB_DB_USERNAME}"
                  password="${OPENSHIFT_MONGODB_DB_PASSWORD}"
                  />

POM.xml

<properties>
              <db.userName>${env.OPENSHIFT_MONGODB_DB_USERNAME}</db.userName>
              <db.password>${env.OPENSHIFT_MONGODB_DB_PASSWORD}</db.password>
              <db.url>mongodburl.openshift.com</db.url>
              <db.port>99999</db.port>
            </properties>
            <build>
                <finalName>testFinalName</finalName>
                <resources>
                    <resource>
                        <directory>src/main/resources/</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>

None of the above options worked. has anyone able to access env varilabes while deploying to Openshift?

pavan
  • 11
  • 2
  • I have no time to check it, but is it possible, that these environment variables are not available on OpenShift's Jenkins? Notice that integration test are running on another gear named bldr. – Christian Strempfer Mar 15 '14 at 13:04
  • @Chris I came to same conclusion as you; so probably I will have to rephrase my question - how can we make environment variables available to Jenkin's or bldr instance? – pavan Mar 15 '14 at 16:40

3 Answers3

1

By the way, none of these are OpenShift questions.

But to answer your first part of your question, you should be aware that Spring does not resolve environment variables by default but you can enable the PropertyPlaceholderConfigurer in your configuration file to fix this.

To answer your second question, please look here How to refer environment variable in POM.xml?

Community
  • 1
  • 1
MartinB
  • 231
  • 1
  • 5
  • I understand spring doesn't resolve properties however OpenShift uses Jenkins to create build and subsequently deploy build to their platform. I am expecting their build process will replace environment variables in POM. As you link pointed out I have referred environment variables as ${env.XXXXXXXX} ; Openshift Jenkins while creating build process it is not replacing env variables as expected. – pavan Mar 14 '14 at 23:22
  • Your statement about Openshift isn't correct, it uses standard maven to build and deploy java applications. The use of Jenkins is optional. Please change the name of this issue – MartinB Mar 16 '14 at 10:43
0

Jenkins doesn't know the environment variables.

These environment variables are not available on Jenkins gears, they only exist on an "application gear" with a connected database.

Consider runtime configurations.

You're trying to hardcode the database settings in your deployment artifact. That might be a disadvantage. For example if you deploy to different environments for test and production, you have to build your application multiple times. Therefore you should decide if that's a problem for you. If it is, you can read environment variables from within your Java application easily.

You have to set the environment variables yourself.

There is no easy way to access environment variables of another gear, so you have to set them yourselves (or hardcode them in your pom.xml, which is bad practice at least for passwords).

See knowledge article How do I connect multiple applications to one database? for confirmation that only hard coding is possible.

Christian Strempfer
  • 7,291
  • 6
  • 50
  • 75
  • I have created profile for each environment similar to described as [link](http://slackspace.de/articles/create-deployment-profiles-with-maven/). One of my intentions is to avoid creeping up OpenShift variables into application; I want to consolidate environment variables in deployment profile so it is easy change/manage deployment location. – pavan Mar 15 '14 at 18:01
  • @pavan: The author is hardcoding the database parameters in the pom.xml, but you want to avoid it. Therefore you need to read the environment variables at runtime or set the environment variables on jenkins. – Christian Strempfer Mar 15 '14 at 18:15
0

I had a similar situation lately:

  1. Environment variable was set for Openshift application with rhc client
  2. This variable was visible for user logged over SSH

but

  1. While running start action hook script from Jenkins, the variable was not visible
  2. For test Jenkins job with echo $MY_NEW_VARIABLE it was empty as well

I need to admit that I have a custom Openshift configuration of DIY node, but maybe it will be helpful for someone.

The solution was to reconnect the slave in node configuration (this is something different than marking node offline):

Disconnect Jenkins node

Re-launch Jenkins node

Witek
  • 125
  • 1
  • 6