Continuous Integration With Xcode Server on MacOS
This tutorial aims to set up a Mac to run as a MacOS Server with Xcode Service and set up basic Xcode bots for the macOS Server with Xcode 8 to perform CI.
Join the DZone community and get the full member experience.Join For Free
apple has very comprehensive documentation on xcode servers and continuous integration with the osx server (app) and xcode server (xcode within the server app). you might be wondering what the point of this post is if everything is documented in the guide . apple's guide still reads as the osx server rather than the macos server, but apple has released the new macos server (5.2) with some improvements in the automated xcode builds.
this small tutorial aims to set up a mac to run as a macos server with xcode service and to set up basic xcode bots for the macos server with xcode 8 to perform continuous integration. we will cover:
- macos server basics and setup.
- configure xcode to use the xcode service of the macos server.
- set up development xcode to use xcode server on the macos server.
- create an xcode bot with an example of the xcfit swift package on github.
- run bot integrations and analyze results.
in order to set up continuous integration using macos server and xcode service, we need:
- mac or mac mini with the macos server 5.2 app. (download it from the app store .)
- xcode 8 on the server. (download it from the app store .)
- swift 3 .
- the xcode project repository hosted on github (optional for the tutorial).
- another development mac from which we can trigger xcode bots (optional for this tutorial).
what's new in macos server (5.2)
apple has renamed their old osx server as macos server . it has a lot of new things, like profile manager, a caching server, nfs, xsan 5, etc., but let's focus on what's new for the xcode service.
- the macos server isn't supporting old xcode versions. it supports xcode 8+.
- the macos server isn't supporting old swift versions. it supports swift 3+. read more here .
- xcode bots running on the osx server or xcode server are broken when server app is upgraded to 5.2, i.e., macos server. read customer reviews of the server app .
this means using there are some significant changes in this release that can affect the current ci system with xcode server. the new macos server will have following features.
custom environment variable
this isn’t new, but xcode 8 will now allow us a set of environmental variables without the creation of extra schemes.
advance trigger editing
the macos server will now have two types of triggers: script triggers and email triggers. we will have control over how to set email containers.
issue tracking and blaming
the xcode server will send an email to the person who broke the build. we can now configure bots instead of changing code.
the xcode server now re-integrate your project if it fails using same revision. it also prevents blaming for the broken build.
configurable integration users
the xcode server will have now its own user called " xcode server," which gives you full control with password as any normal macos user. setting a new user seems fairly easy in the xcode server.
now, we will see how to set up the macos server for our fresh mac or mac mini.
set up macos server
this is assuming that you have already downloaded xcode 8 on the mac and have also downloaded the macos server (5.2) app (which might cost some money, but if you already have a developer account, then you might able to redeem it for free).
start server app
once downloaded, we can launch the s erver app.
once launched for the first time, it will show you the entire tutorial for how to use the new macos server for all services.
it's worth spending some time reading this tutorial about some of the other services to see what's new there.
next, the macos server will say choose a mac . select this mac , as we are setting the same mac for the xcode server.
select user to run integrations
the next step is to select the user to run xcode bot integrations. we can use the same user who is logged in, but we have the option to use another user xcode server . there will be an option to create a new user.
we will use the same user to run the test to avoid user swapping. my username is shashi .
now, the macos server will take some time configuring, and then we will be able to connect to the macos server.
configure xcode for xcode service
the macos server has lots of other services, but we need a xcode service, which is a continuous integration service to automate the static analysis of code, build apps, run tests, archive apps, report code coverage, report test results, send notifications, etc. now, developers working on git repositories can trigger integrations by creating xcode bots. xcode integrations can be triggered manually, after code changes, or periodically.
now we have various settings like choosing xcode, setting up user permissions, and configuring development team and development devices.
the next step is to configure xcode service from macos server to run our integrations. now, select xcode service from the server app and choose the xcode 8 app installed on the mac.
choose user to run test
now, we can select here to run our test or integrations triggered from the xcode bot. we have already created an xcode server user, but it can be any user. we can switch users if we wish to. it isn't a good idea to give that user administrative rights because it's not required.
choose development team and devices
now it's time to configure details of our development team with our apple id. we need provisioning profiles, certificated to configure this. we can add real devices to the server as well. once provisioned devices connected to the mac it will start appearing in the device list.
add your git repo
assuming you are a team of developers working on the github repo, we can add a github repo to our xcode service. we can set up r epository access with ssh or http. there is a button to add multiple repositories to be hosted on the xcode server. you can do it from xcodebot setup, as well, which we will cover later in this post. let's add a swift package called xcfit as the git repo.
turn on xcode service
turn on xcode service by clicking on the button once we have everything set up. you should see it as shown in the screenshot below.
at this stage, our mac with a macos server will accept any integrations triggered by the xcode bot. now, developers can create bots as per their needs and our mac will be capable of running those integrations.
share server details with team
we have just completed all the necessary setups to run xcode bot on our mac. it's time to share the server details to the development team so that they can configure the macos server on individual xcode servers installed on the mac. the details are on the main server tab. details will be:
- host name and ip address.
- ssh access with username and password.
- network to be used.
the developers will use these details to configure the local development xcode to connect to the xcode server.
configure xcode to use xcode server
now, we have our macos server fully set up for running our xcode bots. we need to configure the local development xcode to connect to the xcode service installed on the macos server. we have all the details of the server to configure. we will configure the same mac to connect to the local xcode server, but ideally, there will other development macs connecting to the xcode server.
in order to add the macos server to xcode, launch the xcode project you are working on and select xcode > preference > account > click on + > add server .
there will an option to select a server or connect to a host. select the server that we have just configured.
add integration user
on the next screen, we will see the option to log into the user on the macos server. we have to log in with the user we have set up to run integrations. otherwise, the xcode server will show an error. we have configured my user shashi to run integrations so lets log into the server with that user.
once the user is successfully added, we should see a screen with server status "on." it should look like this:
at this stage, we have configured our local xcode to use the macos server. we should now able to create xcode bots.
create xcode bots
xcode bots are similar to the jobs in jenkins or projects in travisci . we have to configure a set of automated instructions to checkout source code, build, test, and notify, etc. the benefit of the xcode bot is that we can configure the bot straight from the xcode. we can see all the created bots in the test navigator of the xcode. we will use the xcfit github repository to configure bots.
clone the project from the github and open the project in the xcode.
$ git clone email@example.com:shashikant86/xcfit.git $ open xcfit.xcodeproj
this will open the project in xcode. go to product > create bot and name the bot xcfit bot .
add repository with ssh credentials
the next step is to add github repo to the bot. we have to add git repo with ssh. xcode will ask you to trust the identity repository. you have to select trust and use credentials for the existing ssh key.
once successfully authorized, we should able to configure the build.
add build configuration
the build configuration is the main stage to configure what we want to perform as part of the build. the xcode bot has some options like:
- scheme: this is the xcode scheme we will build.
- actions: we have to tick perform static analysis and perform test action with code coverage enabled. at this stage, we don't need perform archive action, as it's just a swift package.
- cleaning and configuration : keep this default to "never" and "use scheme setting"
we can then schedule the build whenever we want to build the xcode bot. there are three major options to build or integrate the xcode bot.
- periodically : this has the option to integrate xcode bot daily, weekly, or hourly at a specific time.
- on commit : after every commit to git repository this xcode bot will be integrated.
- manual : we have to manually click integrate to perform the xcode bot integration.
there is an option to select the device to run an integration on. we can run tests on a real device or on simulators. the provisioned devices attached to the xcode servers will appear on the list. we have various os combinations to run integrations on such as macos, ios, tvos, and watchos.
we don't an ios device or simulator, as its swift package and can be run on a mac just in case we are using ios app and want to select a specific device or ios simulator. we will select macos for this bot.
there is an option to use custom environment variables or use predefined variables to perform integrations. a full list of xcode server environmental variable is here . in our case, we don't need to select a variable.
triggers are set of actions to be performed before or after xcode bot integration. the xcode bot has following available triggers:
- pre-integration script : we can add bash scripts to run before integration starts.
- post-integration script: this involves the bash script being executed after integration is finished.
- emails about new issues : receive email notifications when a new issue comes up.
- periodic email reports : receive email notifications after regular intervals.
we don't need to select any trigger for our xcfit bot. you should now see the message that we have successfully configured our bot.
run integrations and analyze results
once the xcode bot is configured, it will automatically start integration. the typical integration will check out the source code and perform all the actions defined in the build configurations. at the end of the integration, we will see a summary.
we will also have the option to view tests, code coverage, and logs.
xcode bot in action
let's watch everything we described in action. you can watch a youtube video about this here or watch this animated gif:
congratulations! you have successfully configured the xcode bot and performed integrations.
wwdc tips for xcode server users
- dedicate a new user to run integrations from the xcode bot.
- avoid using administrative accounts for the xcode server user.
- stay logged in with fast user switching.
- disable screen lock in order to avoid ui tests failing unnecessarily.
- customize needs such as simulators, networks, and advanced provisioning.
Published at DZone with permission of Shashikant Jagtap, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.