如何在Travis和sbt 0.13下的master分支上发布

时间:2013-12-18 17:49:32

标签: sbt travis-ci

我正在使用Travis进行持续构建和集成。

after_success:
- sbt publish

虽然我们希望Travis构建我们的所有分支机构并提取请求,但我们只希望在publish分支时master。{/ p>

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")
})

这种方法的问题是Travis编译分支两次,因为它在最后一步发布失败。

如何在非sbt publish分支上完全阻止Travis运行master

2 个答案:

答案 0 :(得分:43)

您可以考虑在发布脚本之外处理此问题:

after_success:
  - test $TRAVIS_PULL_REQUEST == "false" && test $TRAVIS_BRANCH == "master" && sbt publish

答案 1 :(得分:6)

我为它做了一个新任务,并在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")
}

此处的关键是使用Def.taskDyn,这样我就可以在尾部位置调用publish而不依赖它。 publishTo将是明显的逻辑。