<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Loïs Di Qual on Medium]]></title>
        <description><![CDATA[Stories by Loïs Di Qual on Medium]]></description>
        <link>https://medium.com/@ldiqual_scoop?source=rss-f5c286f5661b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*8pUUqSgiTngxpmS0.</url>
            <title>Stories by Loïs Di Qual on Medium</title>
            <link>https://medium.com/@ldiqual_scoop?source=rss-f5c286f5661b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 11:53:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ldiqual_scoop/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Improve iOS CI build times with CocoaPods caching]]></title>
            <link>https://medium.com/takescoop/improve-ios-ci-build-time-with-cocoapods-caching-4a049ee45e63?source=rss-f5c286f5661b------2</link>
            <guid isPermaLink="false">https://medium.com/p/4a049ee45e63</guid>
            <category><![CDATA[scoop-eng]]></category>
            <dc:creator><![CDATA[Loïs Di Qual]]></dc:creator>
            <pubDate>Sat, 03 Feb 2018 01:15:45 GMT</pubDate>
            <atom:updated>2018-02-14T21:28:36.574Z</atom:updated>
            <content:encoded><![CDATA[<p>This post describes how to cache compiled pods frameworks to allow reusing them across multiple CI builds — saving about 5–10 minutes of CI run time depending on how many dependencies you have. We’ll use <a href="https://github.com/fastlane/fastlane">fastlane</a> to help build the app and Amazon S3 to save and restore the Pods cache.</p><h3>CocoaPods &amp; the Pods project</h3><p>Many — if not most — iOS apps now use CocoaPods to manage dependencies. Swift and ObjC libraries like Alamofire or RxSwift can be incorporated into an app just by adding a single line to a Podfile.</p><pre>target &#39;App&#39;<br>use_frameworks!<br>pod &#39;Alamofire&#39;</pre><p>CocoaPods analyzes the Podfile, generates an Xcode project called Pods, and adds it as part of a Xcode workspace that encompasses both your app and the Pods project:</p><pre>App workspace<br>  App project<br>    App source code<br>  Pods project<br>    Alamofire</pre><p>For the App project to build properly, Pods needs to build as well. Depending on how many dependencies you have and what languages they&#39;re written in, this can take a long time.</p><p>Many companies &amp; projects use Continuous Integration (CI) systems to lint, test, and build their apps. Often those CI runs happen on each commit pushed to version control and are required for pull requests to be merged. As developers wait on builds to finish to merge their code, each minute that we can shave off is a big win.</p><h3>Where can we save time?</h3><p>Let’s look at how Xcode archives a workspace:</p><ol><li>Xcode builds the Pods target and generates the proper frameworks (Pods.framework, Alamofire.framework, etc) in the $BUILT_PRODUCTS_DIRdirectory. By default this happens in the build/ directory at the root of your project.</li><li>Xcode builds the App target, links it against pods frameworks in $BUILT_PRODUCTS_DIR, then runs the Embed Pods Frameworks script that CocoaPods automatically adds to the project.</li><li>Xcode generates an archive containing the app, the pod frameworks (eg: Alamofire.framework), Pods.framework, the Swift standard libraries, and code signs the whole thing.</li></ol><p>Step 2 &amp; 3 can’t be optimized much. Your App code changes on each commit and unless you find a way to cache incremental builds of the App target, there’s not very much you can do about speeding it up.</p><p>Step 1, on the other hand, seems interesting. If we manage to retrieve compiled frameworks from an earlier build, then Xcode could detect them and skip the Pods build altogether.</p><p>In theory, this means we’d only ever have to build the Pods target once, store the built frameworks to a remote server, then all future CI builds could download those frameworks and link against them. Not only does this save time for release builds, but this principle can also be applied when building apps for UI &amp; unit tests in debug mode.</p><p>To do this, we need to:</p><ul><li>Restore the cache from S3 if it exists</li><li>Build the Pods + App projects separately</li><li>Embed pods frameworks in the App archive</li><li>Save the cache to S3</li></ul><h3>Building projects separately</h3><p>In fastlane terms, this is how you’d archive a whole workspace (Pods + App):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a4ae551e97f6879076676ebc566ad80c/href">https://medium.com/media/a4ae551e97f6879076676ebc566ad80c/href</a></iframe><p>Since we want to save generated pod frameworks to retrieve them during another build, we need to build the Pods + App projects separately.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e7c3d1b019f728fd2c033b58626b122/href">https://medium.com/media/7e7c3d1b019f728fd2c033b58626b122/href</a></iframe><p>Let’s look at what happens here. First we build the Pods project in release mode. This generates the following files:</p><pre>build/<br>  Release-iphoneos/<br>    Alamofire/Alamofire.framework<br>    # ...<br>    # ... other pod frameworks<br>    # ...<br>    Pods.framework</pre><p>Then we build the App project in release mode, by providing two crucial xcodebuild arguments:</p><ul><li>$PODS_CONFIGURATION_BUILD_DIR is the base path where pod frameworks will be searched at linking time. Xcodebuild looks for $PODS_CONFIGURATION_BUILD_DIR/Alamofire/Alamofire.framework so we need to set $PODS_CONFIGURATION_BUILD_DIR to build/Release-iphoneos.</li><li>$FRAMEWORK_SEARCH_PATHS is a list of paths where the linker looks for frameworks. By default it contains paths to each pod framework (eg: Alamofire.framework). Since we also need to link against Pods.framework, we need to add build/Release-iphoneos there.</li></ul><p>At this point the Pods and App project will get built separately and the App project will be aware of the frameworks generated in the Pods project.</p><h3>Embedding Pods Frameworks</h3><p>There’s one more thing we need to do before we can get a successful build. Remember the Embed Pods Frameworks run script that CocoaPods automatically adds to your project? It takes care of copying pod frameworks to the proper directory so that they&#39;ll be part of the final archive.</p><p>A dumbed down version of the script looks like this:</p><pre>cp &quot;$BUILT_PRODUCTS_DIR/Alamofire/Alamofire.framework&quot; \<br>   &quot;$TARGET_BUILD_DIR/$FRAMEWORKS_FOLDER_PATH/&quot;</pre><p>Since the script expects the whole workspace to get built at once, it looks for frameworks in $BUILT_PRODUCTS_DIR but they are nowhere to be found. Due to a lack of sanity checks, the script then copies all files in the repo to $TARGET_BUILD_DIR and tries to codesign them, which of course fails badly.</p><p>Unfortunately I couldn’t find way to set $BUILT_PRODUCTS_DIR to our cache directory by adjusting arguments passed to xcodebuild. The only option we have is to modify the run script so that we can pass another value for $BUILT_PRODUCTS_DIR.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b3a4dd4119ae6717728c250ad8199a4/href">https://medium.com/media/5b3a4dd4119ae6717728c250ad8199a4/href</a></iframe><p>Since the Embed Pods Frameworks run script gets overridden on every pod install, modifying it in Xcode would be pointless. Instead, let&#39;s add a new lane that patches the run script for us. We&#39;ll run this lane before building the App project in CI builds.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f5b391c42d47759081f84a4cbc2a8e5/href">https://medium.com/media/8f5b391c42d47759081f84a4cbc2a8e5/href</a></iframe><p>To patch the project, run:</p><pre>$ fastlane patch_app_project_for_pods_cache</pre><p>Now we can build the Pods + App projects separately using the commands above and everything will build, link, copy, and codesign properly.</p><h3>Saving, checking, and downloading the Pods cache</h3><p>Now that we can build our pod frameworks separately, all we need to do it save them for a later reuse. Pod frameworks are stored in the build/Release-iphoneos directory, so let&#39;s zip it up and upload that to S3. But first, we need a way to juggle between different caches in case that the Podfile changes between builds.</p><h4>Cache key</h4><p>At Scoop we use S3 to store our Pods cache but you can use any other remote storage mechanism. While a cache is just a zip of the build/Release-iphoneosfolder, it needs to be specific to the set of pod versions that the project is using.</p><p>If any pod version changes (for instance Alamofire 5.0.0 to 5.0.1) a new cache needs to get generated. So we can’t really store our cache to a generic cache.zip file, otherwise we run into the risk of using a cache that contains the wrong version of a Pod. This is why we need to generate a <strong>cache key</strong> that stays the same as long as pod versions stay the same.</p><p>A good way to generate a cache key is to combine:</p><ul><li>The SHA1 of Podfile so that if Podfile changes then the key changes as well</li><li>The version of Xcode used to build the cache, so that upgrading to another version of Xcode changes the cache key as well</li><li>The configuration (Debug or Release) used when building the cache</li></ul><p>In fastlane terms:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f0ff14fbd5dc9f04d9098cdc66aae6d/href">https://medium.com/media/1f0ff14fbd5dc9f04d9098cdc66aae6d/href</a></iframe><p>Which generates the following key:</p><pre>s3://S3_BUCKET/cache/pods_debug_ea0b39fcbcca4ee58eea4fa2178eb420fc0f2862_xcode-9C40b.zip</pre><h4>Saving the cache</h4><p>Saving the cache consists of zipping the ./build/Release-iphoneos folder and uploading it to S3 with the cache key.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e045793c436656e95cf62eb731c4e64/href">https://medium.com/media/5e045793c436656e95cf62eb731c4e64/href</a></iframe><p>You’ll need to have the AWS cli installed on your CI, which on CircleCI is a matter of adding the following line to dependencies:override:</p><pre>HOMEBREW_NO_AUTO_UPDATE=1 brew install awscli</pre><h4>Checking whether the cache exists</h4><p>We can check whether the cache already exists by querying S3:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/190400e7b879c6454784c7154325b662/href">https://medium.com/media/190400e7b879c6454784c7154325b662/href</a></iframe><h4>Restoring the cache</h4><p>Restoring the cache in later builds is a matter of fetching a zip file by its S3 key, then unzipping it in the build/ folder:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f1ac34aee6ad220901d8a417cd3db83a/href">https://medium.com/media/f1ac34aee6ad220901d8a417cd3db83a/href</a></iframe><h4>Automatically download or build the cache</h4><p>To glue it together, let’s add a lane that downloads the cache if it exists, or builds it if it doesn’t.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e0c6cd7109bd06198cb2ab7ce9bfa76d/href">https://medium.com/media/e0c6cd7109bd06198cb2ab7ce9bfa76d/href</a></iframe><h3>Putting it together</h3><p>Now that we have all our little building blocks — saving / checking / restoring / building the Pods cache and patching / building the App project — we can run all our lanes like this:</p><pre>$ fastlane restore_or_build_pods_cache<br>$ fastlane patch_project_for_pods_cache<br>$ fastlane build_app</pre><p>You can find Scoop’s Fastfile here: <a href="https://gist.github.com/ldiqual/b060cff88f8ad2147fefa7f5af210e02">https://gist.github.com/ldiqual/b060cff88f8ad2147fefa7f5af210e02</a></p><p>It contains a lot of Scoop specific things to support different targets &amp; services but this will give you an idea of how we apply this in production.</p><h3>Results &amp; Limitations</h3><p>For each commit pushed to Scoop’s iOS repo, CircleCI runs the following steps:</p><ol><li>Checkout source code</li><li>Run pod install</li><li>Lint the project</li><li><strong>Build app for tests: ~5&#39;</strong></li><li><strong>Run unit and integration tests: ~7&#39;</strong></li><li><strong>Build app for QA: ~10&#39;</strong></li><li>Upload IPA to S3</li></ol><p>This would usually take about 25 minutes total. By implementing the Pods caching strategy described above we saved about 5 minutes for both steps 4) and 6), which decreases overall build time to 15–20 minutes.</p><p>Here’s what CircleCI build times look like for the past year, excluding outliers:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/945/1*fUZi3suF4ihIY_bKHfT17g.png" /></figure><p>We have been using this system for about 4 months and it has worked pretty consistently.</p><p>There are significant limitations though:</p><ol><li>It does make it harder to maintain the codebase because for each new app target (eg: Scoop Alpha, Scoop Beta) or extensions (eg: notification services) you need to update the Fastfile so that it also compiles a pods cache for those targets. This doesn’t happen often but we need to be aware of it.</li><li>There is a chance that this could break in future versions of CocoaPods if the Embed Pods Frameworks script changes, or even in future versions of Xcode if the build folder were to change (ie: build/Release-iphoneos).</li><li>A couple things in this technique — namely that it uses a dependency cache without being 100% sure that it is generated under the same conditions (env vars, pod versions, build settings, …) than the current build, and the fact that it patches an Xcode project to hack around a CocoaPods script — makes it unsuitable for production builds. It might work, but we recommend against it.</li></ol><p>Given that we’re limiting this to test and internal builds, we think those limitations are acceptable. We recommend giving pods caching a shot if you care about build times!</p><p>Interested in tackling exciting iOS challenges and putting living, breathing human beings in cars together? Hit us up at <a href="/service/mailto:jobs@takescoop.com">jobs@takescoop.com</a> or visit <a href="http://www.takescoop.com/careers">takescoop.com/careers</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4a049ee45e63" width="1" height="1" alt=""><hr><p><a href="https://medium.com/takescoop/improve-ios-ci-build-time-with-cocoapods-caching-4a049ee45e63">Improve iOS CI build times with CocoaPods caching</a> was originally published in <a href="https://medium.com/takescoop">The Inside Scoop</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>