Use CodeArtifact with the nuget or dotnet CLI
You can use CLI tools like nuget and dotnet to publish and consume packages from CodeArtifact.
This document provides information about configuring the CLI tools and using them to publish or consume packages.
Topics
Configure the nuget or dotnet CLI
You can configure the nuget or dotnet CLI with the CodeArtifact NuGet Credential Provider, with the AWS CLI, or manually. Configuring NuGet with the credential provider is highly recommended for simplified setup and continued authentication.
Method 1: Configure with the CodeArtifact NuGet Credential Provider
The CodeArtifact NuGet Credential Provider simplifies the authentication and configuration of CodeArtifact with NuGet CLI tools. CodeArtifact authentication tokens are valid for a maximum of 12 hours. To avoid having to manually refresh the token while using the nuget or dotnet CLI, the credential provider periodically fetches a new token before the current token expires.
Important
To use the credential provider, ensure that any existing AWS CodeArtifact credentials are cleared from your nuget.config file that may have
been added manually or by running aws codeartifact login to configure NuGet previously.
Install and configure the CodeArtifact NuGet Credential Provider
After you create a repository and configure the credential provider you can use the nuget or dotnet CLI tools
to install and publish packages. For more information, see
Consume NuGet packages from CodeArtifact and Publish NuGet packages to CodeArtifact.
Method 2: Configure nuget or dotnet with the login command
The codeartifact login command in the AWS CLI adds a repository endpoint and
authorization token to your NuGet configuration file enabling nuget or dotnet to connect to your
CodeArtifact repository. This will modify the user-level NuGet configuration which is
located at %appdata%\NuGet\NuGet.Config for Windows and ~/.config/NuGet/NuGet.Config
or ~/.nuget/NuGet/NuGet.Config for Mac/Linux. For more information about NuGet configurations,
see Common NuGet configurations
Configure nuget or dotnet with the login command
-
Configure your AWS credentials for use with the AWS CLI, as described in Getting started with CodeArtifact.
-
Ensure that the NuGet CLI tool (
nugetordotnet) has been properly installed and configured. For instructions, see the nugetor dotnet documentation. -
Use the CodeArtifact
logincommand to fetch credentials for use with NuGet.Note
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.
The login command will:
-
Fetch an authorization token from CodeArtifact using your AWS credentials.
-
Update your user-level NuGet configuration with a new entry for your NuGet package source. The source that points to your CodeArtifact repository endpoint will be called
.domain_name/repo_name
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.
After you create a repository and configure authentication you can use the nuget,
dotnet, or msbuild CLI clients to install and publish packages. For more information, see
Consume NuGet packages from CodeArtifact and Publish NuGet packages to CodeArtifact.
Method 3: Configure nuget or dotnet without the login command
For manual configuration, you must add a repository endpoint and authorization token to your NuGet configuration file to enable nuget or dotnet to connect to your CodeArtifact repository.
Manually configure nuget or dotnet to connect to your CodeArtifact repository.
-
Determine your CodeArtifact repository endpoint by using the
get-repository-endpointAWS CLI command.aws codeartifact get-repository-endpoint --domainmy_domain--domain-owner111122223333--repositorymy_repo--format nugetExample output:
{ "repositoryEndpoint": "https://my_domain-111122223333.d.codeartifact.us-west-2.amazonaws.com/nuget/my_repo/" } -
Get an authorization token to connect to your repository from your package manager by using the
get-authorization-tokenAWS CLI command.aws codeartifact get-authorization-token --domainmy_domainExample output:
{ "authorizationToken": "eyJ2I...viOw", "expiration": 1601616533.0 } -
Create the full repository endpoint URL by appending
/v3/index.jsonto the URL returned byget-repository-endpointin step 3. Configure nuget or dotnet to use the repository endpoint from Step 1 and authorization token from Step 2.
Note
The source URL must end in
/v3/index.jsonfor nuget or dotnet to successfully connect to a CodeArtifact repository.Example output:
Package source with Name:domain_name/repo_nameadded successfully.Note
To use a dualstack endpoint, use the
codeartifact.endpoint.region.on.aws
Consume NuGet packages from CodeArtifact
Once you have configured NuGet with CodeArtifact, you can consume NuGet packages that are stored in your CodeArtifact repository or one of its upstream repositories.
To consume a package version from a CodeArtifact repository or one of its upstream repositories with
nuget or dotnet, run the following command replacing
packageName with the name of the package you want to consume and
packageSourceName with the source name for your CodeArtifact repository in your NuGet configuration file.
If you used the login command to configure your NuGet configuration, the source name is domain_name/repo_name.
Note
When a package is requested, the NuGet client caches which versions of that package exists. Because of this behavior, an install
may fail for a package that was previously requested before the desired version became available.
To avoid this failure and successfully install a package that exists, you can either clear the NuGet cache ahead of an install with
nuget locals all --clear or dotnet nuget locals all --clear, or avoid using the cache during
install and restore commands by providing the -NoCache option for nuget
or the --no-cache option for dotnet.
To install a specific version of a package
See Manage packages using the nuget.exe CLI
Consume NuGet packages from NuGet.org
You can consume NuGet packages from NuGet.org
Publish NuGet packages to CodeArtifact
Once you have configured
NuGet with CodeArtifact, you can use nuget or dotnet to publish package versions to CodeArtifact repositories.
To push a package version to a CodeArtifact repository, run the following command with the full path to your .nupkg file
and the source name for your CodeArtifact repository in your NuGet configuration file.
If you used the login command to configure your NuGet configuration, the source name is domain_name/repo_name.
Note
You can create a NuGet package if you do not have one to publish. For more information, see Package creation workflow
CodeArtifact NuGet Credential Provider reference
The CodeArtifact NuGet Credential Provider makes it easy to configure and authenticate NuGet with your CodeArtifact repositories.
CodeArtifact NuGet Credential Provider commands
This section includes the list of commands for the CodeArtifact NuGet Credential Provider. These commands must be prefixed with
dotnet codeartifact-creds like the following example.
dotnet codeartifact-credscommand
configure set profile: Configures the credential provider to use the provided AWS profile.profileconfigure unset profile: Removes the configured profile if set.install: Copies the credential provider to thepluginsfolder.install --profile: Copies the credential provider to theprofilepluginsfolder and configures it to use the provided AWS profile.uninstall: Uninstalls the credential provider. This does not remove the changes to the configuration file.uninstall --delete-configuration: Uninstalls the credential provider and removes all changes to the configuration file.
CodeArtifact NuGet Credential Provider logs
To enable logging for the CodeArtifact NuGet Credential Provider, you must set the log file in your environment. The credential provider logs contain helpful debugging information such as:
-
The AWS profile used to make connections
-
Any authentication errors
-
If the endpoint provided is not a CodeArtifact URL
Set the CodeArtifact NuGet Credential Provider log file
export AWS_CODEARTIFACT_NUGET_LOGFILE=/path/to/file
After the log file is set, any codeartifact-creds command will append its log output to the contents of
that file.
CodeArtifact NuGet Credential Provider versions
The following table contains version history information and download links for the CodeArtifact NuGet Credential Provider.
| Version | Changes | Date published | Download link (S3) |
|---|---|---|---|
|
1.0.2 (latest) |
Upgraded dependencies |
06/26/2024 |
|
|
1.0.1 |
Added support for net5, net6, and SSO profiles |
03/05/2022 |
|
|
1.0.0 |
Initial CodeArtifact NuGet Credential Provider release |
11/20/2020 |