Search code examples
spring-bootyamlenvironment-variables

How to inject github secrets in application.properties file of Spring boot application?


How can I inject a GitHub secret in the application.properties file of a Spring-Boot application before the execution of build and deploy?

This is my current YAML file:

name: Heroku deployment

on:
  push:
    branches: [ master ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: Deploy to Heroku
      uses: AkhileshNS/[email protected]
      with:
        heroku_api_key: ${{secrets.HEROKU_API_KEY}}
        heroku_email: ${{secrets.HEROKU_EMAIL}}
        heroku_app_name: event-handler

This is my application.properties file:

spring.datasource.url=
spring.datasource.username=
spring.datasource.password=

I currently inject some environmental variables to handle Heroku deployment, I would like to update my YAML file so as to inject also the values of database properties from GitHub secrets. Is it possible to do it using a similar approach? The attempt to align the YAML file as if it was an application.properties did not work, it seems not quite right.


Solution

  • Prepare application.properties file for string replacement:

    spring.datasource.url=<db_url>
    spring.datasource.username=<db_username>
    spring.datasource.password=<db_password>
    

    And modify yaml file with additional commands:

    name: Heroku deployment
    
    on:
      push:
        branches: [ master ]
    
    jobs:
      build:
    
        runs-on: ubuntu-latest
    
        steps:
        - uses: actions/checkout@v3
    
        - name: Update db url
          run: sed -i 's/<db_url>/${{ secrets.DB_URL }}/' src/main/resources/application.properties
    
        - name: Update db username
          run: sed -i 's/<db_username>/${{ secrets.DB_USERNAME }}/' src/main/resources/application.properties
    
        - name: Update db password
          run: sed -i 's/<db_password>/${{ secrets.DB_PASSWORD }}/' src/main/resources/application.properties
    
        - name: Deploy to Heroku
          uses: AkhileshNS/[email protected]
          with:
            heroku_api_key: ${{secrets.HEROKU_API_KEY}}
            heroku_email: ${{secrets.HEROKU_EMAIL}}
            heroku_app_name: event-handler