Skip to content

Commit

Permalink
Show commit hash in actuator endpoint
Browse files Browse the repository at this point in the history
Include the current commit hash in the "Info" actuator endpoint
  • Loading branch information
dnl50 committed Apr 4, 2022
1 parent 2f1830f commit 827696e
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 10 deletions.
9 changes: 9 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import org.springframework.boot.gradle.tasks.run.BootRun
import java.io.ByteArrayOutputStream

plugins {
`java-convention`
Expand Down Expand Up @@ -30,13 +31,21 @@ tasks.getByName<BootRun>("bootRun") {
args = listOf("--spring.profiles.active=dev")
}

val shortCommitHash by tasks.registering(Exec::class) {
commandLine("git", "rev-parse", "--short", "HEAD")
standardOutput = ByteArrayOutputStream()
}

springBoot {
buildInfo {
properties {
additional = mapOf("commit" to DelegatingProvider(shortCommitHash.map { it.standardOutput.toString().trim() }))
time = null
artifact = ""
group = ""
name = ""
}

dependsOn(shortCommitHash)
}
}
93 changes: 93 additions & 0 deletions buildSrc/src/main/java/DelegatingProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import java.io.Serializable;
import java.util.function.BiFunction;

import org.gradle.api.Transformer;
import org.gradle.api.provider.Provider;
import org.jetbrains.annotations.Nullable;

/**
* Spring Boot's {@code BuildInfo} Task calls the {@link Object#toString() toString} Method on every value present in
* the Map of the additional build information properties at task execution time. Therefore, a normal Gradle
* {@link Provider} does not work, since the {@link Object#toString() toString} method does not return the String
* representation of the provided value.
*
* @implNote Instances of this class must be serializable. Gradle will reject unserializable input properties when
* calculating the input's fingerprint.
*/
public class DelegatingProvider<T extends Serializable> implements Provider<T>, Serializable {

/**
* @implNote Marking this property as transient is not an ideal solution, but the only type-safe one, since
* {@link Provider} does not implement {@link Serializable}.
*/
private final transient Provider<T> delegate;

/**
* @param delegate
* The provider all operations are delegated to, not {@code null}.
*/
public DelegatingProvider(Provider<T> delegate) {
this.delegate = delegate;
}

/**
* @return The string representation of the contained value.
*/
@Override
public String toString() {
return delegate.get().toString();
}

@Override
public T get() {
return delegate.get();
}

@Nullable
@Override
public T getOrNull() {
return delegate.getOrNull();
}

@Override
public T getOrElse(T defaultValue) {
return delegate.getOrElse(defaultValue);
}

@Override
public <S> Provider<S> map(Transformer<? extends S, ? super T> transformer) {
return delegate.map(transformer);
}

@Override
public <S> Provider<S> flatMap(Transformer<? extends Provider<? extends S>, ? super T> transformer) {
return delegate.flatMap(transformer);
}

@Override
public boolean isPresent() {
return delegate.isPresent();
}

@Override
public Provider<T> orElse(T value) {
return delegate.orElse(value);
}

@Override
public Provider<T> orElse(Provider<? extends T> provider) {
return delegate.orElse(provider);
}

@Override
@Deprecated
public Provider<T> forUseAtConfigurationTime() {
return delegate.forUseAtConfigurationTime();
}

@Override
public <B, R> Provider<R> zip(Provider<B> provider, BiFunction<T, B, R> biFunction) {
return delegate.zip(provider, biFunction);
}

}
17 changes: 7 additions & 10 deletions buildSrc/src/main/kotlin/java-convention.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ repositories {

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

val integrationTestImplementation by configurations.creating
Expand Down Expand Up @@ -40,19 +39,17 @@ dependencies {
testImplementation("org.mockito:mockito-junit-jupiter")
}

sourceSets {
create("integrationTest") {
compileClasspath += sourceSets.main.get().output
runtimeClasspath += sourceSets.main.get().output
}
val integrationTestSourceSet by sourceSets.creating {
compileClasspath += sourceSets.main.get().output
runtimeClasspath += sourceSets.main.get().output
}

val integrationTest = task<Test>("integrationTest") {
val integrationTest by tasks.registering(Test::class) {
group = "verification"

testClassesDirs = sourceSets["integrationTest"].output.classesDirs
classpath = sourceSets["integrationTest"].runtimeClasspath
shouldRunAfter("test")
testClassesDirs = integrationTestSourceSet.output.classesDirs
classpath = integrationTestSourceSet.runtimeClasspath
shouldRunAfter(tasks.test)
}

tasks.withType<Test> {
Expand Down

0 comments on commit 827696e

Please sign in to comment.