KMMBridgespmPublish SPM to Another Repo

Publish SPM to Another Repo

Swift Package Manager (SPM) uses git to index and version packages. While there are some exceptions, the basic rules are as follows:

  1. The Package.swift file must be in the root of the repo
  2. Package versions are marked with git tags, and must be in strict semver format

Other than that, you can (basically) do what you want. That includes publishing the Package.swift file and git tags to a different repo than your source code.

Publishing to a different repo is more complicated to configure, and can be confusing. If your repo doesn’t rely on git tag versioning for other reasons, you should avoid doing this in most cases.

Package.swift

The SPM plugin for KMMBridge creates the Package.swift file needed to access your published binary. To do that, it needs the binary XCFramework zip file and the URL where it will be hosted. Both of these are needed to correctly configure SPM configuration.

You can use the Package.swift file generated by KMMBridge, but it will be placed in the root of your current repo by default. You can tell it to put the file somewhere else, but you’ll need to figure out how to clone your repo locally to that other location:

kmmbridge {
    spm(spmDirectory = "../OtherRepoFolder")
}

Alternatively, you can take the generated file and push it to your other repo yourself.

In any case, the Package.swift file needs to be committed to the root folder of the “other” repo.

XCFramework Binary zip

KMMBridge assembles and publishes binaries to the location you configure in Gradle. If you publish to somewhere outside of your git hosting, then this isn’t a problem. The URL will be the same. However, if you are using GitHub Packages or GitHub Releases to publish your binaries, you’ll probably want to push them to the “other” repo rather than the one from which you’re building.

GitHub Releases

To configure KMMBridge to publish a binary to a different repo, use the following:

kmmbridge {
    gitHubReleaseArtifacts(repository = "yourorg/OtherRepo")
}

You’ll also need to supply the auth token to actually publish to the release. See Authenticating to the REST API for details. The default KMMBridge GitHub Actions workflow gets this from secrets.GITHUB_TOKEN. That is supplied by GitHub Actions, but is only allowed to publish to the repo you’re running the action on. To provide a different token, you’ll need to pass a Gradle param GITHUB_PUBLISH_TOKEN to the build process.

An example CI call to publish your iOS binary might look like this:

./gradlew kmmBridgePublish -PENABLE_PUBLISHING=true -PGITHUB_PUBLISH_TOKEN=${{ YOUR_TOKEN }} --no-daemon --stacktrace

GitHub Packages

GitHub Packages uses a maven repo to publish the XCFramework binary. The GitHub Packages maven repo is “attached” to a GitHub repo. To publish the XCFramework binary to the “other” GitHub repo, you’ll either need to manually configure the maven publishing details, or use addGithubPackagesRepository() from KMMBridge, but supply details for your “other” repo. To do that, pass in some Gradle properties:

  • GITHUB_REPO - The repo target
  • GITHUB_PUBLISH_TOKEN - The access token (PAT or something equivalent)
  • GITHUB_PUBLISH_USER - (Optional) username
./gradlew kmmBridgePublish -PGITHUB_REPO=yourorg/OtherRepo -PGITHUB_PUBLISH_TOKEN=${{ YOUR_TOKEN }} // etc

We’ve never had a need to publish SPM to a different repo. As a result, there has been very little testing of this configuration. There are multiple points of failure related to cross-repo CI config with GitHub Actions, so be prepared to spend some time debugging.