Configure the Swift Package Manager with CodeArtifact
To use the Swift Package Manager to publish packages to or consume packages from AWS CodeArtifact,
you'll first need to set up credentials to access your CodeArtifact repository. The recommended method for configuring the
Swift Package Manager CLI with your CodeArtifact credentials and repository endpoint is by using the
aws codeartifact login
command. You can also configure the Swift Package Manager manually.
Configure Swift with the login command
Use the aws codeartifact login
command to configure the Swift Package Manager with CodeArtifact.
Note
To use the login command, Swift 5.8 or later is required and Swift 5.9 or later is recommended.
The aws codeartifact login
command will do the following:
-
Fetch an authentication token from CodeArtifact and store it in your environment. How the credentials are stored depends on the operating system of the environment:
macOS: An entry is created in the macOS Keychain application.
Linux and Windows: An entry is created in the
~/.netrc
file.
In all operating systems, if a credentials entry exists, this command replaces that entry with a new token.
Fetch your CodeArtifact repository endpoint URL and add it to your Swift configuration file. The command adds the repository endpoint URL to the project level configuration file located at
/path/to/project/.swiftpm/configuration/registries.json
.
Note
The aws codeartifact login
command
calls swift package-registry
commands that must be run from the directory that contains the
Package.swift
file. Because of this,
aws codeartifact login
command must be run from within the Swift project.
To configure Swift with the login command
Navigate to the Swift project directory that contains your project's
Package.swift
file.Run the following
aws codeartifact login
command.If you are accessing a repository in a domain that you own, you don't need to include
--domain-owner
. For more information, see Cross-account domains.aws codeartifact login --tool
swift
--domainmy_domain
\ --domain-owner111122223333
--repositorymy_repo
\ [--namespacemy_namespace
]
The --namespace
option configures the application to only consume packages from
your CodeArtifact repository if they're in the designated namespace. CodeArtifact namespaces are synonymous with scopes, and are used to
organize code into logical groups and to
prevent name collisions that can occur when your code base includes multiple libraries.
The default authorization period after calling login
is 12 hours, and login
must
be called to periodically refresh the token. For more information about
the authorization token created with the login
command, see
Tokens created with the login command.
Configure Swift without the login command
While it is recommended that you configure Swift with the aws codeartifact login command, you can also configure the Swift Package Manager without the login command by manually updating the Swift Package Manager configuration.
In the following procedure, you will use the AWS CLI to do the following:
Fetch an authentication token from CodeArtifact and store it in your environment. How the credentials are stored depends on the operating system of the environment:
macOS: An entry is created in the macOS Keychain application.
Linux and Windows: An entry is created in the
~/.netrc
file.
Fetch your CodeArtifact repository endpoint URL.
In the
~/.swiftpm/configuration/registries.json
configuration file, add an entry with your repository endpoint URL and authentication type.
To configure the Swift without the login command
In a command line, use the following command to fetch a CodeArtifact authorization token and store it in an environment variable.
Replace
my_domain
with your CodeArtifact domain name.Replace
111122223333
with the AWS account ID of the owner of the domain. If you are accessing a repository in a domain that you own, you don't need to include--domain-owner
. For more information, see Cross-account domains.
Get your CodeArtifact repository's endpoint by running the following command. Your repository endpoint is used to point the Swift Package Manager to your repository to consume or publish packages.
Replace
my_domain
with your CodeArtifact domain name.Replace
111122223333
with the AWS account ID of the owner of the domain. If you are accessing a repository in a domain that you own, you don't need to include--domain-owner
. For more information, see Cross-account domains.Replace
my_repo
with your CodeArtifact repository name.
The following URL is an example repository endpoint.
https://
my_domain
-111122223333.d.codeartifact.us-west-2
.amazonaws.com/swift/my_repo
/Note
To use a dualstack endpoint, use the
codeartifact.
endpoint.region
.on.awsImportant
You must append
login
onto the end of the repository URL endpoint when used to configure the Swift Package Manager. This is done for you in the commands of this procedure.With these two values stored in environment variables, pass them to Swift using the
swift package-registry login
command as follows:Next, update the package registry used by your application so that any dependency will be pulled from your CodeArtifact repository. This command must be run in the project directory where you are trying to resolve the package dependency:
The
--scope
option configures the application to only consume packages from your CodeArtifact repository if they're in the designated scope. Scopes are synonymous with CodeArtifact namespaces, and are used to organize code into logical groups and to prevent name collisions that can occur when your code base includes multiple libraries.-
You can confirm the configuration has been set up correctly by viewing the contents of the project level
.swiftpm/configuration/registries.json
file by running the following command in your project directory:$ cat .swiftpm/configuration/registries.json { "authentication" : { }, "registries" : { "[default]" : { "url" : "https://my-domain-111122223333.d.codeartifact.us-west-2.amazonaws.com/swift/my-repo/" } }, "version" : 1 }
Now that you've configured the Swift Package Manager with your CodeArtifact repository, you can use it to publish and consume Swift packages to and from it. For more information, see Consuming and publishing Swift packages.