Hi everyone, is there a template or we can use for...
# questions
t
Hi everyone, is there a template or we can use for creating our own Grails Guide with the hopes that it will be published on https://guides.grails.org/index.html and the code will reside in a new repository under https://github.com/grails-guides ? I want to adapt this blog post for Grails 6 projects. https://grails.org/blog/2017-04-02.html
πŸ‘ 1
j
The readme on https://github.com/grails/grails-guides has some details.
πŸ™Œ 1
t
Perfect. Thanks!
a
@tylervz Thanks for this. I wrote my own build information with Gradle that includes the Git branch name and commit number. That’s how I can, for certain, know which code is on the server. (Usual problem when running multiple projects)
πŸ‘ 1
But i like your solution better. When i have time, i create a example and post it here.
j
I currently use the gradle plugin com.gorylenko.gradle-git-properties to write the file. spring-boot-actuator reads the values in and I also have groovy class to make static access a little more concise in GSPs.
Copy code
plugins {
    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"
}
Copy code
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
    }
}
Copy code
<!-- ${ahr.conf.GitInfo.gitCommitIdAbbrev}  -->
πŸ™ 1
application yml for spring boot actuator
Copy code
spring:  
  info:
    git:
      location: '<file:build/resources/main/git.properties>'
And I use the grgit gradle plugin to add the commit hash abbreviation to the filename.
Copy code
def 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"
}
πŸ‘ 1
t
In the meantime while I finish writing up that Grails guide, the main change I made to the
build.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 { }
Copy code
project.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)
        }
    }
}
πŸ‘ 1
And I'm also using a Gradle plugin to retrieve git commit info.
Copy code
plugins {
    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"
}
πŸ‘ 1