持续集成 使用 Jenkins 构建由 carthage 管理 framework 的 iOS 工程

Nick · 2017年09月18日 · 最后由 securitytest 回复于 2017年09月26日 · 1963 次阅读
本帖已被设为精华帖!
  • 问题: carthage 每次都需要去 github 拉去代码并构建,大大增加了构建的时间。
  • 思考: 作为一个 framework 管理工具,肯定会支持缓存构建,所以去 github 上查看官方文档。
  • 解决办法:
    在官方文档中找到如下一段说明,即使用--cache-builds即表示使用缓存进行构建。

    Caching builds

    By default Carthage will rebuild a dependency regardless of whether it's the same resolved version as before. Passing the --cache-builds will cause carthage to avoid rebuilding a dependency if it can. See information on version files for details on how carthage performs this caching.

  • 执行:
    修改 Jenkins 脚本,将命令carthage update --platform iOS修改为carthage update --platform iOS --cache-builds,然后很开心的在 Jenkins job 上点了构建。

    carthage update --platform iOS --cache-builds
    *** Fetching Charts
    *** Checking out Charts at "v3.0.2"
    *** xcodebuild output can be found in /var/folders/64/xp952xv54zdb577b33ngw2rw0000gn/T/carthage-xcodebuild.JEKNY6.log
    *** No cache found for Charts, building with all downstream dependencies
    *** Building scheme "Charts" in Charts.xcodeproj

  • 但没找到 cache,怀疑需要第二次 build 才可以。但事实是第二次依然提示No cache found。肯定是哪里不对,回过头来再次查看官方文档,发现如下一段介绍:

    If you're building for iOS, tvOS, or watchOS
    ...
    6.Add the paths to the copied frameworks to the “Output Files”, e.g.:
    $(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/Result.framework
    $(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/ReactiveSwift.framework
    $(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/ReactiveCocoa.framework
    With output files specified alongside the input files, Xcode only needs to run the script when the input files have changed or the output files are missing. This means dirty builds will be faster when you haven't rebuilt frameworks with Carthage.

  • 然后对照我们的工程检查,发现并没有配置这个Output Files,原因应该就在这里了。随即找开发添加,并提交代码,触发构建。
    本以为这次可以命中 cache,但得到无效 cache 的提示:

    carthage update --platform iOS --cache-builds
    *** Fetching Charts
    *** Checking out Charts at "v3.0.2"
    *** xcodebuild output can be found in /var/folders/64/xp952xv54zdb577b33ngw2rw0000gn/T/carthage-xcodebuild.KZDaMB.log
    *** Invalid cache found for Charts, rebuilding with all downstream dependencies
    *** Building scheme "Charts" in Charts.xcodeproj

  • 这个就比较郁闷了,按照官方文档所有的配置都没有问题,参数也没有问题,而且在本地是可以命中 cache 的:

    carthage update --platform iOS --cache-builds
    *** Fetching Charts
    *** Checking out Charts at "v3.0.2"
    *** xcodebuild output can be found in /var/folders/64/xp952xv54zdb577b33ngw2rw0000gn/T/carthage-xcodebuild.gxGJC9.log
    *** Valid cache found for Charts, skipping build

  • 而且开发提交代码后,在我的电脑上和开发的电脑上都可以命中 cache,偏偏只有 Jenkins 无法命中 cache。
    这个时候已经没有其他可能,不会是配置或者参数的问题了,那么来查看一下 xcodebuild 的日志吧。
    发现构建生成的Charts.framework所有的路径中有8.1_8B62,而这个正式 Jenkins 服务器上 xcode 的版本。所以猜测 carthage 的 cache 和 xcode 的版本有关。

    Signing Identity: "-"

    /usr/bin/codesign --force --sign - --timestamp=none /Users/nick/Library/Caches/org.carthage.CarthageKit/DerivedData/8.1_8B62/Charts/v3.0.2/Build/Products/Release-iphonesimulator/Charts.framework

    ** BUILD SUCCEEDED **

  • 随即升级了 xcode 版本到 8.3.3,然后再次构建,成功命中缓存。

    carthage update --platform iOS --cache-builds
    *** Fetching Charts
    *** Checking out Charts at "v3.0.2"
    *** xcodebuild output can be found in /var/folders/64/xp952xv54zdb577b33ngw2rw0000gn/T/carthage-xcodebuild.8aIUyY.log
    *** Valid cache found for Charts, skipping build


关于Carthage

Carthage is intended to be the simplest way to add frameworks to your Cocoa application.

The basic workflow looks something like this:

  1. Create a Cartfile that lists the frameworks you’d like to use in your project.
  2. Run Carthage, which fetches and builds each framework you’ve listed.
  3. Drag the built .framework binaries into your application’s Xcode project.

Carthage builds your dependencies and provides you with binary frameworks, but you retain full control over your project structure and setup. Carthage does not automatically modify your project files or your build settings.

共收到 2 条回复 时间 点赞
simple [精彩盘点] TesterHome 社区 2018 年 度精华帖 中提及了此贴 01月07日 12:08
simple 专栏文章:[精华帖] 社区历年精华帖分类归总 中提及了此贴 12月13日 20:49

用--cache-builds 的确快很多,目前有些 repo 没法 cache,还需要重新拉一次

思寒_seveniruby 将本帖设为了精华贴 09月20日 11:11
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册