Home > Net >  gitlab CI/CD with maven doesn't setup environment variables in application.properties
gitlab CI/CD with maven doesn't setup environment variables in application.properties

Time:10-13

I am trying to build CI/CD pipeline with maven. Problem that I meet is that in application.properties I set variables like that:

database.mealTimeMongoPass=${MONGO_PASS}
database.mealTimeSecret=${SECRET}
database.connectionString=${ATLAS_STRING}
spring.data.mongodb.uri=${ATLAS_STRING}

and I cannot setup them it gitlab. Every time if gitlab will build package all time I cannot run it because connection string is wrong I get error: "The connection string is invalid. Connection strings must start with either 'mongodb://' or 'mongodb srv://"

here example of variable that I set up in gitlab CI/CD settings

Variable example

and here code that I tried run in gitlab CI/CD echo works correct and show correct variable value each mvn script I tried didn't work

 script:
    - echo $SECRET
    - echo $MONGO_PASS
    - echo $ATLAS_STRING
    - mvn install -B #  (I hope that application properties automatically get variables from gitlab env) 
    - mvn -DSECRET=$SECRET -DMONGO_PASS=$MONGO_PASS -DATLAS_STRING=$ATLAS_STRING clean install -B #  (I found this solution on stack) 
    - mvn -Dspring-boot.run.arguments=--database.mealTimeSecret=$SECRET,--database.mealTimeMongoPass=$MONGO_PASS,--spring.data.mongodb.uri=$ATLAS_STRING clean install -B #  (if I change here env variables for normal string it wont't build on gitlab) 

I don't have idea what I should do with that I don't want have variables saved in my repo and don't have idea what toDo with that. Could someone give me advice ? mvn script builds jar file in artifacts after each run I download it and run to test it with command

java -jar filename.jar

Update: I made small investigation and make class to test variables after spring startup:

  @PostConstruct
    public void test() {
        log.info("VARIABLES TEST");
        log.info("properties.getMealTimeSecret(): {}", properties.getMealTimeSecret());
        log.info("properties.getConnectionString(): {}", properties.getConnectionString());
        log.info("properties.getMealTimeMongoPass(): {}", properties.getMealTimeMongoPass());
    }

and variables are all time not set:

properties.getMealTimeSecret(): ${SECRET}
properties.getConnectionString(): ${ATLAS_STRING}
properties.getMealTimeMongoPass(): ${MONGO_PASS}

gitlab-ci.yml:

image: maven:3.8.1-jdk-11

build_artifact:
  stage: build
  script:
    - export
#    - mvn install -B -P no-tests
    - mvn -DSECRET=$SECRET -DMONGO_PASS=$MONGO_PASS -DATLAS_STRING=$ATLAS_STRING clean install -B -P no-tests #  (I found this solution on stack)
#    - mvn -Dspring-boot.run.arguments=--database.mealTimeSecret=$SECRET,--database.mealTimeMongoPass=$MONGO_PASS,--spring.data.mongodb.uri=$ATLAS_STRING clean install -B -P no-tests #  (if I change here env variables for normal string it wont't build on gitlab)
  artifacts:
    paths:
      - target/*.jar
    expire_in: 10 minutes

CodePudding user response:

Educated guess: you haven't enabled maven filtering for your application.properties property file.

Without filtering, those placeholders won't be replaced.

So have something like this in your pom file:

<project>
  ...
  <build>
    ...
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
      ...
    </resources>
    ...
  </build>
  ...
</project>

CodePudding user response:

Try the following steps and make sure that each one is working properly:

  1. Check whether your branch is protected or not. If it is not protected, you won't be able to access to defined environment variables.

  2. Check whether the environment variables set properly or not using export command:

    script:
        - export
        # - <other commands>
    
  3. Set filtering to true in your POM file.

  • Related