We are working on the Spring Boot web application, and the database we use is MySql ;
we have settings that we first test locally (this means that we need to install MySql on our PC);
then we click on bitbucket ;
Jenkins automatically detects a new push request to Bitbucket and builds it (in order to transfer Jenkins mvn build, we also need to install MySql on the virtual machines running Jenkins).
if the Jenkins build is successful, we send the code to our application in OpenShift (using the Openshift deployment plugin in Jenkins).
The problem we are facing, as you may have already understood, is this:
in application.properties we cannot hardcode MySql information. Since our project will be executed in 3 different places (local, Jenkins and OpenShift), we need to make the data source field dynamic in application.properties (we know that there is another way to do this, but we are working on this solution now),
spring.datasource.url = spring.datasource.username = spring.datasource.password =
The solution we came up with is that we create system environment variables locally and in Jenkins vm (naming them the same as OpenShift calls them) and assigning them the correct values respectively:
export OPENSHIFT_MYSQL_DB_HOST="jdbc:mysql://localhost" export OPENSHIFT_MYSQL_DB_PORT="3306" export OPENSHIFT_MYSQL_DB_USERNAME="root" export OPENSHIFT_MYSQL_DB_PASSWORD="123asd"
We did it and it works. We also checked with Map<String, String> env = System.getenv(); that environment variables can be converted to Java variables as such:
String password = env.get("OPENSHIFT_MYSQL_DB_PASSWORD"); String userName = env.get("OPENSHIFT_MYSQL_DB_USERNAME"); String sqlURL = env.get("OPENSHIFT_MYSQL_DB_HOST"); String sqlPort = env.get("OPENSHIFT_MYSQL_DB_PORT");
Now it remains only to use these java variables in our application.properties and we have problems with this.
In which folder and how should we assign the variables password , userName , sqlURL and sqlPort to application.properties so that they can be seen, and how can we include them in application.properties ?
We tried many things, one of which:
spring.datasource.url = ${sqlURL}:${sqlPort}/"nameofDB" spring.datasource.username = ${userName} spring.datasource.password = ${password}
So far no luck. We probably do not put these env variables in the desired class / folder or use them incorrectly in application.properties .
Your help is much appreciated!
Thank!
java spring spring-mvc mysql openshift
SM Feb 21 '16 at 2:39 2016-02-21 02:39
source share