Spring to pick up git commit via Maven plugin or CI/CD variables (gitlab)

<plugin>
<groupId>io.github.git-commit-id</groupId>
<artifactId>git-commit-id-maven-plugin</artifactId>
<version>5.0.0</version>
<executions>
<execution>
<id>get-the-git-infos</id>
<goals>
<goal>revision</goal>
</goals>
<phase>initialize</phase>
</execution>
</executions>
<configuration>
<dotGitDirectory>${project.basedir}/../.git</dotGitDirectory>
<verbose>false</verbose>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties
</generateGitPropertiesFilename>
<commitIdGenerationMode>full</commitIdGenerationMode>
<includeOnlyProperties>
<includeOnlyProperty>git.commit.id.abbrev</includeOnlyProperty>
<includeOnlyProperty>git.commit.id.full</includeOnlyProperty>
<includeOnlyProperty>git.commit.message.short</includeOnlyProperty>
<includeOnlyProperty>git.commit.time</includeOnlyProperty>
</includeOnlyProperties>
</configuration>
</plugin>
package com.boardintelligence.writerapi.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Data
@Configuration
@PropertySource("classpath:git.properties")
public class GitCommitProperties {
@Value("${git.commit.id.abbrev:na}")
private String abbrev;

@Value("${git.commit.id.full:na}")
private String full;

@Value("${git.commit.message.short:na}")
private String messageShort;

@Value("${git.commit.time:na}")
private String time;
}

If the .git directory is not accessible from the CI/CD pipeline

#.gitlab-ci.ymlyour-task:
before_script:
- echo -n "git.commit.id.abbrev=${CI_COMMIT_SHORT_SHA}\ngit.commit.id.full=${CI_COMMIT_SHA}\ngit.commit.message.short=${CI_COMMIT_MESSAGE}\ngit.commit.time=${CI_COMMIT_TIMESTAMP}" > src/main/resources/git.properties

--

--

--

Software Engineer @ London [https://www.linkedin.com/in/elvisciotti]

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Password Strength: Generating a Secure and Memorable Password

The difference between ASAP and “soon”

How to build a great open source community

NeoVim setup for golang programming

Checkpointing Through Reference Counting in RAPIDS cuStreamz

Python zip_longest() Function.

How to avoid downtime in your business

Migrating Superset to Postgres

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Elvis Ciotti

Elvis Ciotti

Software Engineer @ London [https://www.linkedin.com/in/elvisciotti]

More from Medium

Spring OpenAPI Integration

Kubernetes Tutorial | Setup Kubernetes in Windows & Run Spring boot application on k8s cluster

How to test and push to the registry a Spring Boot app on Gitlab CI

Deep dive into Pattern Matching for Java