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.swift
file in your application project folder to update the package dependencies to be used by your project.If the
Package.swift
file does not contain adependencies
section, add one.In the
dependencies
section of thePackage.swift
file, 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.swift
file does not contain atargets
section, add one.In the
targets
section, add the targets that will need to use the dependency.The following snippet is an example snippet showing configured
dependencies
andtargets
sections in aPackage.swift
file:... ], 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.swift
file for your package. -
Run the following
swift package-registry publish
command to publish the package. The command creates a package source archive and publishes it to your CodeArtifact repository.swift package-registry publish
packageScope
.packageName
packageVersion
For example:
swift package-registry publish
myScope
.myPackage
1.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-packages
command as follows:aws codeartifact list-packages --domain
my_domain
--repositorymy_repo
You can list the single version of the package using the
aws codeartifact list-package-versions
command as follows:aws codeartifact list-package-versions --domain
my_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_TOKEN
andCODEARTIFACT_REPO
environment 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_TOKEN
environment 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:
mkdir
testDir
&& cdtestDir
swift 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-packages
command as follows:aws codeartifact list-packages --domain
my_domain
--repositorymy_repo
You can list the single version of the package using the
aws codeartifact list-package-versions
command as follows:aws codeartifact list-package-versions --domain
my_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 clone
command. For information about cloning GitHub repositories, see Cloning a repositoryin the GitHub Docs. git clone
repoURL
Navigate to the repository that you just cloned:
cd
repoName
-
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 publish
command to create the package and publish it to your configured CodeArtifact repository.swift package-registry publish
packageScope
.packageName
versionNumber
For example:
swift package-registry publish
myScope
.myPackage
1.0.0
If you're using a Swift version that is older than 5.9, you must use the
swift archive-source
command to create the package and then use a Curl command to publish it.If you haven't configured the
CODEARTIFACT_AUTH_TOKEN
andCODEARTIFACT_REPO
environment 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-source
command:swift package archive-source
If successful, you will see
Created
in 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-packages
command as follows:aws codeartifact list-packages --domain
my_domain
--repositorymy_repo
You can list the single version of the package using the
aws codeartifact list-package-versions
command as follows:aws codeartifact list-package-versions --domain
my_domain
--repositorymy_repo
\ --format swift --namespacemy_scope
--packagepackage_name