Continuous Integration With GitHub, Fastlane & Jenkins

In this tutorial, you’ll learn how to use GitHub, Travis, Jenkins and fastlane to make Continuous Integration part of your daily workflow. By Audrey Tam.

4.7 (28) · 1 Review

Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Emailing From Jenkins

First, you need an SMTP (Simple Mail Transfer Protocol) server. You could configure one with Postfix, but this tutorial uses smtp.gmail.com — you need a Google account to follow along.

This time, you must configure both your project and your Jenkins system.

Configuring Your Project

Start with your project: Use the Numero menu to get back to Configure, then select the Post-build Actions tab.

From the Add post-build action menu, select Editable Email Notification:

In the form that appears, you don’t have to edit anything, but note the value of Project Recipient List is $DEFAULT_RECIPIENTS — you’ll need to make sure to edit this field, when you find it.

Below this form, click Advanced Settings… to see more options: The default trigger is Failure – Any, so you don’t have to do anything to prevent success emails.

To duplicate the default behavior of Travis CI, select Always from the Add Trigger menu:

Click Advanced… in the Always section and add your email address to Recipient List:

Note: This is just insurance, to make sure the recipient list isn’t empty.

Click Save to return to your project page.

Configuring Your Jenkins System

Now to configure your Jenkins system: Use the Jenkins menu to select Manage Jenkins ▸ Configure System:

Scroll down to Extended E-mail Notification and settle in for lots of form-filling!

  1. For SMTP server, type smtp.gmail.com, then click Advanced….
  2. Check Use SMTP Authentication, then type your Google User Name and Password.
  3. Check Use SSL.
  4. For SMTP port, type 465.
  5. For Default Recipients (aha!), type your email address.
  6. Click Apply to save your entries but stay on this page.

Scroll all the way down, to (plain old) E-mail Notification and click Advanced…: All your email settings should be already here; if not, enter them again.

Right down at the bottom, check Test configuration by sending test e-mail, then fill in your email address.

Before you click Test configuration, there’s one more thing you need to do. Because Google has tightened up security, go to Less secure app access and flick the switch to ON:

Now return to Jenkins and click Test configuration. Wait a bit, and you should see “Email was successfully sent”. Check your email to find:

Note: If you get error messages about “Username and Password not accepted”, check your SMTP Authentication User Name and Password in both places. Also switch secure apps access OFF then ON, maybe more than once.

Pushing to GitHub

Now edit the code in Converter.swift to make your second unit test pass. Edit the convert(_:) method so it returns a String with the correct number of “I” characters:

func convert(_ number: Int) -> String {
  return String(repeating: "I", count: number)
} 

Commit and push to GitHub, then watch your build’s console log — it should end with “2 tests, with 0 failures” and an email sent to your email address:

20:54:28 Test Suite 'ConverterTests' passed at 2019-02-19 20:54:28.700.
20:54:28     Executed 2 tests, with 0 failures (0 unexpected) in 0.002 (0.003) seconds
20:54:28 Test Suite 'NumeroTests.xctest' passed at 2019-02-19 20:54:28.701.
20:54:28     Executed 2 tests, with 0 failures (0 unexpected) in 0.002 (0.003) seconds
20:54:28 Test Suite 'All tests' passed at 2019-02-19 20:54:28.701.
20:54:28     Executed 2 tests, with 0 failures (0 unexpected) in 0.002 (0.004) seconds
20:54:29 
20:54:29 
20:54:29 Test session results and logs:
20:54:29    /Users/Shared/Jenkins/Library/Developer/Xcode/DerivedData/Numero-bhnpnysdndqcwobwddhndhxectdi/Logs/Test/Run-Numero-2019.02.19_20-54-23-+1100.xcresult
20:54:29 
20:54:29 2019-02-19 20:54:29.034 xcodebuild[49013:36278327] [MT] IDETestOperationsObserverDebug: 1.783 elapsed -- Testing started completed.
20:54:29 2019-02-19 20:54:29.034 xcodebuild[49013:36278327] [MT] IDETestOperationsObserverDebug: 0.000 sec, +0.000 sec -- start
20:54:29 2019-02-19 20:54:29.034 xcodebuild[49013:36278327] [MT] IDETestOperationsObserverDebug: 1.783 sec, +1.783 sec -- end
20:54:29 ** TEST SUCCEEDED **
20:54:29 
20:54:29 Testing started on 'iPhone 8'
20:54:29 Email was triggered for: Always
20:54:29 Sending email for trigger: Always
20:54:29 Sending email to: audrey.tam@xxxxxxx
20:54:33 Finished: SUCCESS

And there’s the email:

Whew! That took a while longer than Travis CI, but you’ve got the basic CI workflow happening with pushes to GitHub triggering Jenkins builds, which trigger email notifications. Take a well-earned break!

Note: Remember to turn off Less secure app access on your Gmail account.

Using fastlane With Jenkins

To use fastlane with Jenkins, you need to install it on your Mac — follow the instructions in our fastlane Tutorial: Getting Started: Enter ruby -v to check you have Ruby 2.0 and xcode-select --install to ensure you have Xcode Command Line Tools (CLT), then enter this command to install fastlane in /usr/local/bin:

sudo gem install -n /usr/local/bin fastlane --verbose

While fastlane is installing, in your Jenkins project’s Build ▸ Command field, replace the xcodebuild command with this:

fastlane scan

With fastlane installed, cd to the root directory of Numero, and enter this command:

fastlane init

When fastlane asks “What would you like to use fastlane for?”, select 4. Manual setup. This just adds Gemfile and a fastlane folder to your project. Commit and push to GitHub, to start the next build.

Note: If Jenkins says it doesn’t know where fastlane is, specify the full path in the Command field. You can find the full path in Terminal, with which fastlane.

Cleaning Up

Jenkins runs as a launch daemon so it can restart after you exit or even kill the process. To stop Jenkins, run this command in Terminal:

sudo launchctl unload /Library/LaunchDaemons/org.jenkins-ci.plist

The launch daemon restarts when you restart your Mac, which you only discover when you try to expose another service on localhost:8080 and find it’s already in use! To completely uninstall Jenkins, run this command:

/Library/Application Support/Jenkins/Uninstall.command

Check that this has deleted Applications/Jenkins and /Users/Shared/Jenkins, and also the unnamed standard user in System Preferences ▸ Users & Groups — when you restart your Mac, you shouldn’t see the option to login as Jenkins.

Where to Go From Here?

You can download the final project using the link at the top or bottom of this tutorial.

In this tutorial, you got started using both Travis CI and Jenkins. Travis CI is a super-easy pathway to getting started with CI, so no more excuses! Jenkins takes more effort to set up, but there’s no additional cost for private repositories, and it’s more flexible than Travis CI.

Here are some useful references for Travis CI, Jenkins and fastlane:

There are many other CI services or products. The fastlane documentation includes a short list of those that integrate with fastlane.

Beyond CI are the CDs: Continuous Delivery, where you automate your release process so you’re always ready to deploy changes to your users, and Continuous Deployment, where you actually deploy every change automatically. You can move on to CD with a combination of Jenkins pipelines and fastlane. Or you might find Travis CI’s deploy command is sufficient — for example, to automatically deploy to Heroku.

Feel free to add comments or questions in the forum below!