'Generate env specific properties file in gitlab-ci.yml for gitlab runner

I need to automate the CI/CD pipeline for an SpringBoot application. This application has application.properties file which contains few obvious env specific properties like DB properties, Kafka properties etc.

For each of my env. where the gitlab runner tuns, we have commands which provides properties to be used in that env. e.g. user which runs the gitlab runner has script getDBURL which returns DB URL of that env. Same script is available in all env which returns those env. specific values.

So, if the build is running in QA env, I need to replace DBURL, DBPass, etc. in apllication.properties file & generate it as an artefact along with jar/war build for the app.

How to write gitlab-ci.yml for such configuration?

My existing file that generates jar looks like:

stages:
  - build
  - publish

build-code-job:
  stage: build
  before-script: #some cleanup 
  script:
    - echo "build app1"
    - mvn  $MAVEN_CLI_OPTS clean package

publish-nexus:
  stage: publish
  script: 
   - mvn $MAVEN_CLI_OPTS deploy -Dmaven.test.skip=true 
   - echo "Publishing to local repository" 
  only: 
   - master

Appreciate any help to generate application.properties file which will generate env specific configuration in it for gitlab runner.



Solution 1:[1]

Not sure if there's any elegant way but I implemented it with simple script to find and replace environment specific values.

Lets say below snippet is part of your application.properties file:

spring.datasource.url=jdbc:oracle:thin:@//<<DB_SERVER>>:<<DB_PORT>>/<<DB_SERVICE>>
spring.datasource.username=<<DB_USER>>
spring.datasource.password=<<DB_PASSWORD>>

There must be some way to get above environment specific values from your server where runner runs the build job. Below script accepts the source & destination file path, copies source file to build (destination) path & then does the find & replace as per values retrieved from server env:

updatePropertiesFile.sh

#!/bin/bash

SOURCE_FILE=$1
DEST_FILE=$2

echo "Extracting env variables"
dbhost=$(get_db_host)
dbport=$(get_db_port)
dbservicename=$(ge_tservicename)
dbuser=$(get_db_user)
dbpassword=$(get_db_password)

echo "Copy original properties file to destination"
cp -v -p $SOURCE_FILE $DEST_FILE

echo "Updating the env variable in destination file"
#echo "Replacing <<DB_SERVER>> to $dbhost"
sed -i s/\<\<DB_SERVER\>\>/${dbhost}/g application.properties

#echo "Replacing <<DB_PORT>> to $dbport"
sed -i s/\<\<DB_PORT\>\>/${dbport}/g application.properties

#echo "Replacing <<DB_SERVICE>> to $dbservicename"
sed -i s/\<\<DB_SERVICE\>\>/${dbservicename}/g application.properties

#echo "Replacing <<DB_USER>> to $dbuser"
sed -i s/\<\<DB_USER\>\>/${dbuser}/g application.properties

#echo "Replacing <<DB_PASSWORD>> to $dbpassword"
sed -i s/\<\<DB_PASSWORD\>\>/${dbpassword}/g application.properties

echo "$DEST_FILE updated successfully"

Now that the script is ready to it's job, let's call it from the gitlab-ci.yml as below:

build-code-job:
  stage: build
  before-script: #some cleanup 
  script:
    - echo "build app1"
    - mvn  $MAVEN_CLI_OPTS clean package
    - sh $BUILD_CHECKOUTDIR/$CI_PROJECT_NAME/bin/updatePropertiesFile.sh $BUILD_CHECKOUTDIR/$CI_PROJECT_NAME/src/main/resources/application.properties $BUILD_CHECKOUTDIR/$CI_PROJECT_NAME/release/application.properties

This will copy properties file form project's resource dir to release dir & do the values replacement to generate env specific application.properties using GitLab runner.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Saurabhcdt