Consuming and publishing Swift packages
Consuming Swift packages from CodeArtifact
Use the following procedure to consume Swift packages from an AWS CodeArtifact repository.
To consume Swift packages from a CodeArtifact repository
If you haven't, follow the steps in Configure the Swift Package Manager with CodeArtifact to configure the Swift Package Manager to use your CodeArtifact repository with proper credentials.
Note
The authorization token generated is valid for 12 hours. You will need to create a new one if 12 hours have passed since a token was created.
-
Edit the
Package.swiftfile in your application project folder to update the package dependencies to be used by your project.If the
Package.swiftfile does not contain adependenciessection, add one.In the
dependenciessection of thePackage.swiftfile, add the package you want to use by adding its package identifier. The package identifier consists of the scope and package name separated by a period. See the code snippet following a later step for an example.Tip
To find the package identifier, you can use the CodeArtifact console. Find the specific package version you want to use and reference the Install shortcut instructions on the package version page.
If the
Package.swiftfile does not contain atargetssection, add one.In the
targetssection, add the targets that will need to use the dependency.The following snippet is an example snippet showing configured
dependenciesandtargetssections in aPackage.swiftfile:... ], dependencies: [ .package(id: "my_scope.package_name", from: "1.0.0") ], targets: [ .target( name: "MyApp", dependencies: ["package_name"] ),... ], ...
Now that everything is configured, use the following command to download the package dependencies from CodeArtifact.
swift package resolve
Consuming Swift packages from CodeArtifact in Xcode
Use the following procedure to consume Swift packages from a CodeArtifact repository in Xcode.
To consume Swift packages from a CodeArtifact repository in Xcode
If you haven't, follow the steps in Configure the Swift Package Manager with CodeArtifact to configure the Swift Package Manager to use your CodeArtifact repository with proper credentials.
Note
The authorization token generated is valid for 12 hours. You will need to create a new one if 12 hours have passed since a token was created.
-
Add the packages as a dependency in your project in Xcode.
Choose File > Add Packages.
Search for your package using the search bar. Your search must be in the form
package_scope.package_name.Once found, choose the package and choose Add Package.
Once the package is verified, choose the package products you want to add as a dependency, and choose Add Package.
If you run into problems using your CodeArtifact repository with Xcode, see Swift troubleshooting for common issues and possible fixes.
Publishing Swift packages to CodeArtifact
CodeArtifact recommends Swift 5.9 or later and using the swift package-registry publish command to publish Swift packages. If you are using an earlier
version, you must use a Curl command to publish Swift packages to CodeArtifact.
Publishing CodeArtifact packages with the swift package-registry publish command
Use the following procedure with Swift 5.9 or later to publish Swift packages to a CodeArtifact repository with the Swift Package Manager.
If you haven't, follow the steps in Configure the Swift Package Manager with CodeArtifact to configure the Swift Package Manager to use your CodeArtifact repository with proper credentials.
Note
The authorization token generated is valid for 12 hours. You will need to create a new one if 12 hours have passed since it was created.
-
Navigate to the Swift project directory that contains the
Package.swiftfile for your package. -
Run the following
swift package-registry publishcommand to publish the package. The command creates a package source archive and publishes it to your CodeArtifact repository.swift package-registry publishpackageScope.packageNamepackageVersionFor example:
swift package-registry publishmyScope.myPackage1.0.0 You can confirm that the package was published and exists in the repository by checking in the console or using the
aws codeartifact list-packagescommand as follows:aws codeartifact list-packages --domainmy_domain--repositorymy_repoYou can list the single version of the package using the
aws codeartifact list-package-versionscommand as follows:aws codeartifact list-package-versions --domainmy_domain--repositorymy_repo\ --format swift --namespacemy_scope--packagepackage_name
Publishing CodeArtifact packages with Curl
While it is recommended to use the swift package-registry publish command that comes with Swift 5.9 or later,
you can also use Curl to publish Swift packages to CodeArtifact.
Use the following procedure to publish Swift packages to an AWS CodeArtifact repository with Curl.
If you haven't, create and update the
CODEARTIFACT_AUTH_TOKENandCODEARTIFACT_REPOenvironment variables by following the steps in Configure the Swift Package Manager with CodeArtifact.Note
The authorization token is valid for 12 hours. You will need to refresh your
CODEARTIFACT_AUTH_TOKENenvironment variable with new credentials if 12 hours have passed since it was created.-
First, if you do not have a Swift package created, you can do so by running the following commands:
mkdirtestDir&& cdtestDirswift package init git init . swift package archive-source Use the following Curl command to publish your Swift package to CodeArtifact:
You can confirm that the package was published and exists in the repository by checking in the console or using the
aws codeartifact list-packagescommand as follows:aws codeartifact list-packages --domainmy_domain--repositorymy_repoYou can list the single version of the package using the
aws codeartifact list-package-versionscommand as follows:aws codeartifact list-package-versions --domainmy_domain--repositorymy_repo\ --format swift --namespacemy_scope--packagepackage_name
Fetching Swift packages from GitHub and republishing to CodeArtifact
Use the following procedure to fetch a Swift package from GitHub and republish it to a CodeArtifact repository.
To fetch a Swift package from GitHub and republish it to CodeArtifact
If you haven't, follow the steps in Configure the Swift Package Manager with CodeArtifact to configure the Swift Package Manager to use your CodeArtifact repository with proper credentials.
Note
The authorization token generated is valid for 12 hours. You will need to create a new one if 12 hours have passed since a token was created.
-
Clone the git repository of the Swift package you want to fetch and republish by using the following
git clonecommand. For information about cloning GitHub repositories, see Cloning a repositoryin the GitHub Docs. git clonerepoURL Navigate to the repository that you just cloned:
cdrepoName-
Create the package and publish it to CodeArtifact.
Recommended: If you are using Swift 5.9 or later, you can use the following
swift package-registry publishcommand to create the package and publish it to your configured CodeArtifact repository.swift package-registry publishpackageScope.packageNameversionNumberFor example:
swift package-registry publishmyScope.myPackage1.0.0If you're using a Swift version that is older than 5.9, you must use the
swift archive-sourcecommand to create the package and then use a Curl command to publish it.If you haven't configured the
CODEARTIFACT_AUTH_TOKENandCODEARTIFACT_REPOenvironment variables, or it's been over 12 hours since you have, follow the steps in Configure Swift without the login command.-
Create the Swift package by using the
swift package archive-sourcecommand:swift package archive-sourceIf successful, you will see
Createdin the command line.package_name.zip Use the following Curl command to publish the Swift package to CodeArtifact:
You can confirm that the package was published and exists in the repository by checking in the console or using the
aws codeartifact list-packagescommand as follows:aws codeartifact list-packages --domainmy_domain--repositorymy_repoYou can list the single version of the package using the
aws codeartifact list-package-versionscommand as follows:aws codeartifact list-package-versions --domainmy_domain--repositorymy_repo\ --format swift --namespacemy_scope--packagepackage_name