Using Swift packages with authentication in Xcode Cloud
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.
I recommend creating a post-clone script that runs after Xcode Cloud clones your Git repository.
Create a directory next to your Xcode project or workspace and name it
ci_scripts
.Create an executable shell script, name it
ci_post_clone.sh
, and save it in theci_scripts
directory. To avoid warnings in Xcode Cloud make it executable by runningchmod +x ci_post_clone.sh
in Terminal.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.
#!/bin/bash
## create the `netrc` file
touch ~/.netrc
## write the machine name into the `netrc file
echo "machine rbsc.repositories.cloud.sap" > ~/.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.
Subscribe to my newsletter
Read articles from Marco Eidinger directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Marco Eidinger
Marco Eidinger
I am a Software Engineer working on open source and enterprise mobile SDKs for iOS and MacOS developers written in Swift. From π©πͺ and happily living in πΊπΈ