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)
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
}
)
)
Adding my ugly solution to add git head hash to build:
(addition to the links from Bob Kuhar's answer)
I've already had "lazy val root" so this is what it looks like right now.
lazy val root = (project in file(".")).
enablePlugins(PlayScala).
enablePlugins(BuildInfoPlugin).
settings(
buildInfoKeys := Seq[BuildInfoKey](
name, version, scalaVersion, sbtVersion,
"hostname" -> java.net.InetAddress.getLocalHost().getHostName(),
"whoami" -> System.getProperty("user.name"),
"buildTimestamp" -> new java.util.Date(System.currentTimeMillis()),
"gitHash" -> new java.lang.Object(){
override def toString(): String = {
try {
val extracted = new java.io.InputStreamReader(
java.lang.Runtime.getRuntime().exec("git rev-parse HEAD").getInputStream())
(new java.io.BufferedReader(extracted)).readLine()
} catch { case t: Throwable => "get git hash failed" }
}}.toString()
),
buildInfoPackage := "buildpkg"
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With