Skip to content

Commit

Permalink
PI-1748 Add build info from environment variables (#2986)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-bcl authored Jan 10, 2024
1 parent d0db900 commit 5139e2f
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 7 deletions.
18 changes: 14 additions & 4 deletions .github/actions/cloud-platform-deploy/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,20 @@ inputs:
runs:
using: composite
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Get environment details
uses: ./.github/actions/get-env-details
id: env
with:
environment: ${{ inputs.environment }}

- name: Get build info
uses: ./.github/actions/get-build-info
with:
project: ${{ inputs.project }}
version: ${{ inputs.version }}

- name: Authenticate
uses: ./.github/actions/cloud-platform-auth
with:
Expand All @@ -63,14 +69,18 @@ runs:
--force \
--install \
--reset-values \
--set 'generic-service.env.VERSION=${{ inputs.version }}' \
--set 'generic-service.image.tag=${{ inputs.version }}' \
--set 'version=${{ inputs.version }}' \
--set 'generic-service.image.tag=${{ inputs.version }}' \
--set 'generic-service.env.VERSION=${{ inputs.version }}' \
--set "generic-service.env.BUILD_INFO=$([ -r "$BUILD_INFO" ] && cat "$BUILD_INFO" | base64 -w0 || echo)" \
--set "generic-service.env.GIT_INFO=$([ -r "$GIT_INFO" ] && cat "$GIT_INFO" | base64 -w0 || echo)" \
--timeout 10m \
--values <(curl "$(gh api '/repos/ministryofjustice/hmpps-ip-allowlists/contents/ip-allowlist-groups.yaml' | jq -r '.download_url')") \
--values templates/helm-defaults.yml \
--values 'projects/${{ inputs.project }}/deploy/values.yaml' \
--values 'projects/${{ inputs.project }}/deploy/${{ steps.env.outputs.values-file }}' \
--wait
env:
GITHUB_TOKEN: ${{ inputs.github_token }}
GITHUB_TOKEN: ${{ inputs.github_token }}
BUILD_INFO: projects/${{ inputs.project }}/build-info.properties
GIT_INFO: projects/${{ inputs.project }}/git.properties
35 changes: 35 additions & 0 deletions .github/actions/get-build-info/action.yml
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 }}
55 changes: 55 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,61 @@ updates:
schedule:
interval: "daily"

- package-ecosystem: "github-actions"
directory: "/.github/actions/analyse"
schedule:
interval: "daily"

- package-ecosystem: "github-actions"
directory: "/.github/actions/check-changes"
schedule:
interval: "daily"

- package-ecosystem: "github-actions"
directory: "/.github/actions/cloud-platform-auth"
schedule:
interval: "daily"

- package-ecosystem: "github-actions"
directory: "/.github/actions/cloud-platform-deploy"
schedule:
interval: "daily"

- package-ecosystem: "github-actions"
directory: "/.github/actions/create-signed-pull-request"
schedule:
interval: "daily"

- package-ecosystem: "github-actions"
directory: "/.github/actions/docker-build"
schedule:
interval: "daily"

- package-ecosystem: "github-actions"
directory: "/.github/actions/format-code"
schedule:
interval: "daily"

- package-ecosystem: "github-actions"
directory: "/.github/actions/get-build-info"
schedule:
interval: "daily"

- package-ecosystem: "github-actions"
directory: "/.github/actions/get-env-details"
schedule:
interval: "daily"

- package-ecosystem: "github-actions"
directory: "/.github/actions/merge-changes"
schedule:
interval: "daily"

- package-ecosystem: "github-actions"
directory: "/.github/actions/render-project-template"
schedule:
interval: "daily"

- package-ecosystem: "terraform"
directory: "/templates"
schedule:
Expand Down
17 changes: 14 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import com.gorylenko.GenerateGitPropertiesTask
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jetbrains.kotlin.noarg.gradle.NoArgExtension
import org.springframework.boot.gradle.tasks.buildinfo.BuildInfo
import org.springframework.boot.gradle.tasks.bundling.BootJar
import org.springframework.boot.gradle.tasks.run.BootRun
import uk.gov.justice.digital.hmpps.plugins.ClassPathPlugin
Expand All @@ -12,6 +14,7 @@ plugins {
kotlin("kapt") version "1.9.22" apply false
id("org.springframework.boot") version "3.2.1" apply false
id("io.spring.dependency-management") version "1.1.4" apply false
id("com.gorylenko.gradle-git-properties") version "2.4.1" apply false
id("com.google.cloud.tools.jib") apply false
id("base")
id("org.sonarqube")
Expand Down Expand Up @@ -59,15 +62,16 @@ subprojects {
apply { plugin("org.springframework.boot") }
apply { plugin("io.spring.dependency-management") }
apply { plugin("org.jetbrains.kotlin.jvm") }
apply { plugin("org.jetbrains.kotlin.plugin.spring") }
apply { plugin("org.jetbrains.kotlin.kapt") }
apply { plugin("org.jetbrains.kotlin.plugin.jpa") }
apply { plugin("org.jetbrains.kotlin.plugin.spring") }
apply { plugin("jacoco") }
apply { plugin("test-report-aggregation") }
apply { plugin("jacoco-report-aggregation") }
apply { plugin("org.sonarqube") }
apply { plugin("com.gorylenko.gradle-git-properties") }
apply { plugin(JibConfigPlugin::class.java) }
apply { plugin(ClassPathPlugin::class.java) }
apply { plugin("org.sonarqube") }
apply { plugin("org.jetbrains.kotlin.kapt") }

tasks {
withType<BootRun> {
Expand All @@ -81,6 +85,13 @@ subprojects {
isReproducibleFileOrder = true
archiveFileName.set("${archiveBaseName.get()}-${archiveClassifier.get()}.${archiveExtension.get()}")
}
named<GenerateGitPropertiesTask>("generateGitProperties") { enabled = false }
register<GenerateGitPropertiesTask>("gitInfo") {
gitProperties.gitPropertiesResourceDir = projectDir
}
register<BuildInfo>("buildInfo") {
destinationDir = projectDir
}
}

extensions.configure<NoArgExtension> {
Expand Down
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) } }
}
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())
}
}

0 comments on commit 5139e2f

Please sign in to comment.