Categories
apple ios

Setting up GitLab runner on AWS Mac EC2 for React Native iOS builds

(*3*)

(*2*)

While development and provisioning Fairo banking app we’ve spent somewhat a while on putting in the CI/CD processes in GitLab for our cell application this is written in React Native. In this newsletter we will be able to provide step-by-step information on the right way to create AWS EC2 Mac cases and the right way to arrange important tooling for computerized builds of iOS React Native apps the use of GitLab and its runners.

Namely we will be able to center of attention in this steps:

  • Creating Mac powered EC2 occasion
  • (*8*)Setting up the tooling for iOS builds on faraway EC2 Mac occasion

  • Registering, working and verifying GitLab runner on faraway Mac occasion

Having dozens of merges into grasp department day by day we had to automate our supply pipeline in an effort to supply newest builds to our testers and advertise solid builds to beta/manufacturing.

For iOS we’ve began out through having previous MacGuide Pro (2021 sequence) mendacity at the desk within the place of business with GitLab runner put in hooked up to the web by way of native WiFi. So all of the steps will also be reproduced for native machines additionally👌.

Switching to AWS EC2 Mac occasion was once an try to enhance efficiency of construct system but in addition safety, balance and scalability.

Setup — Amazon EC2 Mac cases are constructed on Apple Mac mini computer systems, that includes eighth Generation 3.2GHz (4.6GHz turbo) Intel Core i7 processors with 6 bodily/12 logical cores, and 32GiB of Memory.

The OS these days supported are macOS 10.14 (Mojave) 10.15 (Catalina) and macOS 11 (Big Sur).

Availability — The Mac cases are these days to be had in areas: US East (N. Virginia), US East (Ohio), US West (Oregon), Europe (Ireland), Asia Pacific (Singapore) and extra.

Pricing — When it involves prices, working an EC2 Mac occasion will also be beautiful pricy. Mac cases are to be had handiest as naked steel cases on Dedicated Hosts with a minimal allocation duration of 24 hours. Simply mentioned, spinning up the example will value you just below $26 an afternoon ($1.083 in line with hour).

Scaling — Dedicated hosts can’t be hooked up to Auto Scaling Group.

If those possible issues didn’t scare you, let’s proceed!

In step one it’s a must to allocate the committed host. This will also be accomplished by way of AWS Console

or simply merely working the next command:

aws ec2 allocate-hosts --region us-east-1 --availability-zone us-east-1b --quantity 1 --instance-type mac1.steel --tag-specifications 'ResourceType=dedicated-host,Tags=[{Key=Name,Value=mac-1}]'

In the console, you will have to see a newly created committed host.

To release the example from console, pass to EC2 cases and select one of macOS offered. The occasion variety will have to be mac1.steel. Make positive you select the committed occasion you simply allotted as a bunch. The occasion will have to be introduced in a public subnet so it will get a public ip related.

AWS Mac cases include 60 GB disk dimension out of the field. To be capable to have compatibility Xcode, do common builds and download all JavaScript, Java and Cocoapods dependencies we want to allocate better garage quantity.

The vital step is to configure safety workforce to permit ssh get right of entry to out of your IP deal with.

Click Review and Launch

You might be induced with an solution to create a new key pair or use present one. Create a brand new one a download it.

Important: Save your keys securely. Once loosing it, you’ll no longer be capable to get right of entry to the example. The key pairs will also be turned around, however it’s a must to apply a sophisticated “occasion restoration” process.

Click Launch cases.

In a few moments, you will have to see a brand new working occasion with public ip related. 🍻

Once the example passes standing exams, you’ll be able to hook up with it the use of person user-ec2 and public ip deal with of began occasion. After connecting you’ll be able to upload different customers too

ssh -i mac-1.pem [email protected]

you’ll be able to save this command as an alias into your ~/.bashrc or ~/.zshrc relying on which shell you might be the use of. For instance upload this into your rc record (don’t omit to reload terminal afterwards):

alias ='ssh -i ~/.ssh/mac-1.pem [email protected]'

Now for those who variety into terminal, you’ll get hooked up to the faraway system.

NOTE: In case you might be dealing with timeouts when connecting to the system, you should definitely take a look at the inbound laws of safety workforce related to EC2 occasion. You will have to permit the get right of entry to handiest from depended on IP addresses.

Installing oh my zsh (non-compulsory, however really useful)

This step is non-compulsory, however extremely really useful. Oh My Zsh is cool and vibrant shell that makes the paintings in terminal a laugh. You too can exchange the PS1 atmosphere variable on your ~/.zshrc to make advised glance superior (and other out of your native system):

export PS1='🍏☁️ %(?:%{%}➜ :%{%}➜ ) %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)'

Paste the code above into your shell config (e.g. ~/.zshrc ) after which reload config in present terminal window through working . ~/.zshrc or open new terminal window.

In case you’ll run out of area for your MacOS occasion, there’s a chance to glue any other quantity.

To connect the amount apply the directions within the AWS documentation (*1*)Increase the scale of an EBS quantity for your Mac occasion. Here are the instructions copied from the documentation that you wish to have to run in an effort to connect the amount:

To make higher disk area to be had to be used

(*15*)Copy and paste the next instructions from faraway Mac occasion. When induced, input a ‘y’.

PDISK=$(diskutil checklist bodily exterior | head -n1 | reduce -d" " -f1)APFSCONT=$(diskutil checklist bodily exterior | grep "Apple_APFS" | tr -s " " | reduce -d" " -f8)sudo diskutil repairDisk $PDISK

Copy and paste the next command.

sudo diskutil apfs resizeContainer $APFSCONT 0

(*11*)If a success, you will have to see have extra space on disk:

(*10*)To be capable to run iOS builds on mac system we’d like Xcode, node, yarn and ruby bundler this is used to put in and run Fastlane.

Cloning repo with React Native app to faraway system

It helped so much to clone app repository on faraway Mac system and to start with run manually all of the steps that can ultimately run within the GitLab runner to test that every one works as anticipated. To achieve this we had to create new SSH key (with out passphrase) on faraway Mac system. Run following command:

ssh-keygen

Confirm all of the choices with out inputing any passwords and as soon as accomplished replica the content material of ~/.ssh/id_rsa.pub and paste it to the SSH keys within the GitLab person settings.

Then clone the repository onto the faraway Mac system.

Installing Xcode

This is the one step that can not be computerized or accomplished by way of CLI (or a minimum of we didn’t give you the chance the right way to do it). In order to put in Xcode you’ll have to attach remotely to the system by way of GUI and set up the Xcode by way of AppStore.

Important: To be capable to log into an occasion with VNC, you wish to have to reset the password of ec2-user.

sudo /usr/bin/dscl . -passwd /Users/ec2-user <your password>

If requested for sudo password, simply hit Enter

To attach remotely with GUI from Mac OS open terminal and hook up with faraway Mac system by way of SSH with port tunnelling possibility:

ssh -L 5900:localhost:5900 -i mac-1.pem [email protected]

Then open finder and press Cmd+Ok and enter the vnc://localhost:5900 into the pop up window, verify.

This will attach you to the faraway Mac with GUI. You must login as ec2-user.

Open the AppStore, login on your Apple ID and set up the newest model of the Xcode.

After Xcode is put in, attempt to run it and settle for all of the licenses. You can do that additionally from terminal through working the next command (that is required handiest as soon as):

sudo xcodebuild -license settle for

If requested for sudo password, simply hit Enter

Installing node

To be capable to bring together JavaScript we’d like node v >= 12 and yarn packager. We can set up node from brew, which can set up newest node model:

brew set up node

after which set up yarn as a substitute of npm

npm set up --global yarn

Try to make sure the set up through putting in challenge JS dependencies by way of yarn.

cd [project_folder] && yarn

Installing ruby bundler

For orchestration of the construct procedure we use Fastlane device which is in keeping with Ruby and we’d like Ruby bundler to run it. To set up simply run

sudo gem set up bundler

Bundler model will have to fit the model in Gemfile.lock within the challenge folder (in case you have any). To set up an identical model you’ll be able to run the next command from the challenge folder. This command will learn model from Gemfile.lock and use it for bundler set up:

sudo gem set up bundler -v "$(grep -A 1 "BUNDLED WITH" Gemfile.lock | tail -n 1)"

It is vital to have handiest one model of ruby put in on faraway Mac system — the device one. We have additionally attempted to put in bundler by way of rvm however later we’ve skilled that GitLab runner was once returning “ERROR: Build failed with: go out standing 1” in an instant after get started and it was once because of provide rvm folders.

In case you wish to have to uninstall rvm, apply this StackOverflow reaction.

Solving the ruby/config.h’ record no longer discovered error

When putting in the bundler chances are you’ll enjoy an error that states that “‘ruby/config.h’ record no longer discovered’. We didn’t discover a higher answer on the net rather then making a symlink to the ruby model introduced through Xcode. The trail under may vary a little on other machines, however with little effort it’s imaginable to search out the positioning of ruby in Xcode folder construction. For us the trail was once following:

cd /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/come with/ruby-2.6.0

And the symlinks that must be accomplished (from the folder above) is:

sudo ln -s universal-darwin20 universal-darwin19

By this we will have to have all of the important tooling put in.

(*12*)To set up runner on mac we’ve used the professional documentation (the guide set up). Just run:

sudo curl --output /usr/native/bin/gitlab-runner "https://gitlab-runner-downloads.s3.amazonaws.com/newest/binaries/gitlab-runner-darwin-amd64"

This will set up runner onto the device. Don’t omit to make the binaryexecutable:

sudo chmod +x /usr/native/bin/gitlab-runner

Registering the runner.

Before runner will settle for any Jobs from GitLab Pipeline it must be 1) registered, 2) began, 3) run 🥺

To sign up the runner we will be able to want the token from GitLab (will also be present in GitLab challenge > Settings > CI/CD > Runners (make bigger)). To sign up runner, run on faraway Mac:

gitlab-runner sign up

And enter following attributes when requested:

  • GitLab URL: https://gitlab.com/
  • (*4*)Token [the one copied from GitLab]

    (*6*)Tag: you’ll be able to selected to tag the runner, then it’s going to be development handiest tagged phases in gitlab-ci.yml. We selected to tag our runner in order that this runner accepts handiest iOS construct Jobs. We have created a tag mac-in-cloud

Running the runner

First we need to get started the runner

gitlab-runner get started

and examine that it has hooked up to the GitLab (pass to identical location as the place the token was once generated in GitLab settings):

Such runner will nonetheless no longer be taking any jobs from the pipeline and the roles might be in pending standing. For runner so to take jobs from pipeline we additionally must run the runner:

gitlab-runner run

or use the next model of the command to begin it within the background

gitlab-runner run &

(*14*)Check the standing of the runner.

At this level all of the tooling will have to be put in and runner up and able to just accept new jobs. If you will have .gitlab-ci.yml configured with Fastlane already you’ll be able to attempt to construct your app on a faraway Mac system. If no longer, learn additional.

In subsequent article we will be able to configure GitLab pipeline and Fastlane device to construct and securely signal an application on faraway Mac system and distribute it to Firebase, Play Store or AppStore.

In case the object was once useful we will be able to be thankful if you’ll apply us right here and strengthen us with some claps 👏. In go back we will be able to percentage much more cool tech articles.

Feel unfastened to remark and ask for main points, percentage your pains and learnings.

This article was once written through tech fans Andrej & Miro these days operating at CreativeDock.