简体   繁体   中英

How to run AWS codeartifact login and keep default repository

I run this commande in a package.json file ( scripts > preinstall ) or ( scripts > prepare ):

aws codeartifact login --tool npm --repository my-repo --domain my-domain --domain-owner <123456789> --profile <me>

full file: ( <123456789> and <me> is modified for stackoverflow)

{
  "name": "my-app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "preinstall": "npm run co:login",
    "co:login": "aws codeartifact login --tool npm --repository my-repo --domain my-domain --domain-owner <123456789> --profile <me>",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": {
    "uuid": "^3.3.2",
    "@my-npm/my-common": "1.0.0"
  }
}

My .npmrc file before aws codeartifact login.. commande:

registry=https://registry.npmjs.org
@my-npm:registry=https://my-domain-<123456789>.d.codeartifact.eu-west-1.amazonaws.com/npm/-repo/

My .npmrc file is modified:

registry=https://my-domain-<123456789>.d.codeartifact.eu-west-1.amazonaws.com/npm/-repo/
@my-npm:registry=https://my-domain-<123456789>.d.codeartifact.eu-west-1.amazonaws.com/npm/-repo/
//my-domain-<123456789>.d.codeartifact.eu-west-1.amazonaws.com/npm/my-repo/:always-auth=true
//my-domain-<123456789>.d.codeartifact.eu-west-1.amazonaws.com/npm/my-repo/:_authToken=eyJ2ZXIiOjEsIml....

but I need keep this:

registry=https://registry.npmjs.org
@my-npm:registry=https://my-domain-<123456789>.d.codeartifact.eu-west-1.amazonaws.com/npm/-repo/
//my-domain-<123456789>.d.codeartifact.eu-west-1.amazonaws.com/npm/my-repo/:always-auth=true
//my-domain-<123456789>.d.codeartifact.eu-west-1.amazonaws.com/npm/my-repo/:_authToken=eyJ2ZXIiOjEsIml....

add --namespace @my-npm at the end of commande line

This parameter added change only the scope @my-npm:registry=... in .npmrc file.

A solution I've been using for this matter is instead of using aws codeartifact login --tool npm --repository my-repo --domain my-domain to login into aws I use a more granular approach using the following commands:

# get endpoint 
endpoint = aws codeartifact get-repository-endpoint --domain my_domain --domain-owner 111122223333 --repository my_repo --format npm

# set a scoped registry
npm config set registry endpoint --scope=@my-package

# get token
token = aws codeartifact get-authorization-token --domain my_domain --domain-owner 111122223333 --repository my_repo

# set token
npm config set //my_domain-111122223333.d.codeartifact.region.amazonaws.com/npm/my_repo/:_authToken=token

# always truth
npm config set //my_domain-111122223333.d.codeartifact.region.amazonaws.com/npm/my_repo/:always-auth=true

These commands are a deconstruction of aws codeartifact login --tool npm --repository my-repo --domain my-domain ( more info ), with the difference that instead of setting a general registry at your .npmrc file (used to set configurations for your npm) will set a scoped registry ( more info ). In this way you will be able to have you fetch your packages from the sources you want. Applying this to the package.json you can insert those commands the value of co:login or even isolate the commands in a script and call these script as the value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM