Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to publish only when on master branch under Travis and sbt 0.13

Tags:

travis-ci

sbt

I'm using Travis for continuous build and integration.

after_success:
- sbt publish

While we want Travis to build all of our branches and pull requests, we only want it to publish when on master branch.

publishTo <<= version { (v: String) =>
  val nexus = s"asdf"
  /* Don't let Travis publish when building pull requests.
   * $TRAVIS_PULL_REQUEST == "false" if it's not a pull request.  So we wan't publishTo to be
   * None when TRAVIS_PULL_REQUEST != false.
   */
  if(Try(sys.env("TRAVIS_PULL_REQUEST")).getOrElse("false") != "false")
    None
  /* Don't let Travis publish except when building master. */
  if(Try(sys.env("TRAVIS_BRANCH")).map(_ != "master").getOrElse(false))
    None
  else if(v.trim.endsWith("SNAPSHOT"))
    Some("snapshots" at nexus + "snapshots")
  // don't let Travis publish releases, either
  else if(Try(sys.env("TRAVIS")).getOrElse("false") == "true")
    None
  else
    Some("releases"  at nexus + "releases")
})

The problem with this approach is that Travis compiles branches twice because it fails publishing at the very last step.

How can Travis be completely prevented from running sbt publish when on non-master branch?

like image 405
schmmd Avatar asked Dec 18 '13 17:12

schmmd


2 Answers

You might consider handling this outside of your publish script:

after_success:
  - test $TRAVIS_PULL_REQUEST == "false" && test $TRAVIS_BRANCH == "master" && sbt publish
like image 195
Ben Marini Avatar answered Oct 20 '22 00:10

Ben Marini


I'd make a new task for it, and call that in Travis.

val publishMasterOnTravis = taskKey[Unit]("publish master on travis")

def publishMasterOnTravisImpl = Def.taskDyn {
  import scala.util.Try
  val travis   = Try(sys.env("TRAVIS")).getOrElse("false") == "true"
  val pr       = Try(sys.env("TRAVIS_PULL_REQUEST")).getOrElse("false") == "true"
  val branch   = Try(sys.env("TRAVIS_BRANCH")).getOrElse("??")
  val snapshot = version.value.trim.endsWith("SNAPSHOT")
  (travis, pr, branch, snapshot) match {
    case (true, false, "master", true) => publish
    case _                             => Def.task ()
  }
}

publishMasterOnTravis := publishMasterOnTravisImpl.value

publishTo := {
  val nexus = s"asdf"
  if (version.value.trim.endsWith("SNAPSHOT")) 
    Some("snapshots" at nexus + "snapshots") 
  else
    Some("releases" at nexus + "releases")
}

The key here is using Def.taskDyn, which lets me call publish at the tail position without depending on it. publishTo would be plain logic.

like image 29
Eugene Yokota Avatar answered Oct 19 '22 23:10

Eugene Yokota