Home > Back-end >  Exporting the variables in Gitlab to properties file
Exporting the variables in Gitlab to properties file

Time:01-24

I am using a Spring boot based application and a pipeline on gitlab. I need to export the tag and version to my variables in the properties file in the repo. In before-script i need to take the value of tag from gitlab-ci.yml and put it in my config.properties file in the project.

  • export APP_VERSION=${CI_COMMIT_TAG:-$(git describe --tags)}

need to pass the values in app version to

version.major=1

version.minor=0

version.patch=0

any other approaches are welcome to do this. End Goal to pass the version to application configuration before the script runs.

CodePudding user response:

Get major, minor and patch by using sed and git describe

Let's start with getting major,minor and patch from the latest tag. You can use sed to parse the tag. I won't go into detail about the syntax, but this should help you get everything you need.

//fetch latest tag
> VERSION=$(git describe --tags $(git rev-list --tags --max-count=1))
> echo $VERSION
v0.2.2

// remove prefix and suffix from tag
> VERSION=$(echo "$VERSION" | sed "s/^.*[^0-9]\([0-9]*\.[0-9]*\.[0-9]*\).*$/\1/")
> echo $VERSION
0.2.2

// get major, minor, patch 
> MAJOR_VERSION=$(echo "$VERSION" | sed "s/^\([0-9]*\).*/\1/")
> echo $MAJOR_VERSION
0

> MINOR_VERSION=$(echo "$VERSION" | sed "s/[0-9]*\.\([0-9]*\).*/\1/")
> echo $MINOR_VERSION
2

> PATCH_VERSION=$(echo "$VERSION" | sed "s/[0-9]*\.[0-9]*\.\([0-9]*\).*/\1/")
> echo $PATCH_VERSION
2

mavens automatic property expansion

As you are using spring boot, you might consider mavens automatic property expansion which allows you to automatically override properties (https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.properties-and-configuration) .

Add this to the build section of your pom.xml

<build>
    ...
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.7</version>
            <configuration>
                <delimiters>
                    <delimiter>@</delimiter>
                </delimiters>
                <useDefaultDelimiters>false</useDefaultDelimiters>
            </configuration>
        </plugin>
        ...
    </plugins>
</build>

This tells the maven-resources plugin to automatically update values in files in your src/main/resources directory that are delimited by @. So now your change your property file that it looks like this:

[email protected]@
[email protected]@
[email protected]@

Finally add your desired properties to your pom.xml:

    <properties>
       ...
         <version.major>1</version.major>
         <version.minor>0</version.minor>
         <version.patch>0</version.patch>
    </properties>

To dynamically update the version you now only need to pass the variables to maven / JVM when building your application.

build:
  stage: build
  script:
    - mvn clean package -Dversion.major=$MAJOR_VERSION -Dversion.minor=$MINOR_VERSION -Dversion.patch=$PATCH_VERSION
  artifacts:
    paths:
    - pom.xml
    - target/*.jar
  except:
    - tags

optional: use sed to write to properties file

If you are not using maven, you could also directly write your app version to the config file via sed

build:
  stage: build
  before_script:
    - sed -i "s/^\(major\.version\s*=\s*\).*\$/\1$MAJOR_VERSION/" src/main/resources/config.properties
    - sed -i "s/^\(minor\.version\s*=\s*\).*\$/\1$MINOR_VERSION/" src/main/resources/config.properties
    - sed -i "s/^\(patch\.version\s*=\s*\).*\$/\1$PATCH_VERSION/" src/main/resources/config.properties
  script:
    - ...
  artifacts:
    paths:
    - pom.xml
    - target/*.jar
  except:
    - tags
  •  Tags:  
  • Related