Using Swift packages with authentication in Xcode Cloud

Using Swift packages with authentication in Xcode Cloud


2 min read

Third-party SDKs may use Swift packages to vent their binary frameworks from a server that requires authentication.

In Xcode Cloud you will encounter the error Could not resolve package dependencies: failed downloading ... if you don't provide the needed credentials for downloading the binary frameworks in such Swift packages.

Apple's article about Building Swift packages or apps that use them in continuous integration workflows talks about ssh credentials but a neat solution is using a netrc file.

In a previous blog, I described how you can use a netrc file to store credentials for authentication as SPM will evaluate your netrc file during package resolution and will add the HTTP Authorization request header for the download calls.

The solution for Xcode Cloud is to write a custom build script that creates the netrc file before the package resolution starts.

You don't need to hard-code the password in the script. Obtaining the password from an environment variable, created in Xcode Cloud, is better.

Xcode Cloud - Workflow - Environment Variables

Xcode Cloud - Workflow - Add Environment Variables

I recommend creating a post-clone script that runs after Xcode Cloud clones your Git repository.

  1. Create a directory next to your Xcode project or workspace and name it ci_scripts.

  2. Create an executable shell script, name it, and save it in the ci_scripts directory. To avoid warnings in Xcode Cloud make it executable by running chmod +x in Terminal.

  3. Add code to your custom script to create the netrc file with all the required information.

The following example will create a netrc file allowing to use the enterprise SAP BTP SDK for iOS.


## create the `netrc` file
touch ~/.netrc

## write the machine name into the `netrc file
echo "machine" > ~/.netrc
## write the username into the `netrc file
echo "login sap-myUserName" >> ~/.netrc 
## write the password, obtained from an environment variable, into the `netrc file
echo "password ${SDK_TOKEN}" >> ~/.netrc

For additional information about custom build scripts, see WWDC21: Customize your advanced Xcode Cloud workflows.

Did you find this article valuable?

Support Marco Eidinger by becoming a sponsor. Any amount is appreciated!