Part 2: iOS Build Automation using Fastlane and Jenkins

Pushkraj P. Lanjekar
7 min readMar 6, 2021
Source: https://www.jenkins.io/

What is Jenkins?

Jenkins is a free open-source automation server. It helps to automate the steps of the software development life cycle like building the product, testing it & deploy it.

What we are going to learn?

  1. Jenkins Installation
  2. Configure and prepare Jenkins
  3. Create your first Jenkins Pipeline
  4. Manage Github Webhook
  5. Configure Email Notifier
  6. Final Touch

So let’s go one by one.

  1. Jenkins Installation: There are multiple ways to install Jenkins on your system. But as we have installed Homebrew for the earlier tutorial, we will go with the same. You just need to run the below command on your terminal from any location.
brew install jenkins

Once the installation is completed then you can run the below command just to cross-check if things are installed correctly or not.

which jenkins

It should show you the path of Jenkins then you are all set.

2. Configure and prepare Jenkins: Once Jenkins is installed, it can be opened using http://localhost:8080/ It will show you a screen like

The default user name of the Jenkins portal is admin

The initial Password of the Jenkins portal is saved into a file named initialAdminPassword. You can get that password by hitting the below command on your Terminal

sudo cat/Users/YOUR_USER_NAME/.jenkins/secrets/initialAdminPassword

Copy that password from terminal and male login on Jenkins portal using admin/INITIAL_ADMIN_PASSWORD. Later on, you can change the User Name and Password from Dashboard -> Manage Jenkins -> Manage Users. Let's not worry about this now.

Once you are done with Login, Jenkins will ask you to install plugins as per your need. You can choose “Select plugins to install” option as of now. It will allow you to install a default set of plugins and any other as per your choice. As of now, we will go with 2 plugins named Git and Email Extension. We will discuss in detail these plugins in the next steps.

The next step is to create a pipeline.

3. Create your first Jenkins Pipeline: You need to click on New Item on Dashboard which will take you to the below screen.

Add pipeline name as per your choice, click on “Freestyle project” and click on OK. It will create a pipeline for you which we will configure in the next steps.

The next step is to configure your SCM. Click on Source Code Management” and then click on the option shown as “Git”.

You need to add three important details as

  • Repository URL: Prefer SSH-based URL here instead of HTTPS-based.
  • Your Github credentials: Click on Add button and it will show you the below screen. Edit only Username, Password and Description fields. Others can be as it is. Add your Github Username and Password here which will establish a connection between the Jenkins server and your repository. Once you are done with filling in all details click on OK.
  • Branch Name: Name of the branch which you want to build and process.

Once you are done with this step click on Apply and then Save.

4. Manage Github Webhook: First, let's try to understand what is Github Webhook. Webhook allows you to trigger your Jenkins Pipeline after certain actions. In our case, we will try to trigger it when any commit gets merged into a specified branch.

The prerequisite of creating a Github webhook is to have an internet-hosted Jenkins server URL. If you have configured your Jenkins on the specific server then you can use that URL. But in our case, Jenkins is configured on localhost and webhooks never work with localhost. Hence we have to make our localhost URL open on the public internet.

For time being you can use “ngrok” to solve your purpose. It will help you to open your port localhost:8080 over public internet via secured tunnels. You need to follow below steps:

  • Sign-up on https://ngrok.com/. After Sign-up it will create an Auth Token for you.
  • Download ngrok.zip and save it anywhere on your Mac. In my case, I kept it on the Desktop for easy access.
  • Unzip ZIP file which will extract your ZIP and show you a runner file named ngrok”
  • Run next command from Terminal (on a path where you have unzipped content) ./ngrok authtoken YOUR_AUTH_TOKENto configure your ngrok with designated Auth Token.
  • Run ./ngrok http 8080so that your port 8080 will be opened over the public internet server. After this command, you will see some output like

You can copy the URL from Terminal which we will use to configure the Github webhook. You can hit the above URL on the browser which should take you to the Jenkins Login page if your setup is correct.

Below are the steps to create a webhook

  • Login to your Github account and open your repository.
  • Click on Settings -> Webhooks -> Add webhook
  • Once you click on Add webhook”, you will land on the below screen where you just need to add Payload URL”. You can paste the same URL which you have copied from Terminal in the above steps. Just add /github-webhook/ next to it. Other details can be as it is.

So this is all about creating a webhook on Github.

IMPORTANT NOTE: ngrok” session is valid till your Terminal window/tab is opened. By any chance, if your Terminal window/tab gets closed then you need to run ./ngrok http 8080again in order to open your localhost:8080 port. If you do this, then just copy the URL again and just EDIT and UPDATE your webhook on Github again.

If you are configuring Jenkins on the internet-hosted server which is already open on the public internet then you need to take care of these steps.

5. Configure Email Notifier: Now we will configure the Email plugin so that emails can be sent to desired email IDs when the build gets FAILED/SUCCEEDED. Below are the steps you need to perform in order to configure the Email plugin.

  • Go to Dashboard -> Manage Jenkins -> Configure System.
  • Go to E-mail Notification” block and add below details
  • SMTP server: smtp.gmail.com (I have configured Gmail hence details are applicable or Gmail hosted emails)
  • Check Use SMTP Authentication”
  • User Name: YOUR_EMAIL_ID
  • Password: YOUR_EMAIL_PASSWORD
  • Check Use SSL
  • SMTP Port: 465
  • Check Test configuration by sending test e-mail” which will allow you to test whether your email configured properly or not. You can add any test email address and click on “Test Configuration” button. It should send the default template email on mentioned email ID if your setup is correct.
  • Click on “Apply” and “Save” to save this configuration. The next step is to revisit your pipeline again and setup Webhook and Email configurations.

6. Final Touch: Go to Dashboard” click on the pipeline you have created some time back in step #3. Then click on Configure” to complete the final setup.

Select tab “Build Triggers” and check “GitHub hook trigger for GITScm polling”. It will actually help your pipeline to make listening to your specified branch of repository and when any new commit is checked-in into that branch, the Jenkins server will automatically trigger your pipeline.

Please refer to the below screenshot for a better understanding.

The next step is to add build steps. You have to select the tab “Build Environment” and select “Execute Shell” from “Add Build Step” dropdown.

Please refer to the below screenshot for a better understanding.

In an earlier blog, we have seen how to configure Fastlane to automate your iOS builds. If you have not seen it then you can refer it from https://pushkraj-lanjekar.medium.com/ios-build-automation-using-fastlane-28390eaf391d

The next step is to add post-build action to send an email. You have to select the tab “Post-build Actions” and select “E-mail Notification” from “Add post-build action” dropdown. Add email ID/s in the fieldRecipients”. If you have multiple email IDs then you can add them using whitespace in between. As this is post-build action, it will send an email to RECIPIENTS if the build gets FAILED/SUCCEEDED.

Please refer to the below screenshot for a better understanding.

Now you can click on “Apply” and “Save” to save this pipeline. Your pipeline can be run in 2 ways

  • By clicking on “Build Now” which can be called a manual run.
  • By webhooks means when any commits get merged into a specified branch. it can be called an automated run.

You can see the status of all builds in job details as shown in the below screenshot. You can click on any build and click on “Console Output” to see details of that build.

So that's all about the automating build process using webhooks on Jenkins. Hope it helps you. Happy Learning :) Please feel free to clap on this and share it.

Part 1: iOS Build Automation using Fastlane: https://pushkraj-lanjekar.medium.com/ios-build-automation-using-fastlane-28390eaf391d

--

--

Pushkraj P. Lanjekar

Tech Lead @Fiserv • 15K+ LinkedIn Family • iOS • Objective-C • Swift • SwiftUI • TDD • Mobile App Security • Agile • Learn • Adapt • Implement • Coffee Freak