Categories:
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 continuous integration and delivery (CI/CD) with Codemagic

Jul 13, 2020

codemagic codemagic

Get 500 build minutes FREE each month

Join over 12k developers building their Android, iOS, Flutter or React Native apps with Codemagic CI/CD
Sign up now

Written by Souvik Biswas

It does not matter whether you are building native iOS, Android, React Native or Flutter applications, Codemagic CI/CD is there for you. Alhough it does not come with support for building native Android and iOS apps directly using the UI, you can easily configure the Codemagic YAML file for building native apps. With the updated first build flow, configuring and building your native apps is even smoother than before.

In this article, you will learn how to use the codemagic.yaml file to build, test and deliver native iOS apps on Codemagic.

It is recommended to go through the article for better understanding, but if you already have experience with using codemagic.yaml file, you can get the YAML template for native iOS project here.

Getting started

Make sure that your native iOS project is uploaded to a code hosting platform (like GitHub, Bitbucket or GitLab) using a version control system. Follow the steps below to get the codemagic.yaml file template:

  1. Log in to Codemagic.

  2. In the Applications overview, search for your project and click Set up build.

  3. Select iOS App as the starter workflow.

  4. You can download the YAML template for native iOS by clicking the download button.

  5. Now, open the codemagic.yaml using any code editor.

The file contents will look like this:

# Workflow setup for building Native iOS project
workflows:
    ios-project: # workflow ID
        name: iOS project # workflow name
        environment:
            xcode: latest
            cocoapods: default
        scripts:
            - xcodebuild build -workspace "<your_xcode_workspace>.xcworkspace" -scheme "<your_scheme>" CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO CODE_SIGNING_ALLOWED=NO
        artifacts:
            - $HOME/Library/Developer/Xcode/DerivedData/**/Build/**/*.app

Default YAML file

You will see that a default workflow ID and workflow name is specified, but you can change them as you wish. The workflow uses the latest versions of both Xcode and CocoaPods.

The default iOS workflow specified in the YAML file is very simple and will only generate a .app file of your iOS app.

Make sure to replace the two angle brackets with Xcode workspace name and Scheme respectively. You can also use -project flag instead of -workspace if you want to specify the Xcode project name.

In order to generate the .ipa file, you will need to set up code-signing.

Setting up code signing

For code signing, you will need two files:

  • Certificate
  • Provisioning profile

If you just want to generate the .ipa file for personal use, you can use the iOS Development Certificate.

To publish your app on App Store Connect, you need to use iOS Distribution Certificate. You can follow the article here.

Follow the steps below:

  1. Open Xcode.

  2. Go to Xcode > Preferences from the navigation bar.

  3. From the top menu of the window, select Accounts.

  4. Now, select the Team under your Apple ID for which you want the certificate.

  5. Click Manage Certificates.

  6. Right click the Development Certificate and select Export Certificate.

  7. Enter the certificate name, password and path for storing the certificate.

  8. To generate the provisioning profile, click Download Manual Profiles.

    This will save the profile in the directory ~/Library/MobileDevice/Provisioning Profiles/

Deep dive into YAML

Let's take a closer look at the codemagic.yaml file.

The default script is only for generating the .app file. To generate the .ipa of your iOS app, you have to add some environment variables for code signing.

Adding environment variables

You need to add the certificate and the provisioning profile in encrypted form as key-value pairs in the environment variables section of the codemagic.yaml file.

We will be adding the following keys:

  • CM_CERTIFICATE (encrypted version of the certificate)
  • CM_CERTIFICATE_PASSWORD (encrypted version of the certificate password)
  • CM_PROVISIONING_PROFILE (encrypted version of the provisioning profile)

You can generate the encrypted version of these files/variables by following these steps:

  1. Go to the project settings.

  2. Click Encrypt environment variables.

  3. Here, you can drag and drop the files or enter the variables to generate their encrypted version.

Modifying scripts

You need to make some changes to the script to generate the .ipa file of the app.

  • Initialize the keychain.

    - keychain initialize
    
  • Decode the provisioning profile and place it in a folder where it can be accessed during the code signing process.

    PROFILES_HOME="$HOME/Library/MobileDevice/Provisioning Profiles"
    mkdir -p "$PROFILES_HOME"
    PROFILE_PATH="$(mktemp "$PROFILES_HOME"/$(uuidgen).mobileprovision)"
    echo ${CM_PROVISIONING_PROFILE} | base64 --decode > $PROFILE_PATH
    echo "Saved provisioning profile $PROFILE_PATH"
    
  • Decode the signing certificate and add it to the keychain.

    echo $CM_CERTIFICATE | base64 --decode > /tmp/certificate.p12
    keychain add-certificates --certificate /tmp/certificate.p12 --certificate-password $CM_CERTIFICATE_PASSWORD
    
  • Run tests.

    xcodebuild \
    -project BMI\ Calculator.xcodeproj \
    -scheme BMI\ Calculator \
    -sdk iphonesimulator \
    -destination 'platform=iOS Simulator,name=iPhone 11 Pro Max,OS=13.4' \
    clean build test CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO
    

    If you face any errors related to the -destination flag while running the tests, just make sure that the IPHONEOS_DEPLOYMENT_TARGET version in the project.pbxproj file is matching the destination Simulator OS version of this command.

  • Use the profile and build the .ipa file.

    xcode-project use-profiles
    xcode-project build-ipa --project "BMI Calculator.xcodeproj" --scheme "BMI Calculator"
    

Collecting artifacts

You need to define the path to the generated .ipa file to retrieve it from the Codemagic build system.

artifacts:
    - build/ios/ipa/*.ipa

Publishing

To get a report of the build along with the generated artifacts in your email, specify the following:

publishing:
    email:
      recipients:
        - name@example.com # enter your email id here

For more information about publishing, refer to this link.

You have successfully completed the workflow setup for building a native iOS app.

Building on Codemagic

Before starting a build on Codemagic, make sure that your have the codemagic.yaml file committed to the version control system.

Follow the steps below to start a build:

  1. In the Applications dashboard, click Finish build setup.

  2. Click Check for configuration file.

  3. Then, click Start your first build.

  4. Select the workflow from the codemagic.yaml file and click Start new build.

This will start a new build for your native iOS project.

Build Complete
Build Complete

Conclusion

Using the codemagic.yaml file gives you some added advantage as it is a lot easier to manage your workflows and keep your build configurations more organized. Also, it gets automatically detected from the version control system when starting a build on Codemagic.


Souvik Biswas is a passionate Mobile App Developer (Android and Flutter). He has worked on a number of mobile apps throughout his journey. Loves open source contribution on GitHub. He is currently pursuing a B.Tech degree in Computer Science and Engineering from Indian Institute of Information Technology Kalyani. He also writes Flutter articles on Medium - Flutter Community.

Latest articles

Show more posts