Setting up sbt to publish to artifactory based on git branch
Asked Answered
I

1

9

I would like to set up an sbt project so that it can publish to the proper artifactory repository based on the (git) branch.

The solution proposed for this question suggests to hardcode the repository in the build.sbt file.

However, I would like the master branch to publish to "releases", and another branch to publish to "snapshots", using the same build.sbt file.

Ideally, I would like the following:

val gitBranch = taskKey[String]("Determines current git branch")               
gitBranch := Process("git rev-parse --abbrev-ref HEAD").lines.head 

publishTo := {                                                                 
  val myArtifactory = "http://some.where/"        
  if (gitBranch.value == "master")                                             
    Some("releases"  at myArtifactory + "releases")                              
  else                                                                         
    Some("snapshots" at myArtifactory + "snapshots")                             
}                  

but this yields "error: A setting cannot depend on a task".

Inquiline answered 10/5, 2016 at 16:22 Comment(0)
I
6

One near-solution is to work with the sbt-release plugin, and then to use isSnapshot (which is a setting) in order to select the repository.

The solution to the original problem is to simply make gitBranch a setting:

val gitBranch = settingKey[String]("Determines current git branch")               

instead of

val gitBranch = taskKey[String]("Determines current git branch")     

Note that a setting is only computed once, at the beginning of the sbt session, so this is not suitable if there is any branch-switching within a session.

Thus, the whole code snippet will become:

val gitBranch = settingKey[String]("Determines current git branch")
gitBranch := Process("git rev-parse --abbrev-ref HEAD").lineStream.head

publishTo := {                                                                 
  val myArtifactory = "http://some.where/"        
  if (gitBranch.value == "master")                                             
    Some("releases"  at myArtifactory + "releases")                              
  else                                                                         
    Some("snapshots" at myArtifactory + "snapshots")                             
}
Inquiline answered 12/5, 2016 at 17:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.