Using envionment variables for database connection string
Asked by Sushil Nembhani:

Hi All,  

I have currently java application which has the database.properties file for storing the connection string, user \& password.

Instead of storing these details in the bitbucket repository i would like to use the environment variables.

How would i do this using the Bitbucket pipeline or Bamboo ?

regards,

Sushil


Answers:
author: Daniel Santos
body: Hi [@Sushil Nembhani](/t5/user/viewprofilepage/user-id/3521120)

It is difficult to understand what part of this process you are looking for just from your question. Do you want to know how to declare the variables, how to consume them in the JAVA application, how to run the declare password variables...?  

Regarding the variables for Bamboo, I encourage you to check this document:

* [Bamboo variables - Atlassian Documentation](https://confluence.atlassian.com/bamboo/bamboo-variables-289277087.html)  
  It will share other pages that will give you details about the global, plan and deployment variables.  
  ?? If you are attempting to use a Bamboo variable in a script task, please make sure you reference it with the naming convention they have when showing in the terminal.  
  The variable **${bamboo.my.var}** would be available as **$bamboo_my_var** .  

Regarding Pipelines ones, please check this one:

* [Variables in pipelines - Atlassian Documentation](https://confluence.atlassian.com/bitbucket/variables-in-pipelines-794502608.html)  
  In this one, you will see sections with the following variable types: team/individual accounts, repository, and deployment.

You will need to choose where is the best location to store the variable you need.  

If you are looking for tips on how to load the variable data into your JAVA application through a script or terminal, then I think this is not the ideal forum. I would encourage you to drop this question on a JAVA specific one.  

I hope I could help.

comments: author: Sushil Nembhani
body: Hi Daniel,  

My first question is instead of storing the database connection string , user name \& password in bitbucket, how can i use the environment variables so that i do not have to change the config file when i moved from dev to staging to prod ?

My second question is should i be using environment variables provided in bamboo or use the variables in the bitbucket pipelines as both do provide the option to use environment variables ?


author: Daniel Santos
body: Let me share how I see you could do this for Bamboo. A similar solution could be used in case you are deploying your project using Pipelines.

1. Create a deployment project with environments for dev, staging, and production.
2. Create the variables you need for user, connection string, password (and any other needed) with the same name in all three environments.
3. Design your application to read those variables
4. Each variable will have a different value configured for it but the same names and that will allow you to have different values for each environment.

I was a little bit confused because I was considering that you were talking about two different solutions, one for Bamboo and another for Pipelines. I'm now thinking that you are only using Bamboo connected to Bitbucket and considering that Bitbucket Pipelines variables could be used in Bamboo. Am I correct?  

<br />


author: Sushil Nembhani
body: Thanks for providing the details.

I am currently using both bitbucket \& bamboo for the entire devops CI/CD.

I would like to know for my case if i need to use the environment variables within **Bitbucket** or **Bamboo** for the storing the connection string details.

Also how would my application (Java or .net) read the bamboo/bitbucket environment variables, i could not find any sample case studies or sample code showing the same.

Regards,

Sushil Nembhani


author: Daniel Santos
body: Hi [@Sushil Nembhani](/t5/user/viewprofilepage/user-id/3521120)

If you are running Pipelines builds you need to use Bitbucket variables, if you are using Bamboo builds you need to use Bamboo variables. You need to have the variables set in the same build environment running. You cannot use Bamboo variables in Bitbucket Pipelines or vice-versa.

Regarding the way to read the variables, as I shared before, that is a matter of JAVA or .net technology that is not related to Bamboo or Bitbucket. You will need to research this specifically on other channels. I've just run a search for this and found a thread in stack overflow for the JAVA case: [Can we read the OS environment variables in Java? - Stack Overflow](https://stackoverflow.com/questions/20610080/can-we-read-the-os-environment-variables-in-java/20610185).

The variables will be available in the build environment when the application is running or when the compilation is running. You will need to seek methods to read them that will be handled by the tool you are using at that moment. For instance, anything that works in the OS terminal should work under Bamboo as well.


author: Sushil Nembhani
body: Thanks Daniel. I will check in bamboo as per the steps provided by you.


Tags: bitbucket-pipelines

bitbucket-server

variable