Loading... Search articles

Search for articles

Sorry, but we couldn't find any matches...

But perhaps we can interest you in one of our more popular articles?
iOS caching with Tuist and Codemagic

Optimising iOS builds with Tuist and Codemagic CI/CD

Jul 20, 2023

This article is written by Himesh Panchal

๐Ÿ‘‹ Welcome to our guide on using Tuist and Codemagic CI/CD for mobile developers. In this guide, we’ll focus on how you can utilize the capabilities of Tuist, a command-line tool that helps maintain and interact with Xcode projects, in conjunction with Codemagic to expedite your build processes. We can significantly enhance build speed and efficiency through smart caching techniques. ๐Ÿš€

Comparative Results ๐Ÿ“Š

We tested on the M1 machine with our sample iOS app to demonstrate the performance gain.

Below are the results:

๐ŸŒ No-Cache Build Time: Approximately 2 minutes 51 seconds

๐Ÿš€ Cache Build Time: Approximately 16 seconds

As depicted, the cache build performed approximately 90% as fast as the no-cache build, showcasing the benefits of caching.

Sample iOS Native App workflow: Faster Builds in Action โšก

Consider our sample iOS native application workflow that relies on Swift Package Manager (SPM) dependencies. The traditional approach of no-cache builds means that each time we push an update or change our codebase, the CI/CD pipeline has to re-download and recompile all the dependencies. This can be a significant time sink, especially for larger apps with numerous dependencies.

Our sample workflow will use the Tuist Framework for caching and the Codemagic Platform to store compiled dependencies after the first build. The stored cache will be reused for subsequent builds instead of recompiling everything, resulting in significantly faster build times. ๐Ÿ’พ๐Ÿš€.

Configuring Tuist in codemagic.yaml workflow.

Install Tuist: This step involves installing the Tuist tool using a bash command that downloads and installs Tuist. Tuist is a command-line tool that helps to maintain and interact with Xcode projects.

- name: Install Tuist
  script: curl -Ls https://install.tuist.io | bash

Setup Tuist in Xcode project: After Tuist is installed, the tuist fetch command is run. This step is crucial as it fetches the latest version of Tuist and sets it up for your project.

- name: Setup Tuist in Xcode project
  script: tuist fetch

Generate Tuist Files: Next, the tuist generate command is run with the -no-open option, which instructs Tuist to generate necessary project files without opening the project in Xcode afterwards.

- name: Generate Tuist Files
  script: tuist generate MyiOSApp --no-open

Build Simulator App: This step is where the actual build process happens. xcodebuild is a command-line tool provided by Xcode that lets you perform build operations. In this case, it is building the specified workspace and scheme for the iPhone simulator. The CODE_SIGN_* variables are set to disable code signing for the build.

- name: Build Simulator App
  script: |
    xcodebuild build \
    -workspace "$CM_BUILD_DIR/$XCODE_WORKSPACE" \
    -scheme "$XCODE_SCHEME" \
    -sdk iphonesimulator \
    CODE_SIGN_IDENTITY="" \
    CODE_SIGNING_REQUIRED=NO \
    CODE_SIGNING_ALLOWED=NO    

Set up Tuist Caching: Lastly, tuist cache warm --dependencies-only is run to prepare or “warm” the cache. This step involves caching only the dependencies of the project. Cached artifacts are stored and can be reused in subsequent builds, saving the time needed to recompile them.

- name: Set up Tuist Caching
  script: tuist cache warm --dependencies-only

How to Store Cache on Codemagic CI/CD? โœจ

Storing cache on Codemagic is a straightforward process. After the initial build process, you can instruct Codemagic to cache the where the compiled SPM dependencies reside. This is achieved by adding a few lines to the codemagic.yaml file.

cache:
  cache_paths:
    - ~/.tuist/Cache

By performing these steps, the build pipeline is set up to take advantage of caching to improve build performance.

Here’s what the codemagic.yaml file could look like after implementation:

ios-native-cache-workflow:
  name: iOS Native Cache Workflow
  environment:
    vars:
      BUNDLE_ID: MyiOSApp.nevercode.io
      XCODE_WORKSPACE: "MyiOSApp.xcworkspace"
      XCODE_SCHEME: "MyiOSApp"
    xcode: 14.1
  scripts:
    - name: Install Tuist
      script: curl -Ls https://install.tuist.io | bash
    - name: Setup Tuist in Xcode project
      script: tuist fetch
    - name: Generate Tuist Files
      script: tuist generate iOSUnitTests --no-open
    - name: Build Simulator App
      script: |
        xcodebuild build \
        -workspace "$CM_BUILD_DIR/$XCODE_WORKSPACE" \
        -scheme "$XCODE_SCHEME" \
        -sdk iphonesimulator \
        CODE_SIGN_IDENTITY="" \
        CODE_SIGNING_REQUIRED=NO \
        CODE_SIGNING_ALLOWED=NO            
    - name: Set up Tuist Caching
      script: tuist cache warm --dependencies-only
  cache:
    cache_paths:
      - ~/.tuist/Cache      

Summary ๐Ÿš€

Caching is a powerful mechanism to expedite your CI/CD pipeline. By leveraging the caching capabilities of the Codemagic platform, we can significantly cut down on redundant tasks, such as recompiling dependencies, leading to faster and more efficient builds.

Related articles

Latest articles

Show more posts