Codemagic

Make you’ve read our Fastlane documentation to understand how the Emerge Fastlane plugin makes it easy to upload builds in your CI pipeline.

Configuring access to Emerge in Codemagic

Create an Emerge API key and add the API Key under the environment variables section in Codemagic.

Name the variable as EMERGE_API_TOKEN and put it under the group emerge_credentials. Then, you can refer to the variable group in codemagic.yaml as:

workflows:
  ios-workflow:
    name: iOS Workflow
    environment:
      groups:
        - emerge_credentials

Fastlane Action

The Fastlane action will upload either a release build or development build to Emerge depending on the branch used to trigger the build.

When a build is triggered by a pull request, Codemagic merges the master branch into your PR branch. We can access the SHA of the merge commit's parent to obtain a base_build_id that gets passed to the Emerge action.

When a build is triggered from the master branch we upload a master build type with the current git SHA as the build_id.

Here’s an example Fastfile that brings it all together:

fastlane_require 'git'

default_platform(:ios)

git = Git.open('..')

platform :ios do
  lane :emerge_app_upload do
    BRANCH = ENV["CM_BRANCH"]
    IS_PULL_REQUEST = ENV["CM_PULL_REQUEST"]
    PR_NUMBER = ENV["CM_PULL_REQUEST_NUMBER"]
    REPO_NAME = ENV["CM_REPO_SLUG"]
    CURRENT_BUILD_ID = ENV["CM_COMMIT"]
    FILE_PATH = "/build/ios/xcarchive/swiftly.xcarchive"
    BASE_BUILD_ID = git.log[0].parent.sha
    PARENT_BUILD_ID = git.log[0].sha
    
    if IS_PULL_REQUEST == "true"
      emerge(
        file_path: FILE_PATH,
        build_type: "pull_request",
        repo_name: REPO_NAME,
        pr_number: PR_NUMBER,
        sha: CURRENT_BUILD_ID,
        base_sha: BASE_BUILD_ID
      )
    elsif BRANCH.eql? "main"
      emerge(
        file_path: FILE_PATH, 
        build_type: "main",
        repo_name: REPO_NAME,
        sha: PARENT_BUILD_ID
      )
    end
  end
end

This script checks if the current build is building a pull request. If it is a pull request, it takes the source commit of the build and compares it to the build of the base commit hash. Then, it uploads to Emerge for processing. Otherwise, it uploads the build to Emerge with the type "main".

To use it in your codemagic.yaml, use the lane that you created:

- name: Upload archive to Emerge Tools
   script: bundle exec fastlane emerge_app_upload

Example codemagic.yaml file

You can upload the iOS build to Emerge Tool as a part of your Codemagic CI/CD workflow to automate the process. Here is an example of scripts you can add to your codemagic.yaml for building the archive and uploading it to Emerge:

scripts:
      - name: Bundle install
        script: | 
          bundle install
      - name: Install Emerge Fastlane plugin
        script: |
          fastlane add_plugin emerge
      - name: Build ipa for distribution
        script: | 
          xcode-project build-ipa --project "$XCODE_PROJECT" --scheme "$XCODE_SCHEME"
      - name: Upload archive to Emerge Tools
        script: | 
          bundle exec fastlane emerge_app_upload