0

I'm trying to make a complete development to the production pipeline.

In this scenario, I want to have a Jenkins job deploy the Java Spring application once a new commit is done.

An example of some of my applications.propperties is:

spring.datasource.url=jdbc:mariadb://localhost:3306/application-api?serverTimezone=CET
spring.datasource.username=USERNAME
spring.datasource.password=PASSWORD

Once you have this in a git repository your credentials are stored and visual for everyone. I don't have the plan to make anything public yet but seems like a bad idea to store application.properties with passwords on a git repository.

So best case scenario I want something like this, where I can set environment vars on my deployment server: spring.datasource.username=${DB_PASS}:PASSWORD In case of environment variable DB_PASS doesn't exist

I'm stuck on how to achieve this. I have seen the usage of @Value but I'm unaware of how to use that with given application properties.

0xh3xa
  • 4,801
  • 2
  • 14
  • 28
JasperFennet
  • 53
  • 10
  • Does this answer your question? [Using env variable in Spring Boot's application.properties](https://stackoverflow.com/questions/35531661/using-env-variable-in-spring-boots-application-properties) – 0xh3xa Mar 22 '20 at 15:44

2 Answers2

3

You can use all properties also as environment variables.

Simply make the name uppercase and replace . by _

SPRING_DATASOURCE_URL=jdbc:mariadb://localhost:3306/application-api?serverTimezone=CET
SPRING_DATASOURCE_USERNAME=USERNAME
SPRING_DATASOURCE_PASSWORD=PASSWORD

Please read the documentation: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
  • the link helped me to discover the profile properties usage. but this still keeps the problem to replacing on repo's – JasperFennet Mar 23 '20 at 11:21
0

Why are you storing the github credentials in application.properties?

Consider connecting Jenkins to github via Webhooks:

Here is the plugin for Jenkins

Here is a tutorial on setting up and using Github webhooks with Jenkins

EDIT 3/24/2020: You can consider having a properties file that is external to the project and add it to the classpath (simply don't commit it) using something like @PropertySource. Further, once you build your project, you can place a properties file in the same directory and it will be picked up by default when you start your spring boot application.

Example:

C:<path to project>/target/project.jar

C:<path to project>/target/custom-properties.properties

then

custom-properties.properties

will be loaded on start up.

Fermi-4
  • 645
  • 6
  • 10