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?
You might consider handling this outside of your publish script:
after_success:
- test $TRAVIS_PULL_REQUEST == "false" && test $TRAVIS_BRANCH == "master" && sbt publish
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.
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