playframeworksbtplayframework-2.1playframework-2.2

How to add commit hash to Play templates?


I want to put the commit hash into a Play Framework template file so that I can view the build information via REST GET call.

In sbt I can get a git commit hash and the git branch name, is there anyway to put this information into a template file during the build process?

build.sbt

name := "my-project"

val branch = "git rev-parse --abbrev-ref HEAD".!!.trim
val commit = "git rev-parse HEAD".!!.trim
val buildTime = (new java.text.SimpleDateFormat("yyyyMMdd-HHmmss")).format(new java.util.Date())

version := "%s-%s-%s".format(branch, commit, buildTime)

Solution

  • I used an sbt plugin called sbt-buildinfo to do this. See the answer to Does sbt have something like gradle's processResources task with ReplaceTokens support? . Technically, it worked. Effectively it kind-of sucked in that Play would reload the whole project every time anything changed. Perhaps they've overcome this by now? Give sbt-buildinfo a try: https://github.com/sbt/sbt-buildinfo#usage

    Example usage:

    lazy val root = (project in file("."))
      .enablePlugins(BuildInfoPlugin)
      .settings(
        buildInfoKeys := Seq[BuildInfoKey](
          <whateverYouWant>,
          BuildInfoKey.action("commit") {
            scala.sys.process.Process("git rev-parse HEAD").!!.trim
          }
        )
      )