Skip to content

Commit

Permalink
Use SOURCE_VERSION env variable to get Heroku build commit
Browse files Browse the repository at this point in the history
Since 1st April 2015, there's a `SOURCE_VERSION` environment variable
available to builds running on Heroku. For git-push builds, this is the
git commit SHA-1 of the source being built:

https://devcenter.heroku.com/changelog-items/630
http://stackoverflow.com/a/28718917/438886
  • Loading branch information
rtyley committed Oct 26, 2015
1 parent 6201453 commit 95d66d1
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 23 deletions.
21 changes: 0 additions & 21 deletions app/controllers/Application.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,4 @@ object Application extends Controller {
Ok(pullRequestSent(req.pr, req.user, mailType)).addingToSession(prId.slug -> toJson(updatedSettings).toString)
}
}

lazy val gitCommitId = gitCommitIdFromHerokuFile.getOrElse(app.BuildInfo.gitCommitId)

def gitCommitIdFromHerokuFile: Option[String] = {
val existingFileOpt: Option[File] = herokuMetadataFile()

Logger.debug(s"Heroku dyno metadata: $existingFileOpt")

for {
existingFile <- existingFileOpt
commitId <- (Json.parse(new FileInputStream(existingFile)) \ "release" \ "commit").asOpt[String]
} yield {
Logger.debug(s"Heroku dyno commit id: $commitId")
commitId
}
}

def herokuMetadataFile(): Option[File] = {
val file = new File("/etc/heroku/dyno")
if (file.exists && file.isFile) Some(file) else None
}
}
2 changes: 1 addition & 1 deletion app/views/fragments/footer.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
<p></p>
</div>
</footer>
<!-- @Application.gitCommitId -->
<!-- @app.BuildInfo.gitCommitId -->
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ lazy val root = (project in file(".")).enablePlugins(
).settings(
buildInfoKeys := Seq[BuildInfoKey](
name,
BuildInfoKey.constant("gitCommitId", Option(System.getenv("BUILD_VCS_NUMBER")) getOrElse(try {
BuildInfoKey.constant("gitCommitId", Option(System.getenv("SOURCE_VERSION")) getOrElse(try {
"git rev-parse HEAD".!!.trim
} catch { case e: Exception => "unknown" }))
),
Expand Down

0 comments on commit 95d66d1

Please sign in to comment.