11

我正在努力将 SBT 用于具有以下基本工作流程的 CI 流程:

  1. 编译测试
  2. 缓存~/.sbt~/.ivy2/cache
  3. 缓存target我项目中的所有目录

在随后的步骤中:

  1. 恢复~/.sbt~/.ivy2/cache
  2. 恢复完整项目,包括先前生成target的包含.class文件和相同源代码的目录(应该是相同的结帐)
  3. 通过运行测试sbt test

100% 的时间,sbt test重新编译整个项目。我想了解或调试为什么会这样,因为自上次编译以来没有任何改变(好吧,什么都不应该改变,那么是什么导致它相信某些东西?)

我目前正在将 circleci 与 docker 执行器一起使用。这意味着有一个新的 docker 实例,来自同一个图像,运行每个步骤,尽管我希望缓存能够解决这个问题。

相关部分.circleci/config.yml(如果你不使用圆圈,这应该仍然可以理解;我已经注释了我能做的):

---
version: 2

jobs:
  # compile and cache compilation
  test-compile:
    working_directory: /home/circleci/myteam/myproj
    docker:
      - image: myorg/myimage:sbt-1.2.8
    steps:
      # the directory to be persisted (cached/restored) to the next step
      - attach_workspace:
          at: /home/circleci/myteam
      # git pull to /home/circleci/myteam/myproj
      - checkout
      - restore_cache:
          # look for a pre-existing set of ~/.ivy2/cache, ~/.sbt dirs 
          # from a prior build
          keys:
            - sbt-artifacts-{{ checksum "project/build.properties"}}-{{ checksum "build.sbt" }}-{{ checksum "project/Dependencies.scala" }}-{{ checksum "project/plugins.sbt" }}-{{ .Branch }}
      - restore_cache:
          # look for pre-existing set of 'target' dirs from a prior build
          keys:
            - build-{{ checksum "project/build.properties"}}-{{ checksum "build.sbt" }}-{{ checksum "project/Dependencies.scala" }}-{{ checksum "project/plugins.sbt" }}-{{ .Branch }}
      - run:
          # the compile step
          working_directory: /home/circleci/myteam/myproj
          command: sbt test:compile
      # per: https://www.scala-sbt.org/1.0/docs/Travis-CI-with-sbt.html
      # Cleanup the cached directories to avoid unnecessary cache updates
      - run:
          working_directory: /home/circleci
          command: |
            rm -rf /home/circleci/.ivy2/.sbt.ivy.lock
            find /home/circleci/.ivy2/cache -name "ivydata-*.properties" -print -delete
            find /home/circleci/.sbt -name "*.lock" -print -delete
      - save_cache:
          # cache ~/.ivy2/cache and ~/.sbt for subsequent builds
          key: sbt-artifacts-{{ checksum "project/build.properties"}}-{{ checksum "build.sbt" }}-{{ checksum "project/Dependencies.scala" }}-{{ checksum "project/plugins.sbt" }}-{{ .Branch }}-{{ .Revision }}
          paths:
            - /home/circleci/.ivy2/cache
            - /home/circleci/.sbt
      - save_cache:
          # cache the `target` dirs for subsequenet builds
          key: build-{{ checksum "project/build.properties"}}-{{ checksum "build.sbt" }}-{{ checksum "project/Dependencies.scala" }}-{{ checksum "project/plugins.sbt" }}-{{ .Branch }}-{{ .Revision }}
          paths:
            - /home/circleci/myteam/myproj/target
            - /home/circleci/myteam/myproj/project/target
            - /home/circleci/myteam/myproj/project/project/target
      # in circle, a 'workflow' undergoes several jobs, this first one 
      # is 'compile', the next will run the tests (see next 'job' section
      # 'test-run' below). 
      # 'persist to workspace' takes any files from this job and ensures 
      # they 'come with' the workspace to the next job in the workflow
      - persist_to_workspace:
          root: /home/circleci/myteam
          # bring the git checkout, including all target dirs
          paths:
            - myproj
      - persist_to_workspace:
          root: /home/circleci
          # bring the big stuff
          paths:
            - .ivy2/cache
            - .sbt

  # actually runs the tests compiled in the previous job
  test-run:
    environment:
      SBT_OPTS: -XX:+UseConcMarkSweepGC -XX:+UnlockDiagnosticVMOptions  -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Duser.timezone=Etc/UTC -Duser.language=en -Duser.country=US
    docker:
      # run tests in the same image as before, but technically 
      # a different instance
      - image: myorg/myimage:sbt-1.2.8
    steps:
      # bring over all files 'persist_to_workspace' in the last job
      - attach_workspace:
          at: /home/circleci/myteam
      # restore ~/.sbt and ~/.ivy2/cache via `mv` from the workspace 
      # back to the home dir
      - run:
          working_directory: /home/circleci/myteam
          command: |
            [[ ! -d /home/circleci/.ivy2 ]] && mkdir /home/circleci/.ivy2

            for d in .ivy2/cache .sbt; do
              [[ -d "/home/circleci/$d" ]] && rm -rf "/home/circleci/$d"
              if [ -d "$d"  ]; then
                mv -v "$d" "/home/circleci/$d"
              else
                echo "$d does not exist" >&2
                ls -la . >&2
                exit 1
              fi
            done
      - run:
          # run the tests, already compiled
          # note: recompiles everything every time!
          working_directory: /home/circleci/myteam/myproj
          command: sbt test
          no_output_timeout: 3900s

workflows:
  version: 2
  build-and-test:
    jobs:
      - test-compile
      - test-run:
          requires:
            - test-compile

第二阶段的输出通常如下所示:

#!/bin/bash -eo pipefail
sbt test

[info] Loading settings for project myproj-build from native-packager.sbt,plugins.sbt ...
[info] Loading project definition from /home/circleci/myorg/myproj/project
[info] Updating ProjectRef(uri("file:/home/circleci/myorg/myproj/project/"), "myproj-build")...
[info] Done updating.
[warn] There may be incompatibilities among your library dependencies; run 'evicted' to see detailed eviction warnings.
[info] Compiling 1 Scala source to /home/circleci/myorg/myproj/project/target/scala-2.12/sbt-1.0/classes ...
[info] Done compiling.
[info] Loading settings for project root from build.sbt ...
[info] Set current project to Piranha (in build file:/home/circleci/myorg/myproj/)
[info] Compiling 1026 Scala sources to /home/circleci/myorg/myproj/target/scala-2.12/classes ...

我该怎么做才能确定为什么第二次重新编译所有源并缓解它?

我在 linux 容器中运行 sbt 1.2.8 和 scala 2.12.8。


更新

我还没有解决这个问题,但我想我会为我最糟糕的问题分享一个解决方法。

主要问题:将“测试编译”与“测试运行”分开次要问题:构建速度更快,而无需在每次推送时重新编译所有内容

我对二级没有办法。对于初级:

我可以从 CLI via而不是 via运行scalatest运行器,以避免任何重新编译的尝试。运行器可以对文件目录进行操作。scala -cp ... org.scalatest.tools.Runnersbt test.class

变更摘要:

  1. 更新 docker 容器以包含 scala cli 安装。(不幸的是,我现在需要保持这些版本同步)
  2. 构建阶段:sbt test:compile 'inspect run' 'export test:fullClasspath' | tee >(grep -F '.jar' > ~test-classpath.txt)
    • 编译但也记录一个可复制的类路径字符串,适合传递scala -cp VALUE_HERE给运行测试
  3. 测试阶段:scala -cp "$(cat test-classpath.txt)" org.scalatest.tools.Runner -R target/scala-2.12/test-classes/ -u target/test-reports -oD
    • 通过运行器运行 scalatest,使用 中的编译.class文件target/scala-2.12/test-classes,使用编译阶段报告的类路径,并将 printint 打印到标准输出以及报告目录

我不喜欢这个,它有一些问题,但我想我会分享这个解决方法。

4

5 回答 5

1

如果您使用的是比 1.0.4 更新的 sbt 版本,则缓存对您不起作用,因为编译器将始终使所有内容无效。此锌编译器问题已在此处报告:https ://github.com/sbt/sbt/issues/4168

我的建议是降级 CI 的 sbt 版本。还要检查和验证 CI 是否正在更改 .sbt 或 .ivy2 文件时间戳。如果它们被更改,请通过压缩和解压缩它们分别缓存它们。

我对 Bitbucket Pipelines CI 有同样的问题,并成功地让它在这里工作

于 2019-12-22T14:48:37.497 回答
0

SBT 对重新编译非常挑剔,而 Docker 给它带来了特别的麻烦。

看一眼:

于 2019-07-05T09:47:58.513 回答
0

我在 travis 构建中遇到了类似的问题,我怀疑这个解决方案也适用于 circle-ci。根本原因是缓存存储为tar文件,文件的修改时间只有第二个分辨率。您可以指定具有足够分辨率的格式。我的解决方案是创建一个小脚本travis_tar.sh

#!/bin/bash
/bin/tar-orig --format=posix $@

然后用这个脚本替换系统tar:

sudo mv /bin/tar /bin/tar-orig
sudo mv .travis/travis_tar.sh /bin/tar
sudo chmod +x /bin/tar

这可能在缓存加载后发生,香草系统 tar 解压 posix 格式的 tar 文件就好了。

于 2019-10-15T11:44:51.930 回答
0

我也有同样的问题。我放弃了尝试让所有时间戳匹配并最终发现我可以使用:

sbt 'set  Compile / compile / skip := true' 'test'

它仍然不是完美的,sourceGenerators可能还有一些其他的东西仍然可以运行,但它肯定比没有它要好得多。

于 2020-02-19T02:49:06.203 回答
0

我也在 gitlab 工作中使用 sbt 1.2.8 遇到了这个问题。以前(在 sbt 0.13 中)缓存target目录工作正常。

现在我正在尝试通过设置手动调试:

logLevel := Level.Debug,
incOptions := incOptions.value.withApiDebug(true).withRelationsDebug(true),

在我的构建中。这应该打印失效的原因。它产生的输出太多,无法在 CI 中运行,所以我无法重现我看到问题的确切条件。

于 2019-04-01T18:39:07.900 回答