iOS Jenkins Git Gradle TestFlight

Jenkins iOS – Git, xcodebuild, TestFlight

knowventBanner

Introduction with Jenkins iOS

If you are new to continuous integration for mobile platforms then you are in the right place. This article will explain how to setup a fully automated continuous integration environment. To do this we will use the following:

  • Jenkins: Our Continious integration server application
  • Git: Our code repository,
  • TestFlight: Our mobile testing distribution platform and each popular mobile platform
  • XcodeBuild: The build tool for iOS

The life cycle of a build will look like this:

  1. Developer commits and pushes their changes to the Repo
  2. Jenkins iOS Server Job monitors Git Repo and triggers a build on the developer commit
  3. Jenkins iOS Server Job executes the Build Scripts (XcodeBuild) checked in to the Repo
  4. Jenkins iOS Server Uploads completed build to TestFlight

As you can tell by the workflow above, Jenkins is pretty much the orchestrator of this process. Jenkins essentially replaces the developers duties of building, checking for errors, running unit/functional/integration tests and publishes those builds out to the business or end users. Hence, the name Jenkins.

Get Jenkins Installed on a Server Environment

Step 1: Download Jenkins Server.

Mac

Step 2: Install Jenkins

As noted before Jenkins is build on the Java Run-time. You will need to install the Java Runtime if your system doesn’t already have it installed.

Jenkins iOS will install a daemon that will allow it to run on it own, whether someone is logged in or not. Jenkins installer will create a user called Jenkins. It is recommend that you follow these setup to ensure the Jenkins user is correctly configured:

  1. After installing Jenkins go to into system preferences –> Group and Users.
  2. Unlock the panel. Give the “Blank” user a full name so you recognize them.
  3. Change the password to something you know.
  4. Grant the user admin rights. Here is a how-to grant admin rights tutorial.
  5. Log off the current user and log in using Jenkins. Finish this tutorial while logged in with Jenkins user.

Setup 3: Open the dashboard

You will setup and manage Jenkins iOS using a browser that points to a website URL. Typically it will be accessible via http://localhost:8080 while you are on the machine Jenkins iOS is installed.

Jenkins iOS Dashboard

Get Jenkins iOS Setup

Jenkins iOS Management Screen

Jenkins out of the box is fairly vanilla. This is good since it allows for it to be customized. Customization comes in the form of Plugins. For this tutorial, we are going to need the following Plugins:

  • Git Plugin: This will allow Jenkins to Monitor and pull code from our git repositories that will in turn trigger the build.
  • TestFlight Plugin: This will streamline the post build upload process for submitting your ipa files to TestFlight.
  • Xcode Plugin: This plugin allows Jenkins to call Xcode command line tools.

Setting up the server

Configuring Jenkins

Most of the default setting in the root Dashboard –> Manage Jenkins –> Configure System section is sufficient to get started. However, some routines, such as the xcodebuild, relay on xcode being installed. So you will need to ensure Jenkins execution context has any necessary environment setup.

Create a Jenkins Job

This is usually done after everything has been setup. For the purposes of this tutorial, we will have an initial job setup in advance.

Create Jenkins iOS Job

Select New Job –> Enter a Job Name –> Select Build a free-style software project

This will give use vanilla Jenkins job that we can customize our CI process with our desired plugins.

Manage Jenkins Android iOS

Using the Git Plugin

In order to use the Git Plugin, your build server needs to have Git installed. For Mac, this comes installed along with XCode. If not, you should: Open terminal –> Execute command xcode-select --install

Step 1: Install and Enable Git Plugin

At the Root of the Jenkins website Select Manage Jenkins –> Select Manage Plugins

Jenkins iOS Manage Plugin

Select the Available Tab, Type Git in the search, Check the box and install/restart Jenkins.

Step 2: Setup Source Control Management Settings

The purpose of this setup is to perform a git Clone command on your configured Repository and the designated branch. This will pull down the latest commit locally so the build can be invoked.

Using the Job that was created earlier we will configure it’s Source Control Management section to use Git. Starting at the Jenkins Dashboard you should see you list of Jobs created. Click on iOS Job –> Then click on Configure on the left pane menu.

Jenkins Source Control Management

The 2.0 Version of this plugin seems to fail setting credentials on a mac. You might see this error when setting up could not lock config file .git/config. I used the 1.5 version over the 2.0 because of this BUG. You can find the git plugin version 1.5 here.

The 1.5 version of the Git Plugin doesn’t support credentials (only SSH). So it order to get this to work with a username and password I needed to pass the credentials via URL like this: https://username:password@github.com/you/example.git

At this point, if you execute your job then Jenkins should perform a fetch against your configured Git repository and clone the project files into the job’s workplace (which should be: {Jenkins Home Folder}/jobs/<Your Job Name>).

So, give it a try.

Step 3: Add and Setup SCM Polling

In the Build Triggers section of the Job Configuration screen you will be able define the polling Interval. These configuration accepts cron syntax to define the polling schedule.

Simply Check the Poll SCM check box and define your cron schedule. Below illustrates a 5 min polling (this is too often of a schedule to run all the time but for tutorial and testing purposes it is acceptable).

Jenkins iOS Git Polling Trigger

Ideally, I would like to use a Git Hook to push a trigger to Jenkins but unfortunately I am using TFS with a Git bridge interface that doesn’t support Git hooks. So, I am subjected to polling for changes.

If you would like for information about setting up a Git Hook to trigger Jenkins builds Kohsuke Kawaguchi wrote a great post on this.

At this point, if you do a commit to your repository Jenkins will identify the change execute the Job.

Go ahead, give it a try.

Step 4: Add and Setup Build Step – iOS Job

This assumes your build environment has xcode, xcodebuild tools, provisioning profiles and necessary certifications installed.

The shell script I have written below performs a clean, run a build, create an signed archive and an exported ipa for adhoc deployments.

Navigate in finder or terminal to your job’s workspace (something like ~/jobs/{your Job Name}/workplace.

Create a file called iOS_Build.sh

Copy the script below into your newly create iOS_Build.sh file while replacing your project specific information (PROVISIONING_PROFILE, CODE_SIGN_IDENTITY, exportProvisioningProfile name and an appropriate name for your xcarchive/ipa files)

Create the following directories ./JenkinsBuild, ./JenkinsArchiveand./JenkinsIPAExport`.

xcodebuild -alltargets clean

rm -rf "./JenkinsBuild/*"

xcodebuild -target HelloJenkins PROVISIONING_PROFILE="00000000-0000-0000-0000-000000000000" CONFIGURATION_BUILD_DIR=JenkinsBuild

rm -rf "./JenkinsArchive/*"

xcodebuild -scheme HelloJenkins archive PROVISIONING_PROFILE="00000000-0000-0000-0000-000000000000" CODE_SIGN_IDENTITY="iPhone Developer: Justin Hyland (XXXXXXXXXX)" -archivePath ./JenkinsArchive/HelloJenkins.xcarchive

rm -rf "./JenkinsIPAExport/*"

xcodebuild -exportArchive -exportFormat IPA -exportProvisioningProfile iOS\ Team\ Provisioning\ Profile:\ com.yourAPP.HelloJenkins -archivePath ./JenkinsArchive/HelloJenkins.xcarchive -exportPath ./JenkinsIPAExport/HelloJenkins.ipa

If you get this error error: can't exec '/Developer/usr/bin/xcodebuild' (No such file or directory) you many need to run the following command in the terminal:

sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer

Execute this script via terminal while in your job’s workspace directory: sh iOS_Build.sh

I would recommend running each xcodebuild command one at a time to help ease any troubleshooting required.

If you where prompted for a password during the code signing/archiving command then you will need to change your certificate’s access control settings.

Open Keychain Access

Click System and My Certificates in the left hand side of the window and Click the lock to unlock the system keychain.

Expand your iOS certificate to expose your private key and Double Click the key.

Click Access Control tab and ensure Allow all applications to access this item is selected.

Click Save Changes

Jenkins iOS Certificate Access Control

If you where prompted for a password during the build script execution, try it again to make sure you are not required to enter a password.

Once your build is successfully simply add a new Execute Shell command build step in Jenkins and enter sh iOS_Build.sh and your done.

Jenkins iOS Build Setup Command

Step 5: Add and Setup Post Build Step – Jenkins iOS TestFlight Upload

TestFlight is a great way to distribute beta and internal applications. I have used it with great success. Likely, there is a Jenkins Plugin to upload your automated builds.

Once you have created a TestFlight account the configuration is simple. Navigate to the TestFlight API Doc page and get your API_Token and your Team_Token.

You token pair needs to be setup in: Jenkins Dashboard –> Configure System –> Scroll to TestFlight section, Enter your tokens and Click Save.

Back to the Jenkins iOS job. All you need to do is Select your Token pair setup previously and enter your .ipa file location (relative to your job’s workspace folder). Additionally, you can setup a distribution list on TestFlight for sending out notifications via Jenkins upload. After you setup a distribution list on TestFlight, supply that name in the advanced section of the Jenkins Plugin. Now you have email notifications via TestFlight.

Jenkins iOS Post Build TestFlight

Posted in Continuous Integration, iOS, Programming and tagged , , , , , .
  • sathish

    IOS app Build using git and test flight in jenkins

    I configured everything,Jenkin server run locally in mac..have shell command its run in terminal but not working in jenkins.

    This is the error m getting in console.

    Building in workspace /Users/administrator/.jenkins/jobs/IOS Build/workspace

    Fetching changes from the remote Git repository

    Fetching upstream changes from https://github.com/xx.git

    using .gitcredentials to set credentials

    Checking out Revision 2aae0a3928df2afc3ed2ef33ecc8cb7dc080db21 (origin/master)

    [workspace] $ /bin/sh -xe /var/folders/8g/_4fy7d5x4vs5y2xl3dm0x2wm0000gn/T/hudson5817658650174942757.sh

    + sh ts.sh

    sh: ts.sh: No such file or directory

    Build step ‘Execute shell’ marked build as failure

    Build step ‘Upload to Testflight’ marked build as failure

    Finished: FAILURE

    • Justin Hyland

      @disqus_900bc54o8j:disqus At first glance, Jenkins process cannot find your script. Make sure this script is in path /Users/administrator/.jenkins/jobs/IOS Build/workspace. and your execute shell script looks something like this: sh ts.sh

      Also, I would make sure the Jenkins user that is running the Jenkins Service the can run the script successfully.
      Hope that helps

    • sathish

      Thanks a lot..i was not not added shell command file in git.

      I’m getting this now:

      Moving exported product to ‘/Users/administrator/.jenkins/jobs/IOS Build/workspace/JenkinsIPAExport/BlackBoard.ipa’

      ** EXPORT SUCCEEDED **

      Uploading to testflight

      File: /Users/administrator/.jenkins/jobs/IOS Build/workspace/JenkinsIPAExport/BlackBoard.ipa

      DSYM: null

      org.apache.http.NoHttpResponseException: The target server failed to respond

      org.apache.http.NoHttpResponseException: The target server failed to respond

      Build step ‘Upload to Testflight’ marked build as failure

      Finished: FAILURE

      I already added Testflight Tokens.

      Pls assist me.

    • Justin Hyland

      This is related to network issue with your environment or a invalid api request. The TestFlight plugin is attempting to post your build but failing since the tesflight server didn’t respond.

      The help you debug this, test this process separately by executing a curl command found here: https://testflightapp.com/api/doc/

    • sathish

      Thanks justin..It was due to Testflight server,,Now its working fine!!!

    • naresh.chinna

      first time i’m working on mac ios how to setup hudson and required configuration to build source from xcode please let me know hyland

  • SHRI HARSHA

    Guys,

    I did refer this blog for the issue am facing to upload the generated .ipa to the TestFlight / HockeyApp.

    Below is the Jenkin Jobs log :

    Output directory: ‘/Users/shriharsha/.jenkins/jobs/GITTestFlight/workspace/build/Release-iphoneos/BarCodeScanner-1.0-1.0.ipa’
    Temporary Directory: ‘/var/folders/z2/kt4t5d9n0vx03tynrw9cr1tw0000gp/T/3qSHfS436T’ (will NOT be deleted on exit when verbose set)
    + /bin/cp -Rp /Users/shriharsha/.jenkins/jobs/GITTestFlight/workspace/build/Release-iphoneos/BarCodeScanner.app /var/folders/z2/kt4t5d9n0vx03tynrw9cr1tw0000gp/T/3qSHfS436T/Payload
    Program /bin/cp returned 0 : []
    ### Checking original app
    + /usr/bin/codesign –verify -vvvv /Users/shriharsha/.jenkins/jobs/GITTestFlight/workspace/build/Release-iphoneos/BarCodeScanner.app
    Program /usr/bin/codesign returned 0 : [/Users/shriharsha/.jenkins/jobs/GITTestFlight/workspace/build/Release-iphoneos/BarCodeScanner.app: valid on disk
    /Users/shriharsha/.jenkins/jobs/GITTestFlight/workspace/build/Release-iphoneos/BarCodeScanner.app: satisfies its Designated Requirement
    ]
    Done checking the original app
    + /usr/bin/zip –symlinks –verbose –recurse-paths /Users/shriharsha/.jenkins/jobs/GITTestFlight/workspace/build/Release-iphoneos/BarCodeScanner-1.0-1.0.ipa .
    Program /usr/bin/zip returned 0 : [ adding: Payload/ (in=0) (out=0) (stored 0%)
    adding: Payload/BarCodeScanner.app/ (in=0) (out=0) (stored 0%)
    adding: Payload/BarCodeScanner.app/_CodeSignature/ (in=0) (out=0) (stored 0%)
    adding: Payload/BarCodeScanner.app/_CodeSignature/CodeResources (in=3075) (out=809) (deflated 74%)
    adding: Payload/BarCodeScanner.app/BarCodeScanner (in=325008) (out=122780) (deflated 62%)
    adding: Payload/BarCodeScanner.app/Default-568h@2x.png (in=7014) (out=2150) (deflated 69%)
    adding: Payload/BarCodeScanner.app/Default.png (in=3688) (out=1827) (deflated 50%)
    adding: Payload/BarCodeScanner.app/Default@2x.png (in=6316) (out=2106) (deflated 67%)
    adding: Payload/BarCodeScanner.app/embedded.mobileprovision (in=13214) (out=7791) (deflated 41%)
    adding: Payload/BarCodeScanner.app/en.lproj/ (in=0) (out=0) (stored 0%)
    adding: Payload/BarCodeScanner.app/en.lproj/InfoPlist.strings (in=42) (out=23) (deflated 45%)
    adding: Payload/BarCodeScanner.app/en.lproj/ViewController_iPad.nib (in=892) (out=587) (deflated 34%)
    adding: Payload/BarCodeScanner.app/en.lproj/ViewController_iPhone.nib (in=3524) (out=1942) (deflated 45%)
    adding: Payload/BarCodeScanner.app/Info.plist (in=1294) (out=859) (deflated 34%)
    adding: Payload/BarCodeScanner.app/PkgInfo (in=8) (out=8) (stored 0%)
    adding: Payload/BarCodeScanner.app/ResourceRules.plist (in=168) (out=117) (deflated 30%)
    adding: Payload/BarCodeScanner.app/zbar-back.png (in=319) (out=275) (deflated 14%)
    adding: Payload/BarCodeScanner.app/zbar-help.html (in=3383) (out=1332) (deflated 61%)
    adding: Payload/BarCodeScanner.app/zbar-helpicons.png (in=22384) (out=22068) (deflated 1%)
    adding: Payload/BarCodeScanner.app/zbar-samples.png (in=1312) (out=1312) (stored 0%)
    total bytes=391641, compressed=165986 -> 58% savings
    ]
    Results at ‘/Users/shriharsha/.jenkins/jobs/GITTestFlight/workspace/build/Release-iphoneos/BarCodeScanner-1.0-1.0.ipa’
    [Release-iphoneos] $ ditto -c -k –keepParent -rsrc /Users/shriharsha/.jenkins/jobs/GITTestFlight/workspace/build/Release-iphoneos/BarCodeScanner.app.dSYM /Users/shriharsha/.jenkins/jobs/GITTestFlight/workspace/build/Release-iphoneos/BarCodeScanner-1.0-1.0-dSYM.zip
    [workspace] $ /bin/sh -xe /var/folders/z2/kt4t5d9n0vx03tynrw9cr1tw0000gp/T/hudson2728524692141356705.sh
    + ls /Users/shriharsha/.jenkins/jobs/GITTestFlight/workspace/build/Release-iphoneos
    BarCodeScanner-1.0-1.0-dSYM.zip
    BarCodeScanner-1.0-1.0.ipa
    BarCodeScanner.app
    BarCodeScanner.app.dSYM
    BarCodeScannerTests.octest
    BarCodeScannerTests.octest.dSYM
    Uploading to HockeyApp…
    java.lang.ArrayIndexOutOfBoundsException: 0
    at hockeyapp.HockeyappRecorder.perform(HockeyappRecorder.java:321)
    at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:770)
    at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:734)
    at hudson.model.Build$BuildExecution.post2(Build.java:183)
    at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:683)
    at hudson.model.Run.execute(Run.java:1770)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:89)
    at hudson.model.Executor.run(Executor.java:240)
    Build step ‘Upload to HockeyApp’ marked build as failure
    Uploading to testflight
    No file to upload was found with paths search criteria: /Users/shriharsha/.jenkins/jobs/GITTestFlight/workspace/build/Release-iphoneos/BarCodeScanner-Release-1.0.ipa
    Build step ‘Upload to Testflight’ marked build as failure
    Finished: FAILURE

    Am driving nuts to resolve this. Please suggest me in resolving this.

  • Pingback: Fix Error Could Not Lock Config File .git/config Jenkins Windows XP, Vista, 7, 8 [Solved]()

  • Pingback: Testflight - TOP 10 HOT NEWS()

  • Pingback: The Testflight - Latest Update News | Latest news()

  • ilteriscanberk

    Does this still work with Apple’s changes to Testflight?

    • Justin Hyland

      Apple purchased TestFlight after this was written. No one has reported that this no longer works. What kind of problems are you having?