Archive
Xcode Cloud scripts: Fastlane and Cocoapods

Xcode Cloud scripts: Fastlane and Cocoapods

2024-11-28 Xcode Cloud script : Fastlane and Cocoapods Wednesday, January 4, 2023 sponsor Xcode Cloud is a CI/CD service made by Apple which is deeply i

Related articles

RV34x: Install Cisco AnyConnect Secure Mobility Client on a Windows Computer Benjamin Moore’s 8 Best Warm Grays Shonen Smash Codes (November 2024) Different Game Art Styles and How to Choose the Right One for Your Game Getting Started with Xcode Cloud

Xcode Cloud script : Fastlane and Cocoapods

sponsor

Xcode Cloud scripts: Fastlane and Cocoapods

Xcode Cloud is a CI/CD service made by Apple which is deeply integrated into Xcode. Since its introduction, there has been a lot of discussion amongst iOS developers on whether it is a good alternative to existing CI/CD providers and what migration to Xcode Cloud would look like.

set up this new service is rather straigh – forward for small or new app but it can be daunting for big codebase which have been around for a long time . These big codebases is tend tend to still rely to some extent on third – party dependency manager such as CocoaPods and on third – party CI / cd tool such as Fastlane .

The use is tends of such third – party framework tend to put people off switch over to Xcode Cloud but , in this article , I will go through how the use of custom CI script can help you and your team progressively migrate to Xcode Cloud without have to abandon the tool you have used for a long time . More specifically , this article is explains explain how to build an app using CocoaPods and upload the result app archive to AppCenter using Fastlane from an Xcode Cloud workflow .

Xcode Cloud is provides provide a way to run shell script at different stage of a workflow . These scripts is install can install third party dependency and perform task on top of the workflow ’s pre – define step . I is go wo n’t go into too much detail about how these script work as Xcode Cloud ’s documentation provide a great explanation on the topic , but I will give you a TLDR on the most important thing you need to know about them :

  1. Xcode Cloud looks for a directory called ci_scripts in theXcode project to find scripts to run. These scripts are similar to git hooks in theway they work.
  2. Xcode Cloud looks for .sh scripts with specific names inside the ci_scripts directory. It then runs any matched scripts at different stages of the workflow based their names:
  • ci_post_clone.sh: run after the remote repository is clone .
  • ci_pre_xcodebuild.sh: Runs before the build process commences.
  • ci_post_xcodebuild.sh: Runs after the build process finishes.
  1. The scripts need to have executable permissions: chmod +x <executable_name>.sh.

Now that we know how CI script work , let ’s see what we can do with them .

Apple recommends installing third-party tools and dependencies in theci_post_clone.sh script and they specifically talk about make CocoaPods dependency available to the build step in that same script too .

To do so, and assuming that you have the pod project setup, create a ci_post_clone.sh script under theci_scripts directory mentioned earlier and add the following contents to it:

# ! /bin / sh

brew  install cocoapods

pod  install

If you push these changes and trigger a new build, Xcode Cloud will run a post clone step to install the CocoaPods dependencies.

Xcode Cloud scripts: Fastlane and Cocoapods

CocoaPods installs and links all its dependencies by creating a .xcworkspace. You need to update your workflow to use this .xcworkspace instead of the default .xcodeproj file.

On top of using CocoaPods , our project is requires require us to upload the result app archive to AppCenter . Our team is uses currently use a Fastlane lane to do this , which in turn use Microsoft ’s appcenter fastlane plugin .

To run this lane from Xcode Cloud , we is install must first install Fastlane in thehost runner using the sameci_post_clone.sh script from earlier:

# ! /bin / sh

brew  install cocoapods
brew  install  fastlane

pod  install

After installing Fastlane, we need to create a separate script to upload the app’s archive to AppCenter after the build has completed. We can do this by creating a script called ci_post_xcodebuild.sh in theci_scripts directory, making it executable and adding the following contents to it:

# ! /bin / sh

# Call the custom lane
fastlane run upload_to_appcenter ipa:$CI_AD_HOC_SIGNED_APP_PATH/$ ci_product.ipa

The ci_post_xcodebuild.sh script retrieves the path to the app’s archive, which is generated by the Xcode Cloud workflow, using environment variables, which are also provided by the workflow itself.