tylervz
06/04/2024, 6:16 PMJames Fredley
06/04/2024, 6:58 PMtylervz
06/04/2024, 6:59 PMArjang
06/05/2024, 3:49 PMArjang
06/05/2024, 3:59 PMJames Fredley
06/05/2024, 4:07 PMplugins {
id "groovy"
id "org.grails.grails-web"
id "org.grails.grails-gsp"
id "com.github.erdi.webdriver-binaries"
id "war"
id "idea"
id "com.bertramlabs.asset-pipeline"
id "application"
id "eclipse"
id "com.gorylenko.gradle-git-properties" version "2.4.1"
id "org.ajoberstar.grgit" version "5.2.1"
}
package ahr.conf
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.PropertySource
import org.springframework.stereotype.Component
@Component
@Configuration
// For Local Dev Environment
@PropertySource(value = "<file:build/resources/main/git.properties>", ignoreResourceNotFound = true)
// For deployed zip/war/jar
@PropertySource(value = "classpath:git.properties", ignoreResourceNotFound = true)
class GitInfo {
// Additional keys are available in build/resources/main/git.properties
private static String GIT_COMMIT_ID_ABBREV
private static String GIT_COMMIT_ID
private static String GIT_BRANCH
private static String GIT_BUILD_HOST
private static String GIT_BUILD_USER_EMAIL
private static String GIT_COMMIT_TIME
private static String GIT_COMMIT_USER_EMAIL
private static Boolean GIT_DIRTY
static String getGitCommitIdAbbrev(){
return GIT_COMMIT_ID_ABBREV
}
@Value('${git.commit.id.abbrev:na}')
public void setGitCommitIdAbbrev(String input){
GitInfo.GIT_COMMIT_ID_ABBREV = input
}
static String getGitCommitId(){
return GIT_COMMIT_ID
}
@Value('${git.commit.id:na}')
public void setGitCommitId(String input){
GitInfo.GIT_COMMIT_ID = input
}
static String getGitBranch(){
return GIT_BRANCH
}
@Value('${git.branch:na}')
public void setGitBranch(String input){
GitInfo.GIT_BRANCH = input
}
static String getGitBuildHost(){
return GIT_BUILD_HOST
}
@Value('${git.build.host:na}')
public void setGitBuildHost(String input){
GitInfo.GIT_BUILD_HOST = input
}
static String getGitBuildUserEmail(){
return GIT_BUILD_USER_EMAIL
}
@Value('${git.build.user.email:na}')
public void setGitBuildUserEmail(String input){
GitInfo.GIT_BUILD_USER_EMAIL = input
}
static String getGitCommitTime(){
return GIT_COMMIT_TIME
}
@Value('${git.commit.time:na}')
public void setGitCommitTime(String input){
GitInfo.GIT_COMMIT_TIME = input
}
static String getGitCommitUserEmail(){
return GIT_COMMIT_USER_EMAIL
}
@Value('${git.commit.user.email:na}')
public void setGitCommitUserEmail(String input){
GitInfo.GIT_COMMIT_USER_EMAIL = input
}
static String getGitDirty(){
return GIT_DIRTY
}
@Value('${git.dirty:na}')
public void setGitDirty(Boolean input){
GitInfo.GIT_DIRTY = input
}
}
<!-- ${ahr.conf.GitInfo.gitCommitIdAbbrev} -->
James Fredley
06/05/2024, 4:14 PMspring:
info:
git:
location: '<file:build/resources/main/git.properties>'
James Fredley
06/05/2024, 4:19 PMdef getCheckedOutGitCommitHash() {
grgit.head()?.abbreviatedId
}
task awsEB(type: Zip, dependsOn: assemble) {
from bootWar.outputs.files
rename "(.*).war", "admin.jar"
from "aws"
archiveFileName = "admin-env-${env}-${getCheckedOutGitCommitHash()}.zip"
}
tylervz
06/06/2024, 3:10 PMbuild.gradle
code shared in that blog post to get it working with Grails 6 is to wrap the buildProperties.doLast
closure inside project.gradle.projectsEvaluated { }
tylervz
06/06/2024, 3:11 PMproject.gradle.projectsEvaluated {
buildProperties.doLast {
// Find the right file
File grailsBuildInfoFile = it.outputs.files.files.find { File file ->
file.name == '<http://grails.build.info|grails.build.info>'
} as File
if (!grailsBuildInfoFile) {
return // No need to continue if the file is not there
}
Properties properties = new Properties()
// Read properties from the file
grailsBuildInfoFile.withInputStream { InputStream inputStream ->
properties.load(inputStream)
}
// Get the current date and time in UTC
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC"))
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
// Add new properties from various sources
properties.setProperty('build.time', now.format(formatter))
// Get a System property
properties.setProperty('build.java.version', System.getProperty('java.version'))
// Get the host name where the build was created
properties.setProperty('build.host', InetAddress.localHost.hostName)
// Add property set by your CI (in this case CircleCI)
Map<String, String> env = System.getenv()
if (env.CIRCLE_BUILD_NUM) {
properties.setProperty('build.number', env.CIRCLE_BUILD_NUM)
properties.setProperty('build.git.revision', env.CIRCLE_SHA1)
properties.setProperty('build.git.branch', env.CIRCLE_BRANCH)
} else {
// Get the commit id (aka hash) of HEAD
String gitCommitId = grgit.head().id
properties.setProperty('build.git.revision', gitCommitId)
properties.setProperty('build.git.branch', grgit.branch.current().getName())
}
// Write the properties back to the file
grailsBuildInfoFile.withOutputStream { OutputStream outputStream ->
properties.store(outputStream, null)
}
}
}
tylervz
06/06/2024, 3:11 PMplugins {
id "groovy"
id "org.grails.grails-web"
id "org.grails.grails-gsp"
id "com.github.erdi.webdriver-binaries"
id "war"
id "idea"
id "com.bertramlabs.asset-pipeline"
id "application"
id "eclipse"
id "org.ajoberstar.grgit" version "5.2.2"
}