Continuous Integration for iOS – Create a build job

| 0 comments

Now that the Jenkins server is running as a Launch Agent, it’s time to create a build job for building and testing an app.

  1. The first step is to provide Jenkins the necessary certificates and provisioning profiles. Add them as always to the keychain of the current user.
    Attention! If the Jenkins asks for access to the Keychain, press Always allow in the appearing popup. Otherwise the build process will stop. Alternatively you can unlock the keychain via script (this exposes your password – not recommended!)Additionally I had to login to the Apple developer account in Xcode, otherwise the provisioning profiles won’t be recognised.

     $ security unlock-keychain -p <MAC_USER_PASSWORD> (<KEY_CHAIN_PATH>)
  2. Choose New Item from the left side of the Jenkins dashboard.
  3. Choose Build a free-style software project, give it a speakable name and click OK.
  4. Add a description (html is supported) that explains what the job is doing, when it is triggered and which steps are executed.
  5. Select Discard Old Builds and set Max # of builds to keep to 10.
  6. Configure Source Code Management (the repository url, credentials and the branch to build).
  7. Configure Build-Trigger (e.g. poll SCM every 2 minutes: ‘H/2 * * * *’)
  8. Choose Add build step. If you want to use the Xcode plugin, choose Xcode and fill out the parameter inputs. Or choose Execute shell if you want to follow along and use the script approach.
    If you want to leverage the full power of the script approach, have a look at this build system.

 


 

Script code for building the app

  • Organise Output
    BUILD_DIRECTORY=~/Desktop/Jenkins_Build
    ARTEFACTS_DIRECTORY=~/Desktop/Jenkins_Artefacts/${BUILD_TAG}
  • Clean build directory before building
    rm -rv $BUILD_DIRECTORY
    mkdir -p $BUILD_DIRECTORY
  • Set Xcode version (if a different version is required)
    export DEVELOPER_DIR=/Applications/$XCODE_APP_NAME.app/Contents/Developer
  • Print current Xcode version
    echo “Xcode version: “ `xcodebuild -version`
    echo “Xcode path: “ `xcode-select -print-path`
  • Build the app
    xctool 	-project ${ProjectName} \
    	-scheme JenkinsTest \
            -sdk iphoneos \
           	-configuration Release \
            CONFIGURATION_BUILD_DIR=$BUILD_DIRECTORY \
            clean \
            build
    
  • Create IPA / Signing the app (Specify a special certificate and provisioning profile if you want to create ipa files for different use cases)
    xcrun	-sdk iphoneos \
    	PackageApplication \
            -v $BUILD_DIRECTORY/JenkinsTest.app \
            -o $BUILD_DIRECTORY/JenkinsTest.ipa \
    	#-sign “iPhone Distribution” \
    	#-embed PATH/Example_Ad_Hoc.mobileprovision
    
  • Zip dsym for distribution
    zip -r $BUILD_DIRECTORY/$PROJECT_NAME.dSYM.zip BUILD_DIRECTORY/*.app.dSYM
  • Save artefacts
    mkdir -p $ARTEFACTS_DIRECTORY
    mv $BUILD_DIRECTORY/$PROJECT_NAME.dSYM.zip $ARTEFACTS_DIRECTORY/
    mv $BUILD_DIRECTORY/JenkinsTest.ipa $ARTEFACTS_DIRECTORY/
    

 

Script code for testing the app

  • Build tests and write test report. (If you want to run tests against multiple SDKs, have a look at the xctool documentation)
    xctool	-workspace SampleProject.xcworkspace \
           	-scheme ExampleApp \
           	-reporter plain \
           	-reporter junit:test-reports/report.xml \
           	test \
           	-freshSimulator \
           	-freshInstall
    

 

Publish test results (JUnit-compatible output)

  1. Choose Add post-build action and then choose Publish JUnit test result report.
  2. Add test-report/*.xml into the field Test report XMLs.
  3. After some builds, when enough data was collected, a graph will be shown in the status view of the build job.
Test results

Test results

 

📌  The last post in this series shows how to get insights in code quality with code metrics…

Leave a Reply

Required fields are marked *.