Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Build Your Pipeline in Jenkins 2.0 as Code for iOS9 and XCode7 Projects

DZone's Guide to

How to Build Your Pipeline in Jenkins 2.0 as Code for iOS9 and XCode7 Projects

It's easy to set up your iOS9 project so that activities like build, test, code coverage, check style, reports, and notifications are described in only one file.

· DevOps Zone ·
Free Resource

Easily enforce open source policies in real time and reduce MTTRs from six weeks to six seconds with the Sonatype Nexus Platform. See for yourself - Free Vulnerability Scanner. 

Introduction

You can define the Continuous Integration and Continuous Delivery process as code with Jenkins 2.0 for your projects in iOS 9. Activities like build, test, code coverage, check style, reports, and notifications can be described in only one file.

What Is the Idea?

One of the DevOps goals it to build process of CI/CD with characteristics that can be written once and run always.

When you write your process, you avoid human error and can track all changes over the time. You can learn from your errors and improve your next steps.

Jenkins support this philosophy of work when including the Jenkinsfile file along with Pipeline modules. The Jenkinsfile file is used to describe all step needed in your workflow. The site Jenkins.io have a lot of information related to this topic but now, we are going to become dirty our hands with a real example.

Time Table: An Example Project

Time Table is an example to show how can we model our CI/CD process for iOS9 projects.

Source Code

The source code can be cloned or downloaded from GitHub to test it.

Setting Up Jenkinsfile

The following lines will show what do you need to include in your iOS9 project to setting up the pipeline. First of all, create a new file with the name Jenkinsfile in the project root and after adding the code behind to Jenkinsfile archive. It's simple, right?

node('iOS Node') {

    stage('Checkout/Build/Test') {

        // Checkout files.
        checkout([
            $class: 'GitSCM',
            branches: [[name: 'master']],
            doGenerateSubmoduleConfigurations: false,
            extensions: [], submoduleCfg: [],
            userRemoteConfigs: [[
                name: 'github',
                url: 'https://github.com/mmorejon/time-table.git'
            ]]
        ])

        // Build and Test
        sh 'xcodebuild -scheme "TimeTable" -configuration "Debug" build test -destination "platform=iOS Simulator,name=iPhone 6,OS=9.3" -enableCodeCoverage YES | /usr/local/bin/xcpretty -r junit'

        // Publish test restults.
        step([$class: 'JUnitResultArchiver', allowEmptyResults: true, testResults: 'build/reports/junit.xml'])
    }

    stage('Analytics') {

        parallel Coverage: {
            // Generate Code Coverage report
            sh '/usr/local/bin/slather coverage --jenkins --html --scheme TimeTable TimeTable.xcodeproj/'

            // Publish coverage results
            publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: 'html', reportFiles: 'index.html', reportName: 'Coverage Report'])


        }, Checkstyle: {

            // Generate Checkstyle report
            sh '/usr/local/bin/swiftlint lint --reporter checkstyle > checkstyle.xml || true'

            // Publish checkstyle result
            step([$class: 'CheckStylePublisher', canComputeNew: false, defaultEncoding: '', healthy: '', pattern: 'checkstyle.xml', unHealthy: ''])
        }, failFast: true|false   
    }

    stage ('Notify') {
        // Send slack notification
        slackSend channel: '#my-team', message: 'Time Table - Successfully', teamDomain: 'my-team', token: 'my-token'
    }
}

Understanding Jenkinsfile

Specify the node:

node('iOS Node') {  
    ......
}

The Jenkins node must have installed Mac OS with XCode7.

Task Definitions

Sequential tasks: check out code, build, test, and notify.

stage('Checkout/Build/Test') {
    ......
}

stage ('Notify') {
    ......
}

Parallel tasks: code coverage and check style.

stage('Analytics') {

    parallel Coverage: {
        ......
    }, Checkstyle: {
        ......
    }, failFast: true|false

}

Jenkins group tasks in stages. This task can be run as the sequential or parallel process depends on the case. The Jenkinsfile file shows both examples.

Check Out Source Code

// Checkout files.
checkout([
    $class: 'GitSCM',
    branches: [[name: 'master']],
    doGenerateSubmoduleConfigurations: false,
    extensions: [], submoduleCfg: [],
    userRemoteConfigs: [[
        name: 'github',
        url: 'https://github.com/mmorejon/time-table.git'
    ]]
])

The Pipeline SCM Step Plugin get the source code from GitHub.

Build and Test

// Build and Test
sh 'xcodebuild -scheme "TimeTable" -configuration "Debug" build test -destination "platform=iOS Simulator,name=iPhone 6,OS=9.3" -enableCodeCoverage YES | /usr/local/bin/xcpretty -r junit'

The project is compiled using the xcodebuild tool. Parameters like scheme, configuration, and destination must be set up depending on the project information.

During the tests execution, xcpretty transforms the tests result into a standard JUnit file to be consulted. The file is generated in the following location: build/reports/junit.xml.

You must have installed Xcpretty to work with tests.

Publish Test Results

// Publish test restults.
step([$class: 'JUnitResultArchiver', allowEmptyResults: true, testResults: 'build/reports/junit.xml'])

The Plugin JUnit show the tests result in a table.

You must have installed Plugin JUnit to publish tests reports.

Code Coverage

// Generate Code Coverage report
sh '/usr/local/bin/slather coverage --jenkins --html --scheme TimeTable TimeTable.xcodeproj/'

Slather generate the code coverage report. Slater can be configured to show the report in html format and saved in the following location: ./html/index.html.

You must have installed Slather to generate code coverage reports.

Publish Code Coverage Report

// Publish coverage results
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: 'html', reportFiles: 'index.html', reportName: 'Coverage Report'])

The Plugin HTML Publisher is used to publish the code coverage reports.

You must have installed Plugin HTML Publisher to publish index.html file generated by Slather.

Generate Checkstyle Report

// Generate Checkstyle report
sh '/usr/local/bin/swiftlint lint --reporter checkstyle > checkstyle.xml || true'

SwiftLint is used to evaluate the source code. The report is generated in checkstyle and stored in the checkstyle.xml file under the project root folder.

You must have installed SwiftLint to generate checkstyle reports.

Chekstyle Publisher Report

// Publish checkstyle result
step([$class: 'CheckStylePublisher', canComputeNew: false, defaultEncoding: '', healthy: '', pattern: 'checkstyle.xml', unHealthy: ''])

The Checkstyle Plugin is used to publish the reports generated by SwiftLint.

You must have installed Checkstyle Plugin to show SwiftLint reports.

Send Slack Notification

// Send slack notification
slackSend channel: '#my-team', message: 'Time Table - Successfully', teamDomain: 'my-team', token: 'my-token'

The Slack Notification Plugin is used to send notifications to channel team. The plugin must be configured according to Slack account and channel team. The values you need setup are channel, message, teamDomain , and token.

You must have installed Slack Notification Plugin to send notifications.

Setting up Jenkins Job

Create New Job

Create a new Jenkins job with the name time-table and selecting Pipeline option. After do click OK button.

Setting Up Pipeline

The Pipeline configuration must be the same like this:

      Definition: Pipeline script from SCM
             SCM: Git
    Repositories: https://github.com/mmorejon/time-table.git
Branch Specifier: */master
     Script Path: Jenkinsfile

Build Job

Run time-table job twice and see the results.

Conclusion

Now you know how to write your own CI/CD process using Pipeline Modules in Jenkins 2.0. It's your turn to build the Jenkinsfile that needs your team.

Automate open source governance at scale across the entire software supply chain with the Nexus Platform. Learn more.

Topics:
devops ,jenkins ,pipelines ,xcode7

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}