Gestire i pacchetti Node.js

Artifact Registry ti consente di pubblicare, organizzare e installare pacchetti Node.js in repository in formato npm utilizzando npm o yarn. Puoi visualizzare i pacchetti e i file archiviati, gestire i tag di versione, installare le dipendenze nei tuoi progetti ed eliminare o ritirare le versioni inutilizzate.

Prima di iniziare

  1. Se il repository di destinazione non esiste, creane uno nuovo.
  2. Verifica di disporre delle autorizzazioni richieste per il repository.
  3. Configura l'autenticazione per npm.
  4. (Facoltativo) Configura i valori predefiniti per i comandi gcloud.
  5. Se utilizzi l'helper delle credenziali di npm per l'autenticazione, ottieni un token di accesso prima di connetterti a un repository con npm.

Ruoli obbligatori

Per ottenere le autorizzazioni necessarie per gestire i pacchetti, chiedi all'amministratore di concederti i seguenti ruoli IAM nel repository:

Per saperne di più sulla concessione dei ruoli, consulta Gestisci l'accesso a progetti, cartelle e organizzazioni.

Potresti anche riuscire a ottenere le autorizzazioni richieste tramite i ruoli personalizzati o altri ruoli predefiniti.

Ottenere un token di accesso

I token di accesso sono validi per 60 minuti. Genera un token di accesso poco prima di eseguire comandi che interagiscono con i repository.

Per ottenere un token, utilizza una di queste opzioni:

  • Utilizza il comando npx per aggiornare il token di accesso.

    1. Assicurati che le credenziali per la connessione al registro npm pubblico si trovino nel file di configurazione npm dell'utente, ~/.npmrc.

    2. Esegui questo comando nella directory del progetto Node.js.

      npx google-artifactregistry-auth
      

      Se il repository Artifact Registry è impostato come registro globale e i pacchetti non sono inclusi nell'ambito, utilizza il seguente comando in modo che possa scaricare l'helper delle credenziali dal registro npm pubblico anziché dal repository Artifact Registry.

      npm_config_registry=https://registry.npmjs.org npx google-artifactregistry-auth
      
  • Aggiungi uno script al file package.json nel tuo progetto.

    "scripts": {
     "artifactregistry-login": "npx google-artifactregistry-auth"
    }
    

    Esegui lo script nella directory del progetto Node.js.

    npm run artifactregistry-login
    

Artifact Registry legge le impostazioni del repository Artifact Registry nel file .npmrc del progetto e le utilizza per aggiungere le credenziali token al file .npmrc dell'utente. L'archiviazione del token nel file .npmrc dell'utente isola le tue credenziali dal codice sorgente e dal sistema di controllo del codice sorgente.

Aggiunta di pacchetti

Modalità repository: standard

Puoi pubblicare una versione specifica di un pacchetto una sola volta. Si tratta di una restrizione npm per garantire che i contenuti di una versione del pacchetto pubblicata siano sempre gli stessi. Di conseguenza, non puoi:

  • Sovrascrivere una versione del pacchetto pubblicandola di nuovo nel repository
  • Rimuovi un pacchetto o la relativa versione dal repository, quindi pubblica un pacchetto con lo stesso nome e numero di versione

Se non specifichi un tag quando pubblichi un pacchetto, npm aggiunge il tag latest. Per semplificare l'installazione dei pacchetti in una fase di sviluppo specifica, valuta la possibilità di pubblicarli con un tag, ad esempio beta o dev.

Artifact Registry applica nomi di pacchetto alfanumerici minuscoli per i pacchetti npm.

Per aggiungere un pacchetto:

  1. Assicurati che il nome del pacchetto in package.json includa l'ambito configurato per il repository. L'esempio seguente mostra un pacchetto con l'ambito dev-repo.

    "name": "@dev-repo/my-package"
    
  2. Se utilizzi l'helper delle credenziali per l'autenticazione con un token di accesso, ottieni un nuovo token.

  3. Aggiungi pacchetti al repository. Puoi utilizzare un comando npm o yarn.

    Per taggare il pacchetto, includi il flag --tag e sostituisci TAG con il tag che vuoi utilizzare. Se non includi il flag --tag, npm imposta automaticamente il tag su latest.

    npm publish --tag=TAG
    
    yarn publish --tag TAG
    

Visualizzare pacchetti e versioni

Modalità repository: standard, remoto, virtuale

Per ottenere informazioni sul pacchetto con npm o yarn:

  1. Se utilizzi l'helper delle credenziali per l'autenticazione con un token di accesso, ottieni un nuovo token.

  2. Esegui il comando appropriato:

    npm view
    
    yarn info
    

如需使用 Google Cloud 控制台 或 gcloud 查看软件包和软件包版本,请执行以下操作:

控制台

  1. 在 Google Cloud 控制台中打开代码库 页面。

    Apri la pagina Repositori

  2. 在代码库列表中,点击相应代码库。

    软件包页面列出代码库中的软件包。

  3. 点击一个软件包以查看它的版本。

gcloud

如需列出代码库中的软件包,请运行以下命令:

gcloud artifacts packages list [--repository=REPOSITORY] [--location=LOCATION]

替换以下内容:

  • REPOSITORY 是代码库的名称。如果您 配置了默认代码库,则可以省略此标志以使用默认值。
  • LOCATION 是代码库的区域 位置。 如果您配置了 默认位置,则您 可以省略此标志以使用默认值。

如需查看软件包的版本,请运行以下命令:

gcloud artifacts versions list --package=PACKAGE \
    [--repository=REPOSITORY] [--location=LOCATION]

替换以下内容:

  • PACKAGE 是软件包的 ID 或软件包的完全限定 标识符。
  • REPOSITORY 是代码库的名称。如果您 配置了默认 代码库,则可以省略此标志以使用默认值。
  • LOCATION 是代码库的区域 位置。 使用此标志可查看特定位置的代码库。 如果您配置了默认 位置,则可以省略此标志以使用默认值。

La visualizzazione di pacchetti e versioni dalla console Google Cloud o da gcloud CLI è disponibile solo per i repository standard e remoti.

Per i repository remoti, l'elenco restituito deve includere tutte le dipendenze dirette e transitive memorizzate nella cache del repository.

Elenco dei file in corso…

Modalità repository: standard, remoto

Puoi elencare i file in un repository, i file in tutte le versioni di un pacchetto specificato o i file in una versione specifica di un pacchetto.

Per tutti i comandi seguenti, puoi impostare un numero massimo di file da restituire aggiungendo il flag --limit al comando.

Per elencare tutti i file nel progetto, nel repository e nella posizione predefiniti quando sono configurati i valori predefiniti:

gcloud artifacts files list

Per elencare i file in un progetto, un repository e una località specifici, esegui il comando:

gcloud artifacts files list \
    --project=PROJECT \
    --repository=REPOSITORY \
    --location=LOCATION

Per elencare i file per tutte le versioni di un pacchetto specifico:

gcloud artifacts files list \
    --project=PROJECT \
    --repository=REPOSITORY \
    --location=LOCATION \
    --package=PACKAGE

Per elencare i file per una versione specifica del pacchetto:

gcloud artifacts files list \
    --project=PROJECT \
    --repository=REPOSITORY \
    --location=LOCATION \
    --package=PACKAGE \
    --version=VERSION
Per elencare i file per un tag specifico:

gcloud artifacts files list \
    --project=PROJECT \
    --repository=REPOSITORY \
    --location=LOCATION \
    --package=PACKAGE \
    --tag=TAG

Sostituisci i seguenti valori:

  • LOCATION: la posizione regionale del repository.
  • PROJECT: il tuo Google Cloud ID progetto.
  • REPOSITORY: il nome del repository in cui è archiviata l'immagine.
  • PACKAGE: il nome del pacchetto.
  • VERSION: la versione del pacchetto.
  • TAG: il tag associato al pacchetto.

Esempi

Considera le seguenti informazioni sul pacchetto:

  • Progetto: my-project
  • Repository: my-repo
  • Posizione del repository:
  • Pacchetto: my-app

Il comando seguente elenca tutti i file nel repository my-repo nella posizione all'interno del progetto predefinito:

gcloud artifacts files list \
    --location= \
    --repository=my-repo
Il comando seguente elenca i file nella versione 1.0 del pacchetto.

gcloud artifacts files list \
    --project=my-project \
    --location= \
    --repository=my-repo \
    --package=my-app \
    --version=1.0
Il comando seguente elenca i file nella versione del pacchetto con il tag 1.0-dev

gcloud artifacts files list \
    --project=my-project \
    --location= \
    --repository=my-repo \
    --package=my-app \
    --tag=1.0-dev

Pacchetti di tagging

Modalità repository: standard

You can view, add, update, and delete tags. Tags can help you manage semantic versions of your packages and streamline installation of packages at a specific stage of development.

For example, you can tag the current release candidate build with rc. Your team can then install the correct version based on the tag instead of a version specifier, and unpublishing unused pre-release versions won't break your dependencies on the release candidate package.

Viewing tags

To view tags for a package:

Console

  1. Open the Repositories page in the Google Cloud console.

    Apri la pagina Repositori

  2. Click the package to view versions and the associated tags.

  3. Select the package version to tag.

  4. In the row of the selected version, click More actions (More actions), and then click Edit tags.

  5. Type new tags into the field and then click SAVE.

gcloud

Run the command:

gcloud artifacts tags list --package=PACKAGE \
    [--repository=REPOSITORY] [--location=LOCATION]

Where

  • PACKAGE is the name of the package in the repository.
  • REPOSITORY is the name of the repository. If you configured a default repository, you can omit this flag to use the default.
  • LOCATION is a regional or multi-regional location. Use this flag to view repositories in a specific location. If you configured a default location, you can omit this flag to use the default.

For example, to view tags for the package my-package in the repository my-repo in the default location, run the command:

gcloud artifacts tags list --package=my-pkg --repository=my-repo

Creating tags

You can create a tag for a specific version of a package.

To tag an existing image in a repository:

Console

  1. Open the Repositories page in the Google Cloud console.

    Apri la pagina Repositori

  2. Click the package to view versions of the package.

  3. Select the package version to tag.

  4. In the row of the selected version, click More actions (More actions), and then click Edit tags.

  5. Type new tags into the field and then click SAVE.

gcloud

Run the following command:

gcloud artifacts tags create TAG --package=PACKAGE \
    version=VERSION [--location=LOCATION] [--repository=REPOSITORY]

Where

  • TAG is the tag you want to apply to the package.
  • PACKAGE is the name of the package in the repository.
  • VERSION is version of the package that you want to tag.
  • LOCATION is a regional or multi-regional location. Use this flag to view repositories in a specific location. If you configured a default location, you can omit this flag to use the default.
  • REPOSITORY is the name of the repository. If you configured a default repository, you can omit this flag to use the default.

For example, to create the tag release-candidate for version 1.0.0 of package my-package in the repository my-repo in the default location, run the command:

gcloud artifacts tags create release-candidate --version=1.0.0 \
    --package=my-pkg --repository=my-repo

Updating tags

You can change a tag associated with a package version.

To change an existing tag:

Console

  1. Open the Repositories page in the Google Cloud console.

    Apri la pagina Repositori

  2. Click the package to view versions of the package.

  3. Select the package version with the tag to change.

  4. In the row of the selected version, click More actions (More actions), and then click Edit tags.

  5. Edit the tag and then click SAVE.

gcloud

Run the following command:

gcloud artifacts tags update TAG --package=PACKAGE \
    version=VERSION [--location=LOCATION] [--repository=REPOSITORY]

Where

  • TAG is the tag you want to apply to the package.
  • PACKAGE is the name of the package in the repository.
  • VERSION is version of the package that you want to tag.
  • LOCATION is a regional or multi-regional location. Use this flag to view repositories in a specific location. If you configured a default location, you can omit this flag to use the default.
  • REPOSITORY is the name of the repository. If you configured a default repository, you can omit this flag to use the default.

For example, to change the tag for version 1.0.0 of package my-package to production in the repository my-repo in the default location, run the command:

gcloud artifacts tags update production --version=1.0.0 \
    --package=my-pkg --repository=my-repo

Untagging package versions

You can remove an existing tag from a package version.

To remove a tag:

Console

  1. Open the Repositories page in the Google Cloud console.

    Apri la pagina Repositori

  2. Click the image to view versions of the image.

  3. Select the image version to untag.

  4. In the row of the selected version, click More actions (More actions), and then click Edit tags.

  5. Delete the tag and then click SAVE.

gcloud

Run the following command:

gcloud artifacts tags delete TAG --package=PACKAGE \
    [--location=<LOCATION] [--repository=REPOSITORY]

Where

  • TAG is the tag you want to apply to the package.
  • PACKAGE is the name of the package in the repository.
  • LOCATION is a regional or multi-regional location. Use this flag to view repositories in a specific location. If you configured a default location, you can omit this flag to use the default.
  • REPOSITORY is the name of the repository. If you configured a default repository, you can omit this flag to use the default.

For example, to remove the tag release-candidate from package my-package in the repository my-repo in the default location, run the command:

gcloud artifacts tags delete release-candidate --package=my-pkg \
    --repository=my-repo

Installazione dei pacchetti

Modalità repository: standard, remoto, virtuale

Per installare un pacchetto dal repository di pacchetti Node.js:

  1. Se utilizzi l'helper delle credenziali per l'autenticazione con un token di accesso, ottieni un nuovo token.

  2. Utilizza il comando npm install o yarn add.

    npm

    Per installare la versione con il tag latest:

    npm install @SCOPE/PACKAGE
    

    Per installare la versione con un tag diverso:

    npm install @SCOPE/PACKAGE@TAG
    

    Per installare una versione specifica:

    npm install @SCOPE/PACKAGE@VERSION
    

    filato

    Per installare la versione con il tag latest:

    yarn add @SCOPE/PACKAGE
    

    Per installare la versione con un tag diverso:

    yarn add @SCOPE/PACKAGE@TAG
    

    Per installare una versione specifica:

    yarn add @SCOPE/PACKAGE@VERSION
    

    Sostituisci i seguenti valori:

    • SCOPE è l'ambito associato al repository. Se il repository del pacchetto Node.js non è configurato con un ambito, ometti @SCOPE/ dal comando.
    • PACKAGE è il nome del pacchetto nel repository.
    • TAG è il tag della versione che vuoi installare.
    • VERSION è il numero di versione che vuoi installare.

Quando specifichi un pacchetto come dipendenza in package.json, assicurati di includere l'ambito per il repository. L'esempio seguente mostra l'ambito @dev-repo per un pacchetto denominato my-package.

"dependencies": {
  "@dev-repo/my-package": ">=1.0.0"
}

Per i repository standard, scarichi un pacchetto direttamente dal repository.

Per un repository remoto, scarichi una copia memorizzata nella cache del pacchetto e delle relative dipendenze. Se non esiste una copia memorizzata nella cache, il repository remoto scarica il pacchetto dalla sorgente upstream e lo memorizza nella cache prima di fornirtelo. Puoi verificare che il repository remoto abbia recuperato i pacchetti dalla sorgente upstream visualizzando l'elenco dei pacchetti nel repository.

Per un repository virtuale, Artifact Registry esegue la ricerca nei repository upstream del pacchetto richiesto.

  • I repository remoti upstream scaricano e memorizzano nella cache il pacchetto richiesto se non esiste una copia memorizzata nella cache. I repository virtuali forniscono solo i pacchetti richiesti, non li archiviano.
  • Se richiedi una versione disponibile in più di un repository upstream, Artifact Registry sceglie un repository upstream da utilizzare in base alle impostazioni di priorità configurate per il repository virtuale.

Ad esempio, considera un repository virtuale con le seguenti impostazioni di priorità per i repository upstream:

  • main-repo: Priorità impostata su 100
  • secondary-repo1: Priorità impostata su 80.
  • secondary-repo2: Priorità impostata su 80.
  • test-repo: Priorità impostata su 20.

main-repo ha il valore di priorità più alto, quindi il repository virtuale lo cerca sempre per primo.

Sia secondary-repo1 sia secondary-repo2 hanno la priorità impostata su 80. Se un pacchetto richiesto non è disponibile in main-repo, Artifact Registry esegue la ricerca in questi repository. Poiché hanno lo stesso valore di priorità, Artifact Registry può scegliere di pubblicare un pacchetto da uno dei due repository se la versione è disponibile in entrambi.

test-repo ha il valore di priorità più basso e pubblica un artefatto archiviato se nessuno degli altri repository upstream lo contiene.

Eliminazione dei pacchetti

Modalità repository: standard, remoto

Puoi eliminare un pacchetto e tutte le relative versioni oppure eliminare una versione specifica.

  • Una volta eliminato un pacchetto, non puoi annullare l'azione.
  • Per i repository remoti, viene eliminata solo la copia memorizzata nella cache del pacchetto. La fonte upstream non è interessata. Se elimini un pacchetto memorizzato nella cache, Artifact Registry lo scaricherà e lo memorizzerà nuovamente nella cache la prossima volta che il repository riceverà una richiesta per la stessa versione del pacchetto.

Una volta pubblicata una versione del pacchetto, non puoi ripubblicare un pacchetto con la stessa combinazione di nome e versione, anche dopo aver eliminato la versione. Si tratta di una restrizione npm per garantire che i contenuti di una versione del pacchetto pubblicata siano sempre gli stessi.

Se vuoi incoraggiare gli utenti a installare una versione aggiornata del pacchetto, utilizza il comando npm deprecate per contrassegnare la versione precedente del pacchetto come obsoleta. Quando un utente tenta di installare il pacchetto ritirato, Artifact Registry restituisce un avviso di ritiro.

在删除软件包或软件包版本之前,请确认您已在其中告知或解决任何重要依赖项。

如需删除软件包,请按如下所述操作:

控制台

  1. 在 Google Cloud 控制台中打开代码库页面。

    Apri la pagina Repositori

  2. 在代码库列表中,点击相应代码库。

    软件包页面列出代码库中的软件包。

  3. 选择要删除的软件包。

  4. 点击删除。

  5. 在确认对话框中,点击删除。

gcloud

运行以下命令:

gcloud artifacts packages delete PACKAGE \
    [--repository=REPOSITORY] [--location=LOCATION] [--async]

替换以下内容:

  • PACKAGE 是代码库中的软件包名称。
  • REPOSITORY 是代码库的名称。如果您配置了默认代码库,则可以省略此标志以使用默认值。
  • LOCATION 是代码库的区域位置。 使用此标志可查看特定位置的代码库。如果您配置了默认位置,则可以省略此标志以使用默认值。

--async 标志会导致命令立即返回,而无需等待正在进行的操作完成。

如需删除软件包的版本,请按如下所述操作:

控制台

  1. 在 Google Cloud 控制台中打开代码库页面。

    Apri la pagina Repositori

  2. 在代码库列表中,点击相应代码库。

    软件包页面列出代码库中的软件包。

  3. 点击一个软件包以查看它的版本。

  4. 选择您要删除的版本。

  5. 点击删除。

  6. 在确认对话框中,点击删除。

gcloud

运行以下命令:

gcloud artifacts versions delete VERSION \
    --package=PACKAGE \
    [--repository=REPOSITORY] [--location=LOCATION] \
    [--async]

替换以下内容:

  • VERSION 是要删除的版本的名称。
  • PACKAGE 是代码库中的软件包名称。
  • REPOSITORY 是代码库的名称。如果您配置了默认代码库,则可以省略此标志以使用默认值。
  • LOCATION 是代码库的区域位置。 使用此标志可查看特定位置的代码库。如果您配置了默认位置,则可以省略此标志以使用默认值。

--async 标志会导致命令立即返回,而无需等待正在进行的操作完成。

Passaggi successivi