Automate Your Build with Xcode Cloud: A Step-by-Step Guide
Tags: reactnative, ios, xcodecluod, automation
Published: 2023-03-01 11:44:16





![Xcode Cloud integration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ymyi626k2vkd7t9yp82p.png)



It was really exciting moment when Apple announced Xcode Cloud to help in the build sharing process. Finally, there is no need to explore the various CI/CD platforms.


Working with Xcode is exciting, but now that Xcode cloud is available, it becomes even more so. Xcode cloud was once restricted to beta users, but now accessible to anyone and everyone.

![exited for Xcode cloud for native and react-native ci/cd](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ylvac3bgwk1m4m3rq0sh.gif)

## Requirements
Although you have been using Xcode with excellent configurations, there are still certain requirements that you must meet in order to use this feature.

**MacOS**: _macOS Monterey 12 and later_
**Xcode**: _version: 13.4.1 and later_
**Source Control**: _Xcode cloud support BitBucket, Github and GitLab_
**Enrollement**: _Must be enrolled in the Apple Developer Program_

If you are already sharing your builds via Xcode then it's great, but if not then please go for the enrollment. Once you are good you will need to add your apple id in Xcode, you can follow [Add your Apple ID account](https://help.apple.com/xcode/mac/#/devaf282080a).
Also have register your app on [App Store Connect](https://appstoreconnect.apple.com/).

Alright, I hope you got everything. From now on we are going to proceed forward in a series of steps. So lets go...
🏃🏻‍♂️🏃🏻‍♂️🏃🏻‍♂️


## Build Scripts 📝
_If you are using react native or pod or any other configurations during build process this is really an essential part in the process._

Xcode Cloud provides the additional scripting part to let you do whatever you want to do around build process and for this it uses the shell scripts to get the task done. There are three scripts which helps you complete your customisation.
All this scripts file will be under `ci_scripts` folder under you project directory where your .xcodeproj or .xcodeworkspace are placed.

- **Post-clone script**: The post-clone script runs after Xcode Cloud clones your Git repository. You might use a post-clone script to install an additional tool, or to add a new entry to a property list.
create `ci_post_clone.sh` file under the script folder you just created to integrate your scripts.

- **pre-xcodebuild script**: The pre-xcodebuild script runs before Xcode Cloud runs the xcodebuild command. You might use a pre-xcodebuild script to compile additional dependencies.
For this you will need to create `ci_pre_xcodebuild.sh` file under the same directory.

- **Post-xcodebuild script**: The post-xcodebuild script runs after Xcode Cloud runs the xcodebuild command — even if the xcodebuild command fails. You might use a post-xcodebuild script to upload artifacts to storage or another service.
And for this you will need to create `ci_post_xcodebuild.sh` file.

Remember always create your `ci_scripts` folder under the project directory where your .xcodeproject and .xcodeworkspace files are available. Creating at the wrong directory will make your scripts unexecutable.


### How to create the shell script?

If you are having trouble creating a shell script file. Simply follow this:
  `Xcode Project navigator` -> `file` -> `new file`-> `Shell`

select Shell script template and name according to the script you want to create from above. We don't need this file to be there in target, so create without selecting the target in the pop up window.

Now make your script file executable by running command _+x $filename.sh._ in the terminal.

At last in the script section add your code including the shebang like _#!/bin/sh —_ and push this file to your git repository.

If you are still having some doubts about the file and the code, here is what I followed while seting up for the react native project:
Basically I just used the `ci_post_clone.sh` so I can configure my project after the cloning process is done.

![example of ci_post_clone.sh](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/19gyybpuc1ou2dc98fvo.png)


All of my requirements for the react-native project are met by these particular steps. You can skip the node portion if you're using pod just with iOS and you can replace `yarn` with `npm install` if you are not using yarn. You can also add your own configuration here or use another script file if you're looking to do additional things on different levels.



### Workflow
Create the first workflow. Open the Xcode cloud in project from `Xcode Menu -> Product -> Xcode Cloud` and select `Create workflow`. It will open the window like this:


![select product](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u1gwc5o35i6flvy6laqc.png)



Select the team under your product. If you already chosen it in the project editor, it will be present there by default.
Now just click next button and you will be driven to the next page to configure the workflow. 



Name your workflow and start the configurations. Under general tab there aren't much things required. If you already have a repository you can link it other-wise Xcode will suggest to create the repository.

![create your workflow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/exemcmrcz6mwaltc4qg4.png)


Now moving forward to the next tab named environment. Pick the system configurations that suits for your purpose (the Xcode and macOS version and the [environment variables](https://developer.apple.com/documentation/xcode/environment-variable-reference) and click next.).

![Xcode Cloud environment config](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e2nttdov9p2gyb876zyp.png)


You can set up all the modifications and requirements you want to include for the branches under the **branch changes**. The titles there make everything clear for you to make adjustments. You can have look here as well:

![Xcode Cloud branch configurations](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/icfirq9twcc57r7g1jf6.png)


As soon as everything is done click next button and review your workflow and move ahead to allow the access to your source code.
The best part about Xcode Cloud is that it will let you do everything from Xcode itself. After the GitHub access you will be asked to confirm your app on Appstore connect. 


![Xcode Cloud: App Store Connect](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0m4tfglpu7biove3v066.png)


Now just click complete button and there you have triggered the first build with Xcode. This is how it will show the builds and the builds details:
> ![Xcode Cloud: builds list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e0vye2p7t0jozh025qcs.png)
> ![Xcode Cloud: build details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/axxvtui06qr1p4uaojxj.png)
> Under the individual build you can find the detailed log incase you face any trouble. The following official links can also help you with troubleshooting:
> [Resolving common configuration and build issues](https://developer.apple.com/documentation/xcode/resolving-common-configuration-and-build-issues)
> [Resolve GitHub Enterprise connection issues](https://developer.apple.com/documentation/xcode/resolve-github-
enterprise-connection-issues)




If you are here, means you've started the CI/CD with Xcode Cloud and If the sharing and uploading process has been finished, please write down your Xcode Cloud experience and any problems you encountered.

 **Thanks and welcome** 😃
![we are done ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fyge8ihpnz1bhf2l6qnp.gif)