拆分sbt分阶段发布?

时间:2018-03-04 20:32:02

标签: scala sbt sbt-release

我有一个SBT项目和一个CD管道,我想要的是执行以下一系列步骤:

  1. 从git repo
  2. 查看我的项目
  3. 标记提交
  4. 运行测试
  5. 打包我的应用
  6. 现在,此时我还不想发布任何内容,因为我会将二进制文件提升到另一个环境以运行端到端测试。只有当它们成功完成时,我才想推送git标签并将我的artefact上传到远程artefactory存储库。我真正想要实现的是,能够首先运行sbt prepereRelease之后我将升级到我的TEST环境,之后,如果一切正常,运行sbt doRelease。所以我想在build.sbt中使用类似的东西:

    releaseProcess := Seq[ReleaseStep](
          checkSnapshotDependencies,
          inquireVersions,
          runClean,
          runTest,
          setReleaseVersion,
          commitReleaseVersion,
          tagRelease,
          setNextVersion,
          commitNextVersion
    )
    
    commands += Command.command("prepareRelease")((state:State) => {
      val newState = Command.process("release",state)
      println("Release called from prepareRelease...")
      newState
    })
    
    releaseProcess := Seq[ReleaseStep](
          publishArtifacts,
          setNextVersion,
          commitNextVersion,
          pushChanges
    )
    
    commands += Command.command("doRelease")((state:State) => {
      val newState = Command.process("release",state)
      println("Release called from doRelease...")
      newState
    })
    

    我几乎觉得我必须定义两个自定义命令,每个命令都必须使用sbt-release设置从releaseProcess插件调用原始发布命令 - 这就是我要做的一点我知道该如何去做。遗憾的是,上述设置无效,因为releaseProcess设置累积了这些步骤,您仍然可以立即执行所有步骤。

2 个答案:

答案 0 :(得分:0)

您已将prepareReleasedoRelease定义为设置。这意味着设置的值仅在加载或重新加载构建时计算一次。此外,ReleaseStep类型仅描述要作为发布过程的一部分执行的函数,并且不会自行执行任何操作。

看起来您正在使用sbt-release插件。在文档之后,您必须使用自定义步骤重新定义releaseProcess密钥,并运行release命令来执行它们。

答案 1 :(得分:0)

我最终实现它的方法是创建两个自定义命令并将releaseProcess设置附加到state对象,然后将其传递到release插件:

// Defines the release process
releaseIgnoreUntrackedFiles := true

commands += Command.command("prepareRelease")((state: State) => {
  println("Preparing release...")
  val extracted = Project extract state
  val customState = extracted.append(Seq(releaseProcess := Seq[ReleaseStep](
    checkSnapshotDependencies,
    inquireVersions,
    runClean,
    setReleaseVersion,
    commitReleaseVersion,
    tagRelease,
    runTest
  )), state)
  val newState = Command.process("release with-defaults", customState)
  Command.process("dist", newState)
})

commands += Command.command("completeRelease")((state: State) => {
  println("Completing release...")
  val extracted = Project extract state
  val customState = extracted.append(Seq(releaseProcess := Seq[ReleaseStep](
    inquireVersions,
    setNextVersion,
    commitNextVersion,
    pushChanges
  )), state)
  val newState = Command.process("release with-defaults", customState)
  newState
})

所以我的管道做了类似于以下的事情:

  1. sbt prepareRelease

  2. 此时我将二进制文件部署到我的TEST环境

  3. 如果一切正常,那么我会sbt completeRelease

  4. 最终curl我的拉链成为nexus