generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
d0db900
commit 5139e2f
Showing
6 changed files
with
192 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
name: Get build info | ||
description: Generate build info files for Gradle projects | ||
|
||
inputs: | ||
project: | ||
description: The name of the project | ||
required: true | ||
version: | ||
description: The version of the service to deploy | ||
required: true | ||
|
||
runs: | ||
using: "composite" | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Check if Gradle project | ||
id: gradle_file | ||
uses: andstor/file-existence-action@20b4d2e596410855db8f9ca21e96fbe18e12930b # v2 | ||
with: | ||
files: projects/${{ inputs.project }}/build.gradle.kts | ||
|
||
- uses: actions/setup-java@v3 | ||
if: ${{ steps.gradle_file.outputs.files_exists == 'true' }} | ||
with: | ||
java-version: '21' | ||
distribution: 'temurin' | ||
|
||
- name: Get build info | ||
uses: gradle/gradle-build-action@v2 | ||
if: ${{ steps.gradle_file.outputs.files_exists == 'true' }} | ||
with: | ||
arguments: '${{ inputs.project }}:buildInfo ${{ inputs.project }}:gitInfo' | ||
env: | ||
ORG_GRADLE_PROJECT_version: ${{ inputs.version }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
libs/commons/src/main/kotlin/uk/gov/justice/digital/hmpps/config/BuildInfoConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package uk.gov.justice.digital.hmpps.config | ||
|
||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.boot.info.BuildProperties | ||
import org.springframework.boot.info.GitProperties | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import java.util.* | ||
|
||
/** | ||
* Read build and git info from environment variables into the /info endpoint. | ||
* | ||
* This enables us to generate new build info without invalidating caches. | ||
*/ | ||
@Configuration | ||
class BuildInfoConfig { | ||
@Bean | ||
fun buildProperties(@Value("\${build.info:#{null}}") info: String?) = | ||
info?.let { BuildProperties(loadFrom(it, "build.")) } | ||
|
||
@Bean | ||
fun gitProperties(@Value("\${git.info:#{null}}") info: String?) = | ||
info?.let { GitProperties(loadFrom(it, "git.")) } | ||
|
||
private fun loadFrom(base64Properties: String, prefix: String) = Properties() | ||
.apply { load(String(Base64.getDecoder().decode(base64Properties)).reader()) } | ||
.mapKeys { it.key.toString().removePrefix(prefix) } | ||
.let { Properties().apply { putAll(it) } } | ||
} |
45 changes: 45 additions & 0 deletions
45
libs/commons/src/test/kotlin/uk/gov/justice/digital/hmpps/config/BuildInfoConfigTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package uk.gov.justice.digital.hmpps.config | ||
|
||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.hamcrest.Matchers.equalTo | ||
import org.hamcrest.Matchers.nullValue | ||
import org.junit.jupiter.api.Test | ||
import java.util.* | ||
|
||
class BuildInfoConfigTest { | ||
@Test | ||
fun `converts base64-encoded properties into BuildProperties`() { | ||
val properties = """ | ||
build.artifact=value1 | ||
build.group = value2 | ||
build.name=value3\nvalue3 | ||
#build.version=value4 | ||
non-build.other=value5 | ||
""".trimIndent() | ||
val encodedProperties = Base64.getEncoder().encodeToString(properties.toByteArray()) | ||
val buildProperties = BuildInfoConfig().buildProperties(encodedProperties) | ||
|
||
checkNotNull(buildProperties) | ||
assertThat(buildProperties.artifact, equalTo("value1")) | ||
assertThat(buildProperties.group, equalTo("value2")) | ||
assertThat(buildProperties.name, equalTo("value3\nvalue3")) | ||
assertThat(buildProperties.version, nullValue()) | ||
} | ||
|
||
@Test | ||
fun `converts base64-encoded properties into GitProperties`() { | ||
val properties = "git.branch=value1" | ||
val encodedProperties = Base64.getEncoder().encodeToString(properties.toByteArray()) | ||
val gitProperties = BuildInfoConfig().gitProperties(encodedProperties) | ||
|
||
checkNotNull(gitProperties) | ||
assertThat(gitProperties.branch, equalTo("value1")) | ||
} | ||
|
||
@Test | ||
fun `handles null`() { | ||
val gitProperties = BuildInfoConfig().gitProperties(null) | ||
assertThat(gitProperties, nullValue()) | ||
} | ||
} |