# 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](https://developer.apple.com/documentation/xcode/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.

%[https://blog.eidinger.info/xcode-133-supports-spm-binary-dependency-in-private-github-release] 

The solution for Xcode Cloud is to write a [custom build script](https://developer.apple.com/documentation/xcode/writing-custom-build-scripts) 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](https://cdn.hashnode.com/res/hashnode/image/upload/v1688332003603/44e59d5e-79fb-41b6-8fac-e16bd76e48e0.png align="center")

![Xcode Cloud - Workflow - Add Environment Variables](https://cdn.hashnode.com/res/hashnode/image/upload/v1688332013451/f34d1f22-136e-4804-8a8a-bc32f5e34282.png align="center")

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 `ci_post_clone.sh`, and save it in the `ci_scripts` directory. To avoid warnings in Xcode Cloud make it executable by running `chmod +x ci_post_clone.sh` 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](https://github.com/SAP/cloud-sdk-ios).

```bash
#!/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](https://developer.apple.com/videos/play/wwdc2021/10269).
